I have custom metabox checkbox on CPT in my plugin. I want to fire an action and save new option when I save this post. But no matter how many times I tried it's saving option value only on second save. First post save - nothing happens, second post save - the value is saved. What's wrong with this code?
Here's my code:
add_action( 'save_post_theme_template', 'clean_deactivated_theme_templates_post_meta', 10, 2 );
function clean_deactivated_theme_templates_post_meta( $post_id, $post ) {
$location = get_post_meta( $post_id, '_est_template_location', true );
if ( 'header' === $location ) {
update_option( '_est_cpt_save_', 'header', 'yes' );
} else {
update_option( '_est_cpt_save_', 'other', 'yes' );
}
}
I have custom metabox checkbox on CPT in my plugin. I want to fire an action and save new option when I save this post. But no matter how many times I tried it's saving option value only on second save. First post save - nothing happens, second post save - the value is saved. What's wrong with this code?
Here's my code:
add_action( 'save_post_theme_template', 'clean_deactivated_theme_templates_post_meta', 10, 2 );
function clean_deactivated_theme_templates_post_meta( $post_id, $post ) {
$location = get_post_meta( $post_id, '_est_template_location', true );
if ( 'header' === $location ) {
update_option( '_est_cpt_save_', 'header', 'yes' );
} else {
update_option( '_est_cpt_save_', 'other', 'yes' );
}
}
Share
Improve this question
asked May 1, 2020 at 11:58
AlexanderAlexander
1531 gold badge2 silver badges12 bronze badges
7
|
Show 2 more comments
1 Answer
Reset to default 1I think you'd have to move the check for whether or not the $location === header
into the metabox then use the following to save the meta data.
Like this:
//In your metabox before the fields
$location = get_post_meta( $post->ID, '_est_template_location', true );
if( empty( $location ) || $location != 'header' ) :
$location = 'other';
endif;
Then, when saving, you've already addressed everything else and it's a straightforward saving process.
add_action( 'save_post', 'clean_deactivated_theme_templates_post_meta', 1, 2 );
function clean_deactivated_theme_templates_post_meta( $post_id, $post ) {
if( !current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
if( !isset( $_POST['theme_template_fields'] ) || !wp_verify_nonce( $_POST['theme_template_fields'], basename( __FILE__ ) ) ) {
return $post_id;
}
$theme_template_meta['_est_template_location'] = esc_textarea( $_POST['_est_template_location'] );
foreach( $theme_template_meta as $key => $value ) :
if( 'revision' === $post->post_type ) {
return;
}
if( get_post_meta( $post_id, $key, false ) ) {
update_post_meta( $post_id, $key, $value );
} else {
add_post_meta( $post_id, $key, $value);
}
if( !$value ) {
delete_post_meta( $post_id, $key );
}
endforeach;
}
save_post_theme_template()
function? Is there any documentation you read up on about it somewhere that you can share? I've never seen it and a google search doesn't return any results. – Tony Djukic Commented May 1, 2020 at 14:58theme_template
? If that's the case, I believe you should be usingadd_post_meta
&update_post_meta
rather thanupdate_options
. – Tony Djukic Commented May 1, 2020 at 15:41update_post_meta()
either... do you have the code with that attempt? – Tony Djukic Commented May 1, 2020 at 16:02