Meta box tabs - in a custom post type - wordpress

Is it possible to insert meta box tabs in a specific custom post type.
I've been looking at the example :
add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) {
// 1st Meta Box
$meta_boxes[] = array(
'title' => 'Meta Box Tabs Demo',
// List of tabs, in one of the following formats:
// 1) key => label
// 2) key => array( 'label' => Tab label, 'icon' => Tab icon )
'tabs' => array(
'contact' => array(
'label' => 'Contact',
'icon' => 'dashicons-email', // Dashicon
),
'social' => array(
'label' => 'Social Media',
'icon' => 'dashicons-share', // Dashicon
),
'note' => array(
'label' => 'Note',
'icon' => 'https://i.imgur.com/nJtag1q.png', // Custom icon, using image
),
),
// Tab style: 'default', 'box' or 'left'. Optional
'tab_style' => 'default',
// Show meta box wrapper around tabs? true (default) or false. Optional
'tab_wrapper' => true,
'fields' => array(
array(
'name' => 'Name',
'id' => 'name',
'type' => 'text',
// Which tab this field belongs to? Put tab key here
'tab' => 'contact',
),
array(
'name' => 'Email',
'id' => 'email',
'type' => 'email',
'tab' => 'contact',
),
array(
'name' => 'Facebook',
'id' => 'facebook',
'type' => 'text',
'tab' => 'social',
),
array(
'name' => 'Note',
'id' => 'note',
'type' => 'textarea',
'tab' => 'note',
),
),
);
This will work in post if added to functions.php ... but if we try to use it in a custom post type the metabox will appear but the fields will not.
Any help would be appreciated.

Related

WORDPRESS Missing label in multicolor Kirki field

In multicolor field, labels from colour pickers are missing. Code:
Kirki::add_field( $textdomain, [
'type' => 'multicolor',
'settings' => 'multicolor_setting',
'label' => esc_html__( 'Label', $textdomain ),
'section' => 'header__top_bar__general',
'priority' => 10,
'choices' => [
'link' => esc_html__( 'Color', $textdomain ),
'hover' => esc_html__( 'Hover', $textdomain ),
'active' => esc_html__( 'Active', $textdomain ),
],
'default' => [
'link' => '#0088cc',
'hover' => '#00aaff',
'active' => '#00ffff',
],
] );
result:
Console log:
wpColorPickerL10n.defaultLabel is deprecated since version 5.5.0! Use wp.i18n instead.
wpColorPickerL10n.pick is deprecated since version 5.5.0! Use wp.i18n instead.
wpColorPickerL10n.defaultString is deprecated since version 5.5.0! Use wp.i18n instead.
wpColorPickerL10n.defaultAriaLabel is deprecated since version 5.5.0! Use wp.i18n instead.
common.min.js?ver=5.7.1:2
WP 5.7.1, Kirki 3.1.6 from repository, theme_mod
any way to fix quick fix that?
You might be a bit confused here. Why are you using textdomain both as the text domain for the translation AND as the first arguments for add_field? The first argument to the add_field method should be an existing config. As per the documentation:
Kirki::add_config( 'theme_config_id', array(
'capability' => 'edit_theme_options',
'option_type' => 'theme_mod',
) );
Kirki::add_panel( 'panel_id', array(
'priority' => 10,
'title' => esc_html__( 'My Panel', 'kirki' ),
'description' => esc_html__( 'My panel description', 'kirki' ),
) );
Kirki::add_section( 'section_id', array(
'title' => esc_html__( 'My Section', 'kirki' ),
'description' => esc_html__( 'My section description.', 'kirki' ),
'panel' => 'panel_id',
'priority' => 160,
) );
Kirki::add_field( 'theme_config_id', [
'type' => 'multicolor',
'settings' => 'multicolor_setting',
'label' => esc_html__( 'Label', 'kirki' ),
'section' => 'section_id',
'priority' => 10,
'choices' => [
'link' => esc_html__( 'Color', 'kirki' ),
'hover' => esc_html__( 'Hover', 'kirki' ),
'active' => esc_html__( 'Active', 'kirki' ),
],
'default' => [
'link' => '#0088cc',
'hover' => '#00aaff',
'active' => '#00ffff',
],
] );
Here, kirki is an existing text domain. Your text domain seems not to exist. Try removing the text domains. Like this:
$kirki_config_id = '{YOUR KIRKI CONFIG ID}';
Kirki::add_field( $kirki_config_id, [
'type' => 'multicolor',
'settings' => 'multicolor_setting',
'label' => 'Label',
'section' => 'header__top_bar__general',
'priority' => 10,
'choices' => [
'link' => 'Color',
'hover' => 'Hover',
'active' => 'Active',
],
'default' => [
'link' => '#0088cc',
'hover' => '#00aaff',
'active' => '#00ffff',
],
] );
Does that do the trick?

Wordpress add Shortcut link to Enable/Disable plugin in admin bar

I would like to add a link to enable and disable a plugin in the admin bar.
Below the code, I wanted to know if it is the correct method.
thank you
function custom_admin_bar_link( $admin_bar ) {
$admin_bar->add_menu( array(
'id' => 'wp-custom-link',
'title' => 'Custom Item',
'href' => '',
'meta' => array(
'title' => __('Custom'),
),
));
$admin_bar->add_menu( array(
'id' => 'disable-link',
'parent'=> 'wp-custom-link',
'title' => 'Disable Amazon',
'href' => 'https://mydisablelink.com/',
'meta' => array(
'title' => __('Disable Amazon'),
),
));
$admin_bar->add_menu( array(
'id' => 'unable-link',
'parent'=> 'wp-custom-link',
'title' => 'Enable Amazon',
'href' => 'https://mydisablelink.com/',
'meta' => array(
'title' => __('Enable Amazon'),
),
));
}
add_action( 'admin_bar_menu', 'custom_admin_bar_link', 100 );

Wordpress How Can I Display skill1_logo in View

Hi Im practically a newbie and would like to kindly ask how can I display skill1_logo,skill1_title,skill1_description on a page that I want. I was trying to do a 1 page website using wordpress to practice my wordpress.
function theme_skills_customizer($wp_customize){
//adding section in wordpress customizer
$wp_customize->add_section('skills_settings_section', array(
'title' => 'Skills Section'
));
$wp_customize->add_setting('skill1_logo');
$wp_customize->add_setting('skill1_title');
$wp_customize->add_setting('skill1_description');
$wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'skill1_logo', array(
'label' => __( 'Skill 1 Logo', 'themeslug' ),
'width' => 400,
'height' => 400,
'flex-width' => true,
'flex-height' => true,
'section' => 'skills_settings_section',
'settings' => 'skill1_logo',
)));
$wp_customize->add_control( 'skill1_title', array(
'label' => __( 'Skill 1 Title' ),
'section' => 'skills_settings_section', // Add a default or your own section
'type' => 'text',
));
$wp_customize->add_control( 'skill1_description', array(
'label' => __( 'Skill 1 Description' ),
'section' => 'skills_settings_section', // Add a default or your own section
'type' => 'textarea',
));
}
add_action( 'customize_register', 'theme_skills_customizer' );
You can use get_theme_mod function to echo those settings.
e.g.
$s_title = get_theme_mod('skill1_title');

