最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

metabox - save_post_{$post->post_type} action firing on second save

programmeradmin3浏览0评论

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
  • 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. – Tony Djukic Commented May 1, 2020 at 14:58
  • theme_template is the dymanic part of WordPress hook, you can read about it here - developer.wordpress/reference/hooks/… It fires every time you save your custom post. – Alexander Commented 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. – Tony Djukic Commented 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 Commented 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? – Tony Djukic Commented May 1, 2020 at 16:02
 |  Show 2 more comments

1 Answer 1

Reset to default 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;
}
发布评论

评论列表(0)

  1. 暂无评论