available_post_types = array_diff( $post_types, $excluded ); $this->options = get_option( 'address_geocoder_options' ); // Set some default options if none are already set if( ! $this->options ) { $this->options = $this->available_post_types; } // Actions add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) ); add_action( 'save_post', array( $this, 'save_post' ), 10, 2 ); // Settings register_setting( 'address_geocoder_options', 'address_geocoder_options', array( $this, 'validate_options' ) ); } /** * Register the menu item under "Settings" */ function admin_menu() { add_options_page( 'Address Geocoder', 'Address Geocoder', 'manage_options', 'address-geocoder-options', array( $this, 'options_page' ) ); } /** * Enqueue admin scripts */ function admin_enqueue_scripts() { // Load scripts only when necessary if ( $this->is_geocoder_needed() ) { $address_geocoder_options = get_option('address_geocoder_options'); $apikey = $address_geocoder_options['apikey']; if ( ! empty( $apikey ) ) { $mapsapi = '//maps.googleapis.com/maps/api/js?key=' . $apikey; wp_register_script( 'googlemaps', $mapsapi ); wp_register_script( 'marty_geocode_js', plugins_url( '/address-geocoder.js', __FILE__ ) ); wp_enqueue_script( 'googlemaps' ); wp_enqueue_script( 'marty_geocode_js' ); } } } /** * Determine whether the geocoder metabox appears on the current page */ function is_geocoder_needed() { $pagenow = isset( $GLOBALS['pagenow'] ) ? $GLOBALS['pagenow'] : ''; if ( 'post.php' == $pagenow ) { $post_type = get_post_type( $_GET['post'] ); } elseif ( 'post-new.php' == $pagenow ) { $post_type = isset( $_GET['post_type'] ) ? $_GET['post_type'] : 'post'; } if ( !empty( $post_type ) && ! empty( $this->options ) ) { if ( in_array( $post_type, $this->available_post_types ) && 'exclude' != $this->options[ $post_type ] ) { return true; } } return false; } /** * Add the "Geocoder" meta box to the appropriate pages */ function add_meta_boxes( $post_type ) { if ( in_array( $post_type, $this->available_post_types ) && 'exclude' != $this->options[ $post_type ] ) { $current_post_type = get_post_type_object( $post_type ); $metabox_title = empty( $this->options['meta-box-title'] ) ? $current_post_type->labels->singular_name . ' Location' : $this->options['meta-box-title']; add_meta_box( 'martygeocoder', $metabox_title, array( $this, 'meta_box_html' ), $post_type, 'normal', 'high' ); } } /** * Generate the meta box HTML */ function meta_box_html( $object, $box ) { wp_nonce_field( 'save_latlng', 'geocoder_nonce' ); $address_geocoder_options = get_option('address_geocoder_options'); $apikey = $address_geocoder_options['apikey']; if ( $apikey && $apikey != '' ): ?>



Geocode Address

A Google Maps API Key is required. Go to Settings.

options['apikey'] ) ? '' : $this->options['apikey']; ?>

Google Maps API Key

Address Geocoder requires a Google Maps API Key to work. You can get a free API Key here: Instructions for obtaining a key.


Show Metabox on Post Types

options as $post_type => $status ) : ?>

/>

Meta Box Title


available_post_types as $post_type ) { if ( ! isset( $input[ $post_type ] ) ) { $input[ $post_type ] = 'exclude'; } } if ( isset( $input['meta-box-title'] ) ) { $input['meta-box-title'] = sanitize_text_field( $input['meta-box-title'] ); } return $input; } /** * Attach geocode data to posts */ function save_post( $post_id, $post ) { // Skip when nonce isn't present if ( ! isset( $_POST['geocoder_nonce'] ) || ! wp_verify_nonce( $_POST['geocoder_nonce'], 'save_latlng' ) ) { return $post_id; } // Ensure proper access rights if ( ! current_user_can( 'edit_post', $post_id ) ) { return $post_id; } // Save address $address_key = 'martygeocoderaddress'; $address = isset( $_POST[ $address_key ] ) ? sanitize_text_field( $_POST[ $address_key ] ) : ''; if ( empty( $address ) ) { delete_post_meta( $post_id, $address_key ); } else { update_post_meta( $post_id, $address_key, $address ); } // Save lat/lng $latlng_key = 'martygeocoderlatlng'; $latlng = isset( $_POST[ $latlng_key ] ) ? sanitize_text_field( $_POST[ $latlng_key ] ) : ''; if ( empty( $latlng ) ) { delete_post_meta( $post_id, $latlng_key ); } else { update_post_meta( $post_id, $latlng_key, $latlng ); } } } $address_geocoder = new Address_Geocoder(); // Backwards compatibility function get_geocode_latlng( $post_id ) { return get_post_meta( $post_id, 'martygeocoderlatlng', true ); } function get_geocode_lat( $post_id ) { $latlng = get_post_meta( $post_id, 'martygeocoderlatlng', true ); $latlng = explode( ',', $latlng ); return substr( $latlng[0], 1 ); } function get_geocode_lng( $post_id ) { $latlng = get_post_meta( $post_id, 'martygeocoderlatlng', true ); $latlng = explode( ',', $latlng ); return substr( $latlng[1], 0, -1 ); } function get_geocode_address( $post_id ) { return get_post_meta( $post_id, 'martygeocoderaddress', true ); }