WordPress admin: How can I display existing admin sub-menu based on user's role

I require to show an existing sub-menu item of admin menu based on a specific user role. The role is general-travel-editor.
The actual menu layout is like this
Travel Settings
- General settings
- Advanced Settings
The above user role is supposed access only the "General Settings" sub-menu. So I want to display the menu for this type users as:
Travel Settings
- General settings
I tried add_menu_page( 'Travel Settings', 'Travel Settings', 'general-travel-editor', 'general-travel-editor'); but I don't have the option to add the URL of this page.
I am using a VIP plugin.
I need to deliver this by tonight but got stuck!
Somebody please advise how I can do this.
UPDATE
User role and capabilities defined in functions.php
function general_travel_user_capabilities() {
$travel_option_caps = array(
'publish_travel-options' => true,
'edit_travel-options' => true,
'edit_others_travel-options' => true,
'edit_travel-option' => true,
'read_travel-option' => true,
'delete_travel-options' => true,
'delete_travel-option' => true,
'read' => true
);
wpcom_vip_duplicate_role('subscriber', 'travel-option-editor', 'Travel Option Editor', $travel_option_caps);
$travel_option_caps = array(
'publish_travel-options',
'edit_travel-options',
'edit_others_travel-options',
'delete_travel-options',
'delete_others_travel-options',
'read_private_travel-options',
'edit_travel-option',
'delete_travel-option',
'read_travel-option'
);
wpcom_vip_add_role_caps('editor', $travel_option_caps);
wpcom_vip_add_role_caps('administrator', $travel_option_caps);
}
add_action('init', 'general_travel_user_capabilities');
Travel Settings:
function travel_setting_pages_init() {
$travel_settings = travel_create_settings_page(
'travel_settings_page',
'Global Travel Settings',
array(
'parent' => false,
'title' => 'Travel Settings',
'position' => '59.4815152342',
),
array(
'travel_theme_options' => array(
'title' => '',
'description' => 'Settings that affect all pages of the site',
'fields' => array(
'mytravel_root_url' => array(
'type' => 'url',
'label' => 'MyTravel Root URL',
'description' => 'The base url for all MyTravel App links',
'default' => 'http://myurl.com',
),
'travel_phone' => array(
'type' => 'text',
'label' => 'Support Phone Number U.S.',
'description' => 'In top left of all pages',
'default' => '800-610-6500',
),
'travel_phone_canada' => array(
'type' => 'text',
'label' => 'Support Phone Number Canada',
'description' => 'In top left of all pages',
'default' => '877-330-3321',
),
'travel_weather_text' => array(
'type' => 'text',
'label' => 'Alert Text',
'description' => 'Text for the alert on the home page. Required for the alert to be displayed.',
'default' => '',
),
'travel_weather_link' => array(
'type' => 'url',
'label' => 'Alert Link',
'description' => 'Link for the alert on the home page. Required for the alert to be displayed.',
'default' => '',
),
'travel_weather_start_date' => array(
'type' => 'date',
'label' => 'Alert Start Date',
'description' => 'Start date to display the alert on the home page. Optional.',
'default' => '',
),
'travel_weather_end_date' => array(
'type' => 'date',
'label' => 'Alert end Date',
'description' => 'End date to display the alert on the home page. Optional.',
'default' => '',
)
)
)
)
);
// Home Page Settings Sub-Page
$slide_color_options = array(
'orange-grunge' => 'Orange Grunge Pattern',
'orange-img1' => 'Orange Image: Truck Engine with Person',
'orange-img2' => 'Orange Image: Truck Top with Person',
'orange-img3' => 'Orange Image: Forklift',
'orange-trailers' => 'Orange Image: Truck Trailers',
'black-trails' => 'Black Image: Light Trails',
'dark-grey' => 'Grey Image: Hand',
'white' => 'White Image: Trucks in Dock',
'orange' => 'Orange (To be removed)',
'blue' => 'Blue (To be removed)',
'black' => 'Black (To be removed)'
);
$travel_home_settings = travel_create_settings_page(
'travel_home_settings_page',
'Home Page Settings',
array(
'parent' => 'travel_settings_page',
'title' => 'Home Page Settings',
),
array(
'travel_home_slideshow_settings' => array(
'title' => 'Home Slideshow Settings',
'description' => 'If image is not set, slide will not be shown.',
'fields' => array(
// Slide 1
'slide_color_1' => array(
'type' => 'radio',
'label' => 'Slide 1 Background Color',
'default' => 'orange',
'options' => $slide_color_options
),
'slide_image_1' => array(
'type' => 'media',
'label' => 'Slide 1 Image',
'description' => 'Transparent PNG # 911x375. See travel-home-marquee-template.psd',
),
'slide_heading_1' => array(
'type' => 'textarea',
'label' => 'Slide 1 Heading',
),
'slide_sub_heading_1' => array(
'type' => 'wysiwyg',
'label' => 'Slide 1 Sub-Heading',
),
)
),
'travel_home_mid_cta_settings' => array(
'title' => 'Middle CTA Settings',
'fields' => array(
'mid_cta_heading_1_1' => array(
'type' => 'text',
'label' => 'Middle CTA 1 - First Line',
),
)
),
'travel_home_lower_cta_settings' => array(
// first block
'title' => 'Lower CTA Settings',
'fields' => array(
'lower_cta_image_1' => array(
'type' => 'media',
'label' => 'Lower CTA 1 - Header Image',
'description' => 'Transparent PNG # 193x71. If set, overrides main heading text.',
),
)
)
)
);
}
add_action( 'init', 'travel_setting_pages_init' );
travel-option is a Custom Post Type so I could use it in caps array. When logging in with the said user role I can see the CPT menus are coming up nicely but how can I add this settings menu? I apologize for posting such a long code!
I will leave another answer here for anyone facing the same situation.
For me, I was creating a plugin and wanted to give it a role different than (Administrator)
I searched for the already existing wp roles and chose "Author" role. Roles are mentioned in this link
Here's how I added my menu =>
add_menu_page( 'Page Title', 'Menu title', 'upload_files', 'url slug', 'function name to be executed', $logo , 1 );
So you just have to replace the 'upload_files' with any other capability matching the selected role.
The add_submenu_page function - which you should use for "advanced settings" is defined like this:
add_submenu_page( string $parent_slug, string $page_title, string
$menu_title, string $capability, string $menu_slug, callable $function
= '' );
As you can see you can define a certain $capability for displaying your menu or submenu page.
You can either use a capability which is only available to admins like manage_options or you could even define a special capability like this:
$role = get_role( 'administrator' );
$role->add_cap( 'edit_advanced_travel_settings' );
https://codex.wordpress.org/Function_Reference/add_cap
If you do it correctly the advanced page should only be visible to users whom you have granted this capability.
Note: Within the custom admin page callback - for security reasons - you should also check if the current user has the required capability.
if(current_user_can('edit_advanced_travel_settings')) {
// display page
} else {
echo "Sorry advanced travel settings are not available to you.";
}

