Skip to content

Instantly share code, notes, and snippets.

@Fitoussi
Last active November 26, 2018 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Fitoussi/f0181dccd3c4557067fcbfbd28f590e5 to your computer and use it in GitHub Desktop.
Save Fitoussi/f0181dccd3c4557067fcbfbd28f590e5 to your computer and use it in GitHub Desktop.
gmw_update_post_type_post_location example
/*
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' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment