0

I developed a custom metabox plugin and linked it to contact us page in order to save to DB the phone number, email and working hours as user input.

The thing values I entered are not save into DB.

Kindly look into this matter and help me to find a solution.

metabox.php - plugin

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

?>

Thank You!

1

You have to remove if($post_slug != $post->post_type){} from save function or change $post_slug = "book"; to $post_slug = "page";. because it check $post slug is book or not. currently post type is page for contact us page. and you have set as 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);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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