0
votes

I've developed Meta Box plugin in a Custom Post as per this playlist. (I made some custermizations to that) In here he created a custom post using the plugin and do all the metabox operation on that relevant post.

It works fine. But my requirement is to display this meta box in a page.

Here is my code.

metabox.php (plugin code)

<?php

function wpl_owt_custom_init_cpt(){
    $args = array(
        'public' => true,
        'label' => 'Books',
        'supports' => array('title' , 'editor' , 'author' , 'thumbnail' , 'excerpt' , 'comments')
    );
    register_post_type('book' , $args);
   }
   add_action('init', 'wpl_owt_custom_init_cpt');



 function wpl_owt_register_metabox_cpt(){
        //custom post type
        add_meta_box("owt-cpt-id" , "Contact Details" , "wpl_owt_book_function" , "book" , "normal" , "high");
   }
   add_action("add_meta_boxes_book" , "wpl_owt_register_metabox_cpt");


   /**********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);
}


?>

Can someone give me a solution to display the metabox in a page instead of a post.

2
It's not really clear what you want. What do you mean with "wordpress Contact Us page" is that a plugin or something?jrswgtr
I created a plugin to add metabox into post. But mytech lead wants to add it in to a specific page.Private the Penguin
I see, but you talk about "wordpress Contact Us page" as if we all supposed to know what that is. So you want to register the metabox to a page? Then replace "book" by "page" add_meta_box("owt-cpt-id" , "Contact Details" , "wpl_owt_book_function" , "page" , "normal" , "high"); You won't (easily) be able to register it only to one specific page.jrswgtr

2 Answers

0
votes

please have a look as below:

<?php

function add_custom_meta_box()
{
    $screens = ['page'];
    foreach ($screens as $screen) {
        add_meta_box(
            'meta_box_id',           // Unique ID
            'Custom Meta Box Title',  // Box title
            'callback_function',  // Content callback, must be of type callable
            $screen                   // Post type
        );
    }
}
add_action('add_meta_boxes', 'add_custom_meta_box');

function callback_function($post)
{
   /*Do something*/
}

I hope it would help you out

0
votes

Replace

function wpl_owt_custom_init_cpt(){
    $args = array(
        'public' => true,
        'label' => 'Books',
        'supports' => array('title' , 'editor' , 'author' , 'thumbnail' , 'excerpt' , 'comments')
    );
    register_post_type('book' , $args);
   }
   add_action('init', 'wpl_owt_custom_init_cpt');

with this

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
        }
    }
}

and replace this

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

with this.

$post_slug = "page";
    if($post_slug != $post->post_type){
        return;
    }

Hope this will work out.