function to make it work with theme

This commit is contained in:
Dan Ballard 2014-01-07 08:52:48 -05:00
parent da3a9031f1
commit 039ad7a3bf
1 changed files with 49 additions and 6 deletions

View File

@ -11,6 +11,7 @@ Minimum WordPress Version Required: 3.8
Tested up to: 3.8 Tested up to: 3.8
*/ */
// register all handlers
if ( is_admin() ) { if ( is_admin() ) {
add_action( 'admin_menu', 'add_options_menu' ); add_action( 'admin_menu', 'add_options_menu' );
add_action( 'admin_init', 'reg_settings' ); add_action( 'admin_init', 'reg_settings' );
@ -20,14 +21,33 @@ if ( is_admin() ) {
} }
// resgister the SL options page in the WP settings menu
function add_options_menu() { function add_options_menu() {
add_options_page('Simple Location Options', 'Simple Location', 'manage_options', 'simple-location', 'options_page'); add_options_page('Simple Location Options', 'Simple Location', 'manage_options', 'simple-location', 'options_page');
} }
// Display the SL admin options page
function options_page() {
include( plugin_dir_path(__FILE__) . 'options.php');
}
// Register the options page options that WP should store
function reg_settings() {
register_setting( 'simple-location-options', 'simple-location-api-key' );
}
// Register the SL meta edit box on WP pist edit pages
function add_meta_to_edit() { function add_meta_to_edit() {
add_meta_box('simple-location-meta', 'Simple Location', 'simple_location_meta', 'post', 'normal'); add_meta_box('simple-location-meta', 'Simple Location', 'simple_location_meta', 'post', 'normal');
} }
// display the post edit page SL meta edit box
function simple_location_meta() {
include( plugin_dir_path(__FILE__) . 'post-form.php');
}
// Hook for posts being saved.
// If he city or country field is blank, autopopulate from ipinfodb
function simple_location_post_save($post_id) { function simple_location_post_save($post_id) {
if ($_POST['city'] == '' || $_POST['country'] == '') { if ($_POST['city'] == '' || $_POST['country'] == '') {
$key = get_option('simple-location-api-key'); $key = get_option('simple-location-api-key');
@ -38,25 +58,48 @@ function simple_location_post_save($post_id) {
$locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']); $locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
} }
} }
if ($_POST['city'] == '') { if ($_POST['city'] == '') {
$_POST['city'] = ucfirst(strtolower($locations['cityName'])); $_POST['city'] = ucfirst(strtolower($locations['cityName']));
} }
update_post_meta($post_id, '_simple_location_city', sanitize_text_field($_POST['city'])); update_post_meta($post_id, '_simple_location_city', sanitize_text_field($_POST['city']));
if ($_POST['country'] == '') { if ($_POST['country'] == '') {
$_POST['country'] = ucfirst(strtolower($locations['countryName'])); $_POST['country'] = ucfirst(strtolower($locations['countryName']));
} }
update_post_meta($post_id, '_simple_location_country', sanitize_text_field($_POST['country'])); update_post_meta($post_id, '_simple_location_country', sanitize_text_field($_POST['country']));
} }
function simple_location_meta() { function get_city() {
include( plugin_dir_path(__FILE__) . 'post-form.php'); global $post;
return get_post_meta($post->ID, '_simple_location_city', true);
} }
function options_page() { function get_country() {
include( plugin_dir_path(__FILE__) . 'options.php'); global $post;
return get_post_meta($post->ID, '_simple_location_country', true);
} }
function reg_settings() { // City shortcode
register_setting( 'simple-location-options', 'simple-location-api-key' ); function shortcode_city( $attrs ) {
return get_city();
} }
// Country shortcode
function shortcode_country( $attrs ) {
return get_country();
}
// non admin registering of shortcodes
add_shortcode( 'city', 'shortcode_city' );
add_shortcode( 'country', 'shortcode_country' );
// Tempalte function for city
function simple_location_city() {
echo get_city();
}
// Template function for country
function simple_location_country() {
echo get_country();
}