gmw_pt_update_location

Description

This function is one of GEO my WP's most important functions. This function responsible for saving post type locations into GEO my WP custom table in database which then make posts searchable via GEO my WP search forms. With GEO my WP by default it is only possible to assign location to a post using GMW Location section in the back-end. However, using this function it is possible to also save post's location into GEO my WP database table when creating posts from the front-end form ( via custom script or a plugin ).

Usage

<?php /*
The function must be executed after a post was created. 
You can do so using WordPress save_post hook or using another 
hook provided by the plugin or theme which also provide you with the front-end form.

The array argument passes to the function can include 4 items:
1) post_id - the post ID of the post we are updating. 
2) address - The address entered in the form. There are two ways to pass the address:
    1) String - when using a single field for the full address.
	2) array - when using multiple address fields. 
       For example ( it is not a must to use all of the fields ):
		array( 
			'street' => $street,
			'apt' => $apt,
			'city' => $city,
			'state' => $state,
			'zipcode' => $zipcode,
			'country' => $country
		);
3) additional_info ( optional ) - array holding the additional information. 
   For example ( it is not a must to use all of the fields ): 
	array( 
		'phone' => $phone,
		'fax' => $fax,
		'email' => $email,
		'website' => $website
	);
4) map_icon - the filename.ext of the map icon assiciate with the post. 
   The map_icon is being used with premium add-ons. If you have no use for it do not include it in th array.
*/

gmw_pt_update_location( $args );

Parameters

  • $args ( Array )

    array which holds the location information of the post.

Examples

<?php /* 
This example can be used with any post type. 
The update location function is triggered using the 
save_post_{post-type-name} action which fires every time 
a new post is created or an existing post is updated.  
The action hook passes the argument $post_id into the update location function.
Assuming that the address entered in the front end form 
is saved via custom field we can easily pull it using get_post_meta function and 
pass it to the update location function. 
Otherwise, if the address is saved somewhere else ( ex custom table )
you will need to pull it directly from the database 
unless there is a function provided for that ( maybe when using a different plugin ).
The example below I will simply use the post type "post" 
and the custom field "address" as the address holder.
*/

//rename your custom function as you wish ( or leave it as is ).
function gmw_update_post_type_post_location(  $post_id ) {
	
    // Return if it's a post revision
    if ( false !== wp_is_post_revision( $post_id ) )
        return;

    // check autosave //
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    //check if user can edit post
    if ( !current_user_can( 'edit_post', $post_id ) )
        return;

    //get the address from the custom field "address"
	$address = get_post_meta( $post_id, 'address', true );

	//varify that address exists. Otherwise abort the function.
	if ( empty( $address ) )
		return;
   
	//include the update location file file
	include_once( GMW_PT_PATH .'/includes/gmw-pt-update-location.php' );
  	
    //make sure the file included and the function exists
	if ( !function_exists( 'gmw_pt_update_location' ) )
		return;
   
    //Create the array that will pass to the function
	$args = array(
			'post_id' => $post_id, //Post Id of the post 
			'address' => $address // the address we pull from the custom field above
	);

	//run the udpate location function
	gmw_pt_update_location( $args );
}
//execute the function whenever post type is being updated
add_action( 'save_post_post', 'gmw_update_post_type_post_location' );
<?php /* 
This example integrates GEO my WP and Events Manager plugin. 
The script will update GEO my WP database table with the location 
entered in the Event form from the front-end and back-end. 
Note that when using GEO my WP together with Event Manager plugin 
you should disable the GMW Location section for the 
Events Edit Post page ( in the back-end ). 
The usage of Google maps API by the to plugin causes a conflict. 
*/

//rename your custom function as you wish ( or leave it as is ).
function gmw_update_event_manager_location(  $post_id ) {
	
    //verify that even is being updated
	if ( empty( $_POST['action'] ) || $_POST['action'] != 'event_save' )
		return;
        
    //Pass the submitted address field into an array
    //There is no need to change this this fields for Event manager plugin
	$address_fields = array(
			'street'   => $_POST['location_address'],
			'city'     => $_POST['location_town'],
			'state'    => $_POST['location_state'],
			'zipcode'  => $_POST['location_postcode'],
			'country'  => $_POST['location_country']
	);
 
	//include the geocoder file
	include_once( GMW_PT_PATH .'/includes/gmw-pt-update-location.php' );
 
        //make sure the file included and the function needed exists
	if ( function_exists( 'gmw_pt_update_location' ) ) {
		
        //Create the array that will pass to the function
		$args = array(
				'post_id' => $post_id, //Post Id of the post 
				'address' => $address_fields // the address field array created above
		);
		//run the geocoder function
		gmw_pt_update_location( $args );
	}
}
//execute the function whenever post type is being updated
add_action( 'save_post_event', 'gmw_update_event_manager_location' );

Placement

The function should be placed in the functions.php file of your active theme

Source Code

This filter is located in geo-my-wp/plugins/posts/includes/gmw-pt-update-location.php