simple-location/simple-location.php

64 lines
1.9 KiB
PHP

<?php
/*
Plugin Name: Simple Location
Plugin URI: http://www.danballard.com/
Description: Auto tags posts with city, country info
Version: 0.1
Author: Dan Ballard
Author URI: http://www.danballard.com
Minimum WordPress Version Required: 3.8
Tested up to: 3.8
*/
if ( is_admin() ) {
add_action( 'admin_menu', 'add_options_menu' );
add_action( 'admin_init', 'reg_settings' );
add_action( 'admin_menu', 'add_meta_to_edit' );
add_action( 'edit_attachment', 'simple_location_post_save' );
add_action( 'save_post', 'simple_location_post_save' );
}
function add_options_menu() {
add_options_page('Simple Location Options', 'Simple Location', 'manage_options', 'simple-location', 'options_page');
}
function add_meta_to_edit() {
add_meta_box('simple-location-meta', 'Simple Location', 'simple_location_meta', 'post', 'normal');
}
function simple_location_post_save($post_id) {
if ($_POST['city'] == '' || $_POST['country'] == '') {
$key = get_option('simple-location-api-key');
if ($key != '') {
include('ip2locationlite.class.php');
$ipLite = new ip2location_lite;
$ipLite->setKey($key);
$locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
print 'locations: ';
print_r($locations);
}
if ($_POST['city'] == '') {
$_POST['city'] = $locations['cityName'];
}
update_post_meta($post_id, '_simple_location_city', sanitize_text_field($_POST['city']));
if ($_POST['country'] == '') {
$_POST['country'] = $locations['countryName'];
}
update_post_meta($post_id, '_simple_location_country', sanitize_text_field($_POST['country']));
}
function simple_location_meta() {
include( plugin_dir_path(__FILE__) . 'post-form.php');
}
function options_page() {
include( plugin_dir_path(__FILE__) . 'options.php');
}
function reg_settings() {
register_setting( 'simple-location-options', 'simple-location-api-key' );
}