gmw_update_post_location function

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 database table, which then make posts searchable via GEO my WP search forms.

With GEO my WP, by default, it is possible to add location to a post via the location section in the Edit Post page of the admin's dashboard. However, using this function you can sync an address provided via a front-end form with GEO my WP.

* Note that this function is compatibale with GEO my WP version 3.0 or higher. For previous versions of GEO my WP see the function gmw_pt_update_location().

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
		);
*/
gmw_update_post_location( $post_id, $address );

Parameters

  • $post_id ( integer )

    The post ID.

  • $address ( mixed )

    can be a string of an address or an array of address fields. The array accepts any or all of the keys 'street', 'apt', 'city', 'state', 'zipcode', and 'country'.

Examples

<?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. 
*/

/* 
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.
	if ( empty( $address ) ) {
		return;
	}

	// verify the updater function.
	if ( ! function_exists( 'gmw_update_post_location' ) ) {
		return;
	}

	//run the udpate location function
	gmw_update_post_location( $post_id, $address );
}
//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'] ) || 'event_save' !== $_POST['action'] ) {
		return;
	}

	// Verify that the updater function exists.
	if ( function_exists( 'gmw_update_post_location' ) ) {

		// Pass the submitted address field of the Event Manager plugin as an array.
		$address = array(
			'street'   => $_POST['location_address'],
			'city'     => $_POST['location_town'],
			'state'    => $_POST['location_state'],
			'zipcode'  => $_POST['location_postcode'],
			'country'  => $_POST['location_country'],
		);

		//run the updater
		gmw_update_post_location( $post_id, $address );
	}
}
//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-locator/includes/gmw-posts-locator-functions.php