WP Custom meta box for a specific page template - php

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
}

Related

Adding new Icons to Visual Composer

I am trying to add a new font icon set to Visual Composer and although the name is appearing in the dropdown; No dropdown for the actual font icons are being loaded and I cannot figure out what's missing or wrong with my code below.
Any help would be much appreciated.
// Add new custom font to Font Family selection in icon box module
function reach_add_new_icon_set_to_iconbox( ) {
$param = WPBMap::getParam( 'vc_icon', 'type' );
$param['value'][__( 'Reach Icons', 'reach-rdp' )] = 'reach_icons';
vc_update_shortcode_param( 'vc_icon', $param );
}
add_filter( 'init', 'reach_add_new_icon_set_to_iconbox', 40 );
function reach_add_font_picker() {
vc_add_param( 'vc_icon', array(
'type' => 'iconpicker',
'heading' => esc_html__( 'Icon', 'reach-rdp' ),
'param_name' => 'icons_reach_icons',
'settings' => array(
'emptyIcon' => false,
'type' => 'reach_icons',
'iconsPerPage' => 20,
),
'dependency' => array(
'element' => 'icon_type',
'value' => 'reach_icons',
),
'group' => esc_html__( 'Icon', 'reach-rdp' ),
)
);
}
add_filter( 'vc_after_init', 'reach_add_font_picker', 40 );
function reach_vc_iconpicker_type_reach_icons( $icons ) {
// Add custom icons to array
$icons['Reach Icons'] = array(
array( "icon-arrow-left" => "Arrow Left" ),
);
// Return icons
return $icons;
}
add_filter( 'vc_iconpicker-type-reach_icons', 'reach_vc_iconpicker_type_reach_icons' );
/**
* Register Backend and Frontend CSS Styles
*/
add_action( 'vc_base_register_front_css', 'leadinjection_vc_iconpicker_base_register_css' );
add_action( 'vc_base_register_admin_css', 'leadinjection_vc_iconpicker_base_register_css' );
function leadinjection_vc_iconpicker_base_register_css(){
wp_register_style('reach_icons', get_stylesheet_directory_uri() . '/assets/css/reach-font.css');
}
/**
* Enqueue Backend and Frontend CSS Styles
*/
add_action( 'vc_backend_editor_enqueue_js_css', 'leadinjection_vc_iconpicker_editor_jscss' );
add_action( 'vc_frontend_editor_enqueue_js_css', 'leadinjection_vc_iconpicker_editor_jscss' );
function leadinjection_vc_iconpicker_editor_jscss(){
wp_enqueue_style( 'reach_icons' );
}
It looks like you do the most things correct, you just need to replace:
add_filter('init....)
to:
add_action('vc_after_init',....)
update:
Also you have a wrong param name in dependency.
it should be:
'element' => 'type',
And I also will recommend to use the weight attribute to make sorting better:
function reach_add_new_icon_set_to_iconbox( ) {
$param = WPBMap::getParam( 'vc_icon', 'type' );
$param['value'][__( 'Reach Icons', 'reach-rdp' )] = 'reach_icons';
$param['weight'] = 90;
vc_update_shortcode_param( 'vc_icon', $param );
}
and
function reach_add_font_picker() {
vc_add_param( 'vc_icon', array(
'type' => 'iconpicker',
'heading' => esc_html__( 'Icon', 'reach-rdp' ),
'param_name' => 'icons_reach_icons',
'settings' => array(
'emptyIcon' => false,
'type' => 'reach_icons',
'iconsPerPage' => 20,
),
'weight' => 80,
'dependency' => array(
'element' => 'type',
'value' => 'reach_icons',
),
)
);
}
I needed to achieve exactly the same thing - although the icon set I'm adding is selection of icons from the Font Awesome Pro set. Using the answer that almost worked above, here is my fully working version. This is tested and working in version 5.5.2 of WPBakery. I hope this helps someone!
// In the 'Icon library' dropdown for an icon content type, add a new family of icons.
function fapro_add_to_iconbox() {
$param = WPBMap::getParam( 'vc_icon', 'type' );
$param['value'][ __( 'FontAwesome Pro icons', 'js_composer' ) ] = 'fapro';
vc_update_shortcode_param( 'vc_icon', $param );
}
add_filter( 'vc_after_init', 'fapro_add_to_iconbox', 40 );
// This adds a new parameter to the vc_icon content block.
// This parameter is an icon_picker element,
// that displays when fapro is picked from the dropdown.
function fapro_add_font_picker() {
$settings = array(
'type' => 'iconpicker',
'heading' => __( 'Icon', 'js_composer' ),
'param_name' => 'icon_fapro',
'settings' => array(
'emptyIcon' => false,
'type' => 'fapro',
'iconsPerPage' => 20,
),
'dependency' => array(
'element' => 'type',
'value' => 'fapro',
),
'weight' => '1',
'description' => __( 'Select icon from library.', 'js_composer' ),
);
vc_add_param( 'vc_icon', $settings );
}
add_filter( 'vc_after_init', 'fapro_add_font_picker', 50 );
// Add all the icons we want to display in our font family.
function vc_iconpicker_type_fapro( $icons ) {
// Add custom icons to array.
$fapro_icons = array(
array( 'far fa-bacon' => 'Bacon' ),
array( 'fas fa-hamburger' => 'Hamburger' ),
);
// Return icons.
return $fapro_icons;
}
add_filter( 'vc_iconpicker-type-fapro', 'vc_iconpicker_type_fapro' );
// Enqueue the CSS file so that the icons display in the backend editor.
function enqueue_fapro_font() {
wp_enqueue_style( 'agilechilli-font-awesome', 'https://pro.fontawesome.com/releases/v5.7.2/css/all.css' );
}
add_action( 'vc_backend_editor_enqueue_js_css', 'enqueue_fapro_font' );

taxonomy_radio is not showing anything in the front-end in cmb2

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

How to include excerpt option on taxonomy category page?

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

Wordpress - How do I use the media library for customization options?

I'm using Underscores to build a Wordpress theme and I've added an image selector to the customization section, which appears just like the default Background Image section, except clicking 'Select Image' doesn't bring up the media library like I hoped it would. Here's the code I'm using:
function hi_customization_options( $wp_customize ) {
$wp_customize->add_section(
'landing_page_image',
array(
'title' => 'Landing Page Image',
'priority' => 35,
)
);
$wp_customize->add_setting(
'lp-image_selector',
array(
'default' => '',
)
);
$wp_customize->add_control(
'lp-image_selector',
array(
'label' => 'Landing Page Image',
'section' => 'landing_page_image',
'type' => 'image',
)
);
}
add_action( 'customize_register', 'hi_customization_options' );
I think I need to add a 'choices' array to the add_control section, but how do I use that to target the media library?
Thanks
I figured this out. For anyone facing the same issue, I ended up using this code instead:
function hi_customization_options( $wp_customize ) {
$wp_customize->add_section(
'landing_page_image',
array(
'title' => 'Landing Page Image',
'priority' => 35,
)
);
$wp_customize->add_setting(
'lp-image_selector',
array(
'default' => '',
)
);
$wp_customize->add_setting( 'img-upload' );
$wp_customize->add_control(
new WP_Customize_Image_Control(
$wp_customize,
'lp-image_selector',
array(
'label' => 'Landing Page Image',
'section' => 'landing_page_image',
'settings' => 'img-upload'
)
)
);
}
add_action( 'customize_register', 'hi_customization_options' );

Shortcodes are not working in custom metabox

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.

Categories