【问题标题】:Custom Meta Box Save_Post Callback not triggered in functions.php自定义元框 Save_Post 回调未在 functions.php 中触发
【发布时间】:2019-01-31 16:25:17
【问题描述】:

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

我正在运行 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  }

【问题讨论】:

    标签: php wordpress post save meta-boxes


    【解决方案1】:

    您必须使用$_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  }
    

    【讨论】:

    • 谢谢。这可能是问题之一,但仅使用 $_POST 并不能解决问题。事实证明, sanitize_text_field() 正在剥离值,因此没有保存任何内容 - 这是罪魁祸首。当我删除 sanitize_text_field 时,我现在可以保存这些值。现在,我可能需要通过自定义环境来运行它。
    • 我已经测试了我编写的代码,并且它完美地保存了数据,假设您的返回值只是数字 sanitize_text_field 应该可以正常工作。让我知道您期望什么数据,也许我可以为您提供更多帮助。
    • 文本应该只包含浮点数(负数或正数)。似乎 sanitize_text_field 实际上返回了一个空值。现在可以了。非常感谢!
    猜你喜欢
    • 2021-07-30
    • 1970-01-01
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多