0

I followed the instructions given in this blog to set the CMB2 plugin, but it is not working in my case.

theme-meta-function.php →

    <?php
/**
 * Include and set up custom metaboxes and fields. (Make sure you copy this file outside the CMB2 directory)
 *
 * Be sure to replace all instances of 'yourprefix_' with your project's prefix.
 * http://nacin.com/2010/05/11/in-wordpress-prefix-everything/
 *
 * @category YourThemeOrPlugin
 * @package  Demo_CMB2
 * @license  http://www.opensource.org/licenses/gpl-license.php GPL v2.0 (or later)
 * @link     https://github.com/WebDevStudios/CMB2
 */

/**
 * Get the bootstrap! If using the plugin from wordpress.org, REMOVE THIS!
 */



add_action( 'cmb2_admin_init', 'register_testimonial_metabox' );
/**
 * Hook in and add a testimonial metabox. Can only happen on the 'cmb2_admin_init' or 'cmb2_init' hook.
 */
function register_testimonial_metabox() {

 // Start with an underscore to hide fields from custom fields list
    $prefix = '_yourprefix_'; //note, you can use anything you'd like here

/**
 * Start field groups here
 */

// This first field group tells WordPress where to put the fields. In the example below, it is set to show up only on Post_ID=10

$cmb_demo = new_cmb2_box( array(
        'id'            => $prefix . 'metabox',
        'title'         => __( 'Homepage Custom Fields', 'cmb2' ),
        'object_types'  => array( 'page', ), // Post type
        'show_on'      => array( 'id' => array( 10, ) ), // Specific post IDs to display this metabox
        ) );


$cmb_demo->add_field( array(
    'name' => __( 'Testimonial Author', 'cmb2' ),
    'desc' => __( 'Who is the testimonial from', 'cmb2' ),
    'id'   => $prefix . 'author', //Note, I renamed this to be more appropriate
    'type' => 'textarea_small',
    ) );

$cmb_demo->add_field( array(
    'name'    => __( 'Testimonial', 'cmb2' ),
    'desc'    => __( 'add the testimonial here', 'cmb2' ),
    'id'      => $prefix . 'testimonial', //Note, I renamed this to be more appropriate
    'type'    => 'wysiwyg',
    'options' => array( 'textarea_rows' => 5, ),
    ) );

    $cmb_demo->add_field( array(
    'name' => __( 'Author Image', 'cmb2' ),
    'desc' => __( 'Upload an image or enter a URL.', 'cmb2' ),
    'id'   => $prefix . 'image', //Note, I renamed this to be more appropriate
    'type' => 'file',
    ) );
}

then I have included like this in the functions.php →

require_once( dirname(__FILE__) . '/inc/lib/theme-meta-functions.php');

But not a single meta is appearing the post → https://www.screencast.com/t/Bvihe62fZMV

I forget to mention that the plugin is already installed in the Wordpress.

1

Change $prefix = '_yourprefix_'; to $prefix = '_wp';

or add the code below directly to functions.php

add_action( 'cmb2_admin_init', 'register_testimonial_metabox' );

function register_testimonial_metabox() {

$prefix = '_wp'; 

$cmb_demo = new_cmb2_box( array(
'id'            => $prefix . 'metabox',
'title'         => __( 'Homepage Custom Fields', 'cmb2' ),
'object_types'  => array( 'page', ), // Post type
'show_on'      => array( 'id' => array( 10, ) ),
) );


$cmb_demo->add_field( array(
'name' => __( 'Testimonial Author', 'cmb2' ),
'desc' => __( 'Who is the testimonial from', 'cmb2' ),
'id'   => $prefix . 'author', 
) );

$cmb_demo->add_field( array(
'name'    => __( 'Testimonial', 'cmb2' ),
'desc'    => __( 'add the testimonial here', 'cmb2' ),
'id'      => $prefix . 'testimonial',
'type'    => 'wysiwyg',
'options' => array( 'textarea_rows' => 5, ),
) );

$cmb_demo->add_field( array(
'name' => __( 'Author Image', 'cmb2' ),
'desc' => __( 'Upload an image or enter a URL.', 'cmb2' ),
'id'   => $prefix . 'image', 
'type' => 'file',
) );
}
0

the solution →

'object_types'  => array( 'page', ), // Post type
        'show_on'      => array( 'id' => array( 10, ) ), // Specific post IDs to display this metabox

the above was the reason why it was not working on a post.

I just added post and added it here →

'object_types'  => array( 'page', ), // Post type

and deleted this line →

 'show_on'      => array( 'id' => array( 10, ) ), // Specific post 

It all started to work, and there was no issue in the plugin or any piece of code.

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.