How to make conditional field on cmb2 plugin?

I want to make a condition for my members info page.
$biometabox[] = array(
'id' => 'first-section',
'title' => 'Member Data',
'object_types' => array('dausfmembers'),
'fields' => array(
array(
'name' => 'Gender',
'type' => 'radio',
'id' => $dausf.'gender',
'options' => array(
'Male' => 'Male',
'Female' => 'Female'
)
),
array(
'name' => 'Gender',
'type' => 'radio',
'id' => $dausf.'mstatus',
'options' => array(
'Married' => 'Married',
'Single' => 'Single'
)
),
i want to make if female and married show this fileds in admin panel.
array(
'name' => 'Husband Name',
'type' => 'text',
'id' => $dausf.'hname',
),
can anyone help me out from this ??
"Conditional fields" seem not to be integrated within CMB2 core yet. However, there's a plugin called CMB2 Conditionals which might help you achieve the functionality you want.
After installing and setting up the plugin, it'd be simply achieved by setting up your fields as follows:
A special attention to the 'attributes' key, you can play with it as per the plugin's instructions.
$biometabox[] = array(
'id' => 'first-section',
'title' => 'Member Data',
'object_types' => array('dausfmembers'),
'fields' => array(
array(
'name' => 'Gender',
'type' => 'radio',
'id' => $dausf.'gender',
'options' => array(
'Male' => 'Male',
'Female' => 'Female',
),
'attributes' => array(
'required' => 'required',
)
),
array(
'name' => 'Gender',
'type' => 'radio',
'id' => $dausf.'mstatus',
'options' => array(
'Married' => 'Married',
'Single' => 'Single',
),
'attributes' => array(
'required' => 'required',
)
),
array(
'name' => 'Husband Name',
'type' => 'text',
'id' => $dausf.'hname',
'required' => true,
),
'attributes' => array(
'required' => true, // Will be required only if visible.
'data-conditional-id' => $prefix . 'gender',
'data-conditional-value' => 'Female',
),
'attributes' => array(
'required' => true, // Will be required only if visible.
'data-conditional-id' => $prefix . 'mstatus',
'data-conditional-value' => 'Married',
),
...
) );
You'll want to check the plugin's example functions here: https://github.com/jcchavezs/cmb2-conditionals/blob/master/example-functions.php
I hope you manage to make it you work. Good luck.
Although this might be a bit convoluted you can also write a custom jQuery script to show and hide fields based on the options selected:
In your theme directory add two folders called "js" and "css" if they aren't already there.
Then create a file in /js called "admin_scripts.js". And create a file in /css called "admin.css".
So you'll have:
theme_directory/css/admin.css
theme_directory/js/admin_scripts.js
In your functions.php add the following:
function admin_scripts() {
// Adding custom admin scripts file
wp_enqueue_script( 'admin-js', get_template_directory_uri() . '/js/admin_scripts.js', array( 'jquery' ));
// Registering and adding custom admin css
wp_register_style( 'custom_wp_admin_css', get_template_directory_uri() . '/css/admin.css', false, '1.0.0' );
wp_enqueue_style( 'custom_wp_admin_css' );
}
Then just below this function add:
add_action( 'admin_enqueue_scripts', 'admin_scripts' );
In js/admin_scripts.js add the following (remember to change the ids and classes to your field's ids and classes)
jQuery(document).ready( function() {
if( jQuery('#cmb2_select_field_id').val() == 'conditional_option') {
jQuery('.cmb2-field-to-display-on-select').show();
}
jQuery('#cmb2_select_field_id').bind('change', function (e) {
if( jQuery('#cmb2_select_field_id').val() == 'conditional_option') {
jQuery('.cmb2-field-to-display-on-select').show();
}
else{
jQuery('.cmb2-field-to-display-on-select').hide();
}
});
});
And in css/admin.css add the following:
.cmb2-field-to-display-on-select {
display:none;
}

Resources