Ask your WordPress questions! Pay money and get answers fast! (more info)

Copy and Modify a custom field WordPress

  • SOLVED

I have a front end plugin that produces a custom field ('location') with a combined Latitude,Longitude value e.g -32.069574412203,115.90815624919128

I want this user generated 'location' meta value to be COPIED and then SPLIT UP into 2 different custom fields ('latitude' and 'longitude').

So essentially each post will have 3 custom fields
location = -32.069574412203,115.90815624919128
latitude = -32.069574412203
longtitude = 115.90815624919128

I want a function for my functions.php file that will check ONLY those posts that are published and/or updated.

Answers (1)

2015-04-06

Francisco Javier Carazo Gil answers:

pjeaje,

Ok, I prepare the code and send you now. Could you tell me the name of the plugin?


pjeaje comments:

It doesn't matter the name of the plugin. It simply produces a custom field as per above. How will the plugin help you?


Francisco Javier Carazo Gil comments:


function save_location_meta( $post_id, $post, $update ) {

if ( "post" != $post->post_type ) {
return;
}

$location = get_post_meta( $post_id, "location", true );

if( !empty ($location) ):
$location_array = explode(",", $location);
update_post_meta( $post_id, 'latitude', $location_array[0] );
update_post_meta( $post_id, 'longitude', $location_array[1] );

endif
}
add_action( 'save_post', 'save_location_meta', 999, 3 );



Francisco Javier Carazo Gil comments:

Tell me if you have any problem.


Francisco Javier Carazo Gil comments:

The name of the plugin was only for curiosity, try with it and tell me if there ir any problem.


pjeaje comments:

WP User Frontend


pjeaje comments:

I'll test it now


Francisco Javier Carazo Gil comments:

You have to include a ; after endif:

endif;


pjeaje comments:

Doesn't seem to work...
1. can't see any new custom fields


pjeaje comments:

Wait... trying again


pjeaje comments:

The function works when the post is updated but not when the post is created via the plugin front end form


Francisco Javier Carazo Gil comments:

Ok perfect, so I have to find the correct action because the plugin doesn't fires the standard one, I look for it and tell you in a moment.


Francisco Javier Carazo Gil comments:

Ok, so you have to include:

add_action( 'wpuf_add_post_after_insert', 'save_location_meta');


Francisco Javier Carazo Gil comments:

Ok, so you have to include:

add_action( 'wpuf_add_post_after_insert', 'save_location_meta');


pjeaje comments:

[[LINK href="http://docs.wedevs.com/wpuf_add_post_after_insert/"]]http://docs.wedevs.com/wpuf_add_post_after_insert/[[/LINK]]


Francisco Javier Carazo Gil comments:

Sorry the last time, the system has duplicated the answer, include this one also:


add_action( 'wpuf_edit_post_after_update', 'save_location_meta');


Francisco Javier Carazo Gil comments:

Thanks for find it :) I have downloaded the plugin and I have been able to find both hooks.


pjeaje comments:

where do i add it?


Francisco Javier Carazo Gil comments:

After:

add_action( 'save_post', 'save_location_meta', 999, 3 );


pjeaje comments:

Only fires on update, not on creation


pjeaje comments:

function save_location_meta( $post_id, $post, $update ) {



if ( "post" != $post->post_type ) {

return;

}



$location = get_post_meta( $post_id, "location", true );



if( !empty ($location) ):

$location_array = explode(",", $location);

update_post_meta( $post_id, 'latitude', $location_array[0] );

update_post_meta( $post_id, 'longitude', $location_array[1] );



endif;

}

add_action( 'save_post', 'save_location_meta', 999, 3 );
add_action( 'wpuf_add_post_after_insert', 'save_location_meta');


Francisco Javier Carazo Gil comments:

Have you inserted this also?

add_action( 'wpuf_add_post_after_insert', 'save_location_meta');


Francisco Javier Carazo Gil comments:

Please read the thread, with so many messages you haven't read this:

<blockquote>Ok, so you have to include:


add_action( 'wpuf_add_post_after_insert', 'save_location_meta');</blockquote>

Tell me.


pjeaje comments:

Look at my last message to you


Francisco Javier Carazo Gil comments:

Also this one:

add_action( 'wpuf_edit_post_after_update', 'save_location_meta');


pjeaje comments:

Same... only firing on update


Francisco Javier Carazo Gil comments:

That's strange... I would have to see directly in the code. If you want, send me via private message credentials and I can fix it directly in your code.


pjeaje comments:

Let me test it on another website first


pjeaje comments:

OK i've tested it on both websites and updates work but creation doesn't


pjeaje comments:

pm'd


Francisco Javier Carazo Gil comments:

Ok, the hook is correct, but maybe the parameters are not. Try it before:


add_action( 'added_post_meta', 'do_after_post_meta', 10, 4 );
add_action( 'updated_post_meta', 'do_after_post_meta', 10, 4 );
function do_after_post_meta( $meta_id, $post_id, $meta_key, $meta_value )
{
if ( 'location' == $meta_key ) {
if( !empty ($meta_value) ):
$location_array = explode(",", $meta_value);
update_post_meta( $post_id, 'latitude', $location_array[0] );
update_post_meta( $post_id, 'longitude', $location_array[1] );
endif;
}
}


pjeaje comments:

Bingo! thanks


Francisco Javier Carazo Gil comments:

Wow :)

Thanks to you.

Vote if all is correct, if need anymore, tell me.


pjeaje comments:

Voted 20 to you. Thanks.