不保存在元框中输入的值

时间:2019-12-31 作者:Private the Penguin

我开发了一个自定义metabox插件,并将其链接到联系我们页面,以便将电话号码、电子邮件和工作时间作为用户输入保存到DB。

我输入的thing值没有保存到DB中。

请调查此事并帮助我找到解决方案。

metabox.php - 插件

<?php

add_action(\'add_meta_boxes\', \'wpl_owt_register_metabox_cpt\');
function wpl_owt_register_metabox_cpt()
{
    global $post;

    if(!empty($post))
    {
        $pageTemplate = get_post_meta($post->ID, \'_wp_page_template\', true);

        if($pageTemplate == \'page-contact.php\' )
        {
            add_meta_box(
                \'owt-cpt-id\', // $id
                \'Contact Details\', // $title
                \'wpl_owt_book_function\', // $callback
                \'page\', // $page
                \'normal\', // $context
                \'high\'); // $priority
        }
    }
}


   /**********Callback function for metabox at custom post type book******************/

 function wpl_owt_book_function( $post ) {
    //echo "<p>Custom metabox for custom post type</p>";

    define("_FILE_", "_FILE_");

    wp_nonce_field( basename(_FILE_), "wp_owt_cpt_nonce");

    echo "<label for=\'txtPhoneNum\'>Phone</label><br>";
    $phone_num = get_post_meta($post->ID, "telNo" , true);
    echo "<input type =\'tel\' name = \'txtPhoneNum\' value = \'" . $phone_num . "\'\' placeholder = \'Phone Number\' /><br><br>";

    echo "<label for=\'txtEmail\'>Email</label><br>";
    $email = get_post_meta($post->ID, "email" , true);
    echo "<input type =\'email\' name = \'txtEmail\' value = \'" . $email . "\'\' placeholder = \'Email Address\' /><br><br>";

    echo "<label for=\'txtHours\'>Hours of Operation</label><br>";
    $hours = get_post_meta($post->ID, "hourofOps" , true);
    echo "<input type =\'text\' name = \'txtHours\' value = \'" . $hours . "\'\' placeholder = \'Working Hours\' /><br><br>";
}

add_action("save_post" , "wpl_owt_save_metabox_data" , 10 , 2);

function wpl_owt_save_metabox_data($post_id, $post){

    //verify nonce
    if(!isset($_POST[\'wp_owt_cpt_nonce\']) || !wp_verify_nonce($_POST[\'wp_owt_cpt_nonce\'], basename(_FILE_))){
        return $post_id;
    }

    //verify slug value
    $post_slug = "book";
    if($post_slug != $post->post_type){
        return;
    }

    //save value to db filed
    $pub_tel = \'\';
    if(isset($_POST[\'txtPhoneNum\'])){
        $pub_tel = sanitize_text_field($_POST[\'txtPhoneNum\']);
    }

    else{
        $pub_tel = \'\';
    }

    update_post_meta($post_id, "telNo", $pub_tel);


    $pub_email = \'\';

    if(isset($_POST[\'txtEmail\'])){
        $pub_email = sanitize_text_field($_POST[\'txtEmail\']);
    }

    else{
        $pub_email = \'\';
    }

    update_post_meta($post_id, "email", $pub_email);


    $pub_hours = \'\';

    if(isset($_POST[\'txtHours\'])){
        $pub_hours = sanitize_text_field($_POST[\'txtHours\']);
    }
    update_post_meta($post_id, "hourofOps", $pub_hours);
}

?>
谢谢大家!

1 个回复
SO网友:Chetan Vaghela

您必须删除if($post_slug != $post->post_type){} 从保存功能或更改$post_slug = "book";$post_slug = "page";. 因为它检查$post slug是book 或者不是。当前职位类型为page 联系我们页面。您已设置为book

add_action("save_post" , "wpl_owt_save_metabox_data" , 10 , 2);

function wpl_owt_save_metabox_data($post_id, $post){

    //verify nonce
    if(!isset($_POST[\'wp_owt_cpt_nonce\']) || !wp_verify_nonce($_POST[\'wp_owt_cpt_nonce\'], basename(_FILE_))){
        return $post_id;
    }

    //save value to db filed
    $pub_tel = \'\';
    if(isset($_POST[\'txtPhoneNum\'])){
        $pub_tel = sanitize_text_field($_POST[\'txtPhoneNum\']);
    }
    else{
        $pub_tel = \'\';
    }

    update_post_meta($post_id, "telNo", $pub_tel);


    $pub_email = \'\';

    if(isset($_POST[\'txtEmail\'])){
        $pub_email = sanitize_text_field($_POST[\'txtEmail\']);
    }
    else{
        $pub_email = \'\';
    }

    update_post_meta($post_id, "email", $pub_email);


    $pub_hours = \'\';

    if(isset($_POST[\'txtHours\'])){
        $pub_hours = sanitize_text_field($_POST[\'txtHours\']);
    }
    update_post_meta($post_id, "hourofOps", $pub_hours);
}

相关推荐

如何判断自定义域是否由ACF、Metabox.io、Carbon Fields、Customed Build等制成?

我正在开发一个将使用自定义字段的插件,但我很好奇是否有办法知道是哪个插件/库创建了这些自定义字段。我之所以试图找到这种区别,是因为我可以在获取这些字段的数据时使用正确的函数,所以对于ACF,我会使用get_field() 对于Metabox。io我会使用rwmb_meta 如果是定制的,我会使用get_post_meta(). 我在我的数据库中查找,看看是否能找到每个自定义字段的独特之处,但什么都没有跳出来。有没有办法告诉我们哪个插件/库创建了一个特定的自定义字段?