1

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' );          
  }

}
7
  • Where did you find the 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. May 1, 2020 at 14:58
  • theme_template is the dymanic part of WordPress hook, you can read about it here - developer.wordpress.org/reference/hooks/… It fires every time you save your custom post.
    – Alexander
    May 1, 2020 at 15:36
  • So is your CPT named theme_template? If that's the case, I believe you should be using add_post_meta & update_post_meta rather than update_options. May 1, 2020 at 15:41
  • yep, that's correct. I'm just just testing it with option table. But it doesn't make any difference.
    – Alexander
    May 1, 2020 at 15:54
  • So it's not working with the update_post_meta() either... do you have the code with that attempt? May 1, 2020 at 16:02

1 Answer 1

1

I 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;
}
1
  • Unfortunately my metabox code is more complex than this. What I've discovered is that I can't retrieve metadata upon post save even with basic metabox code. So I've decided to switch to Metabox plugin, which works perfectly.
    – Alexander
    May 3, 2020 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.