I am having issue with WordPress meta boxes. actually I am using WordPress Genesis framework and in child theme I had create few meta boxes for my client to show some content before the page content, but in the custom meta box i am using wp-editor and its working fine. but the issue is that when I try to use some shortcodes in this wp-editor then it won't show me anything, it is just returning the whole shortcode as it is.
I am using https://github.com/jaredatch/Custom-Metaboxes-and-Fields-for-WordPress for custom meta boxes.
And my codes are in function.php file:
/* -------------------------------------------------------------------------- */
/* Setup Custom metaboxes */
/* -------------------------------------------------------------------------- */
add_action( 'init', 'be_initialize_cmb_meta_boxes', 9999 );
function be_initialize_cmb_meta_boxes() {
if ( !class_exists( 'cmb_Meta_Box' ) ) {
require_once( CHILD_DIR . '/lib/metabox/init.php' );
}
}
add_filter( 'cmb_meta_boxes', 'cmb_sample_metaboxes' );
function cmb_sample_metaboxes( array $meta_boxes ) {
// Start with an underscore to hide fields from custom fields list
$prefix = '_cmb_';
$meta_boxes[] = array(
'id' => 'text_content',
'title' => 'Text Content',
'pages' => array( 'page', ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true, // Show field names on the left
'fields' => array(
array(
'name' => 'Custom Content',
'desc' => 'This is a title description',
'id' => $prefix . 'custom_content',
'type' => 'title',
),
array(
'name' => 'Tab Name',
'desc' => 'Please descibe the tab name (required)',
'id' => $prefix . 'tab_name',
'type' => 'text',
),
array(
'name' => 'Test wysiwyg',
'desc' => 'field description (optional)',
'id' => $prefix . 'test_wysiwyg',
'type' => 'wysiwyg',
'options' => array( 'textarea_rows' => 5, ),
),
),
);
return $meta_boxes;
}
I save the codes in page.php as:
add_action('genesis_before_loop', 'ehline_before_loop_content');
function ehline_before_loop_content()
{
echo genesis_get_custom_field( '_cmb_tab_name' );
echo '<br />';
echo genesis_get_custom_field( '_cmb_test_wysiwyg' );
}
genesis();
But when I use the shortcodes in this meta box it return something like that
[wptabtitle] Tab 01[/wptabtitle] [wptabcontent]test[/wptabcontent]
Please anyone tell me how can I make it to use shortcodes in the wp-editor.
You need to call do_shortcode() for the content of your custom fields. Here is how the updated code should look like:
add_action('genesis_before_loop', 'ehline_before_loop_content');
function ehline_before_loop_content()
{
echo do_shortcode( genesis_get_custom_field( '_cmb_tab_name' ) );
echo '<br />';
echo do_shortcode( genesis_get_custom_field( '_cmb_test_wysiwyg' ) );
}
genesis();
Also this will not add the auto-paragraphs that you would usually see for your posts contents. You can do two things:
echo apply_filters( 'the_content', genesis_get_custom_field( '_cmb_tab_name' ) );
or
echo wpautop( do_shortcode( genesis_get_custom_field( '_cmb_tab_name' ) ) );
In theory the first one should be better, but sometimes you might get additional output from functions that hook to the the_content filter.
Related
I am building a website using Wordpress in where I want to show the Metadata from the custom fields.
I have setup the cmb2 in my function.php as like below the codes..
add_action( 'cmb2_admin_init', 'cmb2_sample_metaboxes' );
function cmb2_sample_metaboxes() {
$cmb = new_cmb2_box( array(
'id' => 'test_metabox',
'title' => __( 'Test Metabox', 'cmb2' ),
'object_types' => array( 'page', ),
'context' => 'normal',
'priority' => 'high',
'show_names' => true,
) );
$cmb->add_field( array(
'name' => __( 'Test Text', 'cmb2' ),
'desc' => __( 'field description (optional)', 'cmb2' ),
'id' => 'yourprefix_text',
'type' => 'text',
'show_on_cb' => 'cmb2_hide_if_no_cats',
) );
}
Ok, that is working in the post section, That's working. But when I tried to show Metadata in the front-end using
<?php
$text = get_post_meta( get_the_ID(), '_yourprefix_text', true );
echo esc_html( $text );
?>
nothing is echoing.
Anyone please find out what the problems are.
Looks like you've just got a typo. The id does not match the meta_key: 'yourprefix_text' vs '_yourprefix_text'
Fixed:
<?php
$text = get_post_meta( get_the_ID(), 'yourprefix_text', true );
echo esc_html( $text );
?>
I am using penny auction wordpress theme, as i am new to wordpress. My question is to add more fields in side meta-box by using child theme, So for this I have successfully created and activated child theme, but I am stuck here to how can I add more text fields in meta box by using child theme functions file?
The easiest way to do that is to install the Meta Box plugin (https://wordpress.org/plugins/meta-box/), then add this to your child theme's functions.php file:
function yourprefix_register_meta_boxes( $meta_boxes ) {
$prefix = 'metaboxprefix_';
$meta_boxes[] = array(
'id' => 'samplefield',
'title' => __( 'Your Meta Box Title', 'yourtextdomain' ),
'post_types' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => __( 'Your Text Field', 'yourtextdomain' ),
'id' => $prefix . 'yourfieldid',
'type' => 'text',
),
)
);
return $meta_boxes;
}
add_filter( 'rwmb_meta_boxes', 'yourprefix_register_meta_boxes' );
For future reference: http://metabox.io/
I am building a website in where I want to show the tag selected from the custom fields (mainly from radio button). I have setup the cmb2 as like below the codes..
add_action('cmb2_admin_init', 'custom_metaboxes');
function custom_metaboxes() {
$metabox = new_cmb2_box( array(
'object_types' => array( 'post'), //for the post
'title' => 'Additional Fields',
'id' => 'additional'
)
);
// showing in the admin panel
$metabox -> add_field( array(
'name' => 'Taxonomy List',
'desc' => 'This get the list of taxonomy',
'id' => 'taxonomy_list',
'type' => 'taxonomy_radio',
'taxonomy' => 'post_tag',
'default' => 'ami'
)
);
}
Ok, that is working in the post section. My tags are shown in the radio buttons, That's working. But when I tried to show the the selected tag in the front-end using
echo get_post_meta( get_the_id(), 'taxonomy_list', true )// returns nothing
nothing is echoing. Then tried var_dump function it returns string(0) "". What are the problems are working behind the scene.
Anyone please find out what the problems are.
The problem can be solved by using cmb2-2.0.2 version. And here is the code:
<?php
add_action( 'cmb2_init', 'yourprefix_register_demo_metabox' );
function yourprefix_register_demo_metabox() {
$prefix = '_yourprefix_demo_';
$cmb_demo = new_cmb2_box( array(
'id' => $prefix . 'additional',
'title' => 'Additional Fields',
'object_types' => array('page')
) );
$cmb_demo->add_field( array(
'name' => 'Taxonomy List',
'desc' => 'This get the list of taxonomy',
'id' => $prefix . 'taxonomy_list',
'type' => 'taxonomy_radio',
'taxonomy' => 'post_tag'
) );
}
?>
And in frontend you need to write:
<?php
$prefix = '_yourprefix_demo_';
echo get_tag(get_post_meta(get_the_ID(), $prefix.'taxonomy_list', true)[0])->name;
?>
I am trying to add an excerpt option to my category page, to display instead of my description.
So basically, I need a box on this screen which will be able to be used as preview text.
The code used to create this taxonomy is:
add_action( 'init', 'create_product_cat_external' );
function create_product_cat_external() {
register_taxonomy(
'ExternalProducts',
'products',
array(
'label' => __( 'External Products' ),
'rewrite' => array( 'slug' => 'externalproducts' ),
'hierarchical' => true,
)
);
}
and the box needs to be here:
You can use CMB2 plugin and put this in your functions.php for example:
add_action( 'cmb2_admin_init', 'yourprefix_register_taxonomy_metabox' );
function yourprefix_register_taxonomy_metabox() {
$prefix = 'yourprefix_term_';
$cmb_term = new_cmb2_box( array(
'id' => $prefix . 'edit',
'object_types' => array( 'term' ), // Tells CMB2 to use term_meta vs post_meta
'taxonomies' => array( 'products'), // Tells CMB2 which taxonomies should have these fields
// 'new_term_section' => true, // Will display in the "Add New Category" section
) );
$cmb_term->add_field( array(
'name' => __('Excerpt', 'default'),
'id' => $prefix . 'excerpt',
'type' => 'wysiwyg',
'on_front' => false,
) );
}
I have created a custom meta box using option tree framework. It is showing for every page. I want that it will show only for a specific page. How can i do that?
I have written this code to functions.php file
add_action( 'admin_init', 'custom_meta_boxes' );
function custom_meta_boxes() {
$latest_work = array(
'id' => 'latest_work',
'title' => 'latest-work Meta Box',
'desc' => '',
'pages' => array( 'page' ),
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'label' => 'latest-work',
'id' => 'latest-work',
'type' => 'text',
'desc' => 'Tell about your latest work',
'std' => '',
'rows' => '',
'post_type' => '',
'taxonomy' => '',
'class' => ''
)
)
);
ot_register_meta_box( $latest_work );
}
Please tell me how can i do that?
add_action( 'admin_menu', 'remove_post_meta_boxes' );
function remove_post_meta_boxes() {
// lets assume blog page has the id 23
// let's remove the meta box from blog
if( isset($_GET['post']) && $_GET['post'] == 23 ){
remove_meta_box( 'latest_work', 'page', 'normal' );
}
}
take a look for more information remove_meta_box()
Use the following.
$post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
// checks for post/page ID for example i have added 7
if ($post_id == '7'){
/.add_meta box code inside
}