在function.php中未触发自定义元框Save_Post回调

时间:2019-01-31 16:25:18

标签: php wordpress post save meta-boxes

child主题,functions.php,我正在创建一个metabox。我在自定义metabox中有两个文本字段。每当用户从管理员添加帖子中单击“更新”或“发布”时,我都需要保存字段。允许此meta框出现在盟友类型的帖子中,包括自定义帖子。

我正在运行PHP7.3,运行wordpress 5.3

我试图: 1)使用其他钩子,例如save_edit_post,admin_init,publish_post等。

2)为add_Action提供优先级值

3)检查了HTTP发布请求,以确认是否传递了元键值对-是的。

4)使用get_post_meta()和$ POST全局变量来测试值是否通过-但无法验证。

function add_taddressbox_address_meta_box() {
    add_meta_box(
        'taddressbox_address_meta_box', // $id
        'taddressbox Address', // $title
        'show_taddressbox_address_meta_box', // $callback
        get_current_screen(), // $screen
        'normal', // $context
        'high' // $priority
    );
}

function taddressbox_address_save_postdata($post_id, $post, $update)
{
    //if this is post revision, then bail
   if (wp_is_post_revision( $post_id))
   {
       return;
   }

    // if our current user can't edit this post, bail  
    if( !current_user_can( 'edit_post' ) ) return;  


  $lat_val = sanitize_text_field(get_post_meta($post_id, '_taddressbox_lng', true));
    //get_post_meta($post->ID, 'taddressbox_lat', true);
    $lng_val = sanitize_text_field(get_post_meta($post_id, '_taddressbox_lng', true));
    update_post_meta($post_id, '_taddressbox_lat', $lat_val);   
    update_post_meta($post_id, '_taddressbox_lng', $lng_val);


}



function show_taddressbox_address_meta_box() {
    global $post;  

//   $values = get_post_custom( $post->ID );
    $lat = isset( $values['taddressbox_lat'] ) ? trim(esc_attr( $values['taddressbox_lat'][0] )) : '30'; 
    $lng = isset( $values['taddressbox_lng'] ) ? trim(esc_attr( $values['taddressbox_lng'][0] )) : '69';

//$lat = '30';
//$lng ='69';
    //  $meta = get_post_meta( $post->ID, 'taddressbox_address', true );
        ?>

    <input type="hidden" name="taddressbox_address_box_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>">

    <!-- All fields will go here -->

    <div id="map" tabindex="0" style="position: relative;height:400px;margin:0; padding:0; display: block;"></div>
    <div id="taddressbox_latlng">
     <label for"latitude">Latitude</label> <input type="text" id="taddressbox_lat" name="taddressbox_lat" value="<?php echo $lat; ?>">
      <label for"longitude">Longitude</label> <input type="text" id="taddressbox_lng" name ="taddressbox_lng" value="<?php echo $lng; ?>">
    </div>

<script>


<?php if ( trim($lat) == '' || trim($lng) =='' ) { ?> InitializetaddressboxMap();
<?php  } else {  ?>
InitializetaddressboxMap(<?php echo $lat; ?>,<?php echo $lng; ?>);    
<?php } ?>
</script>
    <?php  }

1 个答案:

答案 0 :(得分:0)

您必须使用$_POST[]来捕获并保存表单提交的代码。我已经固定的代码,随意使用它。

add_action( 'add_meta_boxes', 'add_taddressbox_address_meta_box' );

    function add_taddressbox_address_meta_box() {
        add_meta_box(
            'taddressbox_address_meta_box', // $id
            'taddressbox Address', // $title
            'show_taddressbox_address_meta_box', // $callback
            get_current_screen(), // $screen
            'normal', // $context
            'high' // $priority
        );
    }


    add_action( 'save_post', 'taddressbox_address_save_postdata' );
    function taddressbox_address_save_postdata( $post_id )
    {
        //if this is post revision, then bail
       if (wp_is_post_revision( $post_id))
       {
           return;
       }

        // if our current user can't edit this post, bail  
        if( !current_user_can( 'edit_post' ) ) return;  

        if ( ! isset( $_POST['my_lat_lang_box_nonce'] ) ) {
            return $post_id;
        }
        $nonce = $_POST['my_lat_lang_box_nonce'];

        // Verify that the nonce is valid.
        if ( ! wp_verify_nonce( $nonce, 'my_lat_lang_box' ) ) {
            return $post_id;
        }

        $lat_val = sanitize_text_field($_POST['taddressbox_lat']);
        $lng_val = sanitize_text_field($_POST['taddressbox_lng']);

        update_post_meta($post_id, '_taddressbox_lat', $lat_val);   
        update_post_meta($post_id, '_taddressbox_lng', $lng_val);


    }



    function show_taddressbox_address_meta_box( $post ) {

        $lat = get_post_meta( $post->ID, '_taddressbox_lat', true );
        $lng = get_post_meta( $post->ID, '_taddressbox_lng', true );


        wp_nonce_field( 'my_lat_lang_box', 'my_lat_lang_box_nonce' );

         ?>


        <!-- All fields will go here -->

        <div id="map" tabindex="0" style="position: relative;height:400px;margin:0; padding:0; display: block;"></div>
        <div id="taddressbox_latlng">
         <label for"latitude">Latitude</label> <input type="text" id="taddressbox_lat" name="taddressbox_lat" value="<?php echo $lat; ?>">
          <label for"longitude">Longitude</label> <input type="text" id="taddressbox_lng" name ="taddressbox_lng" value="<?php echo $lng; ?>">
        </div>

    <script>


    <?php if ( trim($lat) == '' || trim($lng) =='' ) { ?> InitializetaddressboxMap();
    <?php  } else {  ?>
    InitializetaddressboxMap(<?php echo $lat; ?>,<?php echo $lng; ?>);    
    <?php } ?>
    </script>
        <?php  }