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

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.";
}

Related

Meta box tabs - in a custom post type

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.

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');

Retrieving data from additional OptionTree pages

Adding OptionTree pages is simple with the code below, but does anyone know how to retrieve the stored data?
/**
* Hook to register admin pages
*/
add_action( 'init', 'register_options_pages' );
/**
* Registers all the required admin pages.
*/
function register_options_pages() {
// Only execute in admin & if OT is installed
if ( is_admin() && function_exists( 'ot_register_settings' ) ) {
// Register the pages
ot_register_settings(
array(
array(
'id' => 'custom_options',
'pages' => array(
array(
'id' => 'test_page',
'parent_slug' => 'options-general.php',
'page_title' => 'Test Page',
'menu_title' => 'Test Page',
'capability' => 'edit_theme_options',
'menu_slug' => 'test-page',
'icon_url' => null,
'position' => null,
'updated_message' => 'Test Page updated.',
'reset_message' => 'Test Page reset.',
'button_text' => 'Save Changes',
'show_buttons' => true,
'screen_icon' => 'options-general',
'contextual_help' => null,
'sections' => array(
array(
'id' => 'test_section',
'title' => __( 'Test Section', 'motif-core' )
)
),
'settings' => array(
array(
'id' => 'test_section_input',
'label' => 'Test Input',
'desc' => 'Pretty freaking awesome!',
'std' => '',
'type' => 'text',
'section' => 'test_section',
'class' => ''
)
)
)
)
)
)
);
I tried this $my_plugin_options = get_option('custom_options'); but it only shows the word 'array' on the front end?
This is how to retrieve the stored data:
$my_plugin_options = get_option('custom_options');
echo $my_plugin_options['test_section_input'];

How validate input and output of embed video field

I use CMB2 — WordPress Plugins to create Custom Metaboxes. I have a textarea for embeding video. I need to validate the field in right way both on input and output.
$cmb = new_cmb2_box( array(
'id' => $prefix . 'video_box',
'title' => __( 'Video - Embeded from YouTube or Vimeo', 'ttt' ),
'object_types' => array( 'post' ), // Post type
'context' => 'normal',
'priority' => 'high',
'show_names' => true
) );
$cmb->add_field( array(
'name' => __( 'Embed Code', 'ttt' ),
'id' => $prefix . 'embed_code',
'type' => 'textarea',
'sanitization_cb' => false,
) );
Here is how I use it the field.
$embed_code = get_post_meta($id, 'ttt_embed_code', true )

Implementing A WordPress Settings Class for Options Pages

I'm trying to implement this class: A WordPress Settings Class for Options Pages
What I tried:
function add_wp_page(){
require_once( TEMPLATEPATH."/folder_admin/wm-settings.php" );
// A top level page
$my_top_page = create_settings_page(
'my_top_level_page',
__( 'My Top Level Page' ),
array(
'parent' => false,
'title' => __( 'Top Level Menu' ),
'icon_url' => 'dashicons-admin-generic',
'position' => '63.3'
),
array(
'my_standard_section' => array(
'title' => __( 'Standard' ),
'description' => __( 'My section description.' ),
'fields' => array(
'my_input' => array(
'label' => __( 'Input example' )
),
'my_checkbox' => array(
'type' => 'checkbox',
'label' => __( 'Checkbox example' )
),
'my_textarea' => array(
'type' => 'textarea',
'label' => __( 'Textarea example' )
)
)
)
),
array(
'tabs' => true,
'submit' => __( 'My Submit' ),
'reset' => __( 'My reset' ),
'description' => __( 'My page description.' ),
'updated' => __( 'My success message !')
)
);
// And a sub-page
$my_sub_page = create_settings_page(
'my_sub_page',
__( 'My Sub Page' ),
array(
'parent' => 'my_top_level_page',
'title' => __( 'Sub Level Menu' )
)
);
}
add_action( 'admin_init', 'add_wp_page' );
The TEMPLATEPATH is it ok. If I echo something in wp-settings.php it will print it.
The rest of the code it is from that website. It is from an example.
The menu doesn't show.
Am I missing something?
Thanks
The settings need to be registered before 'admin_init'.
Replace the last line with add_action( 'init', 'add_wp_page' );.
Also, avoid wrapping the require_once in an action or you won't be able to access get_setting() outside your hook.
Oh, and the position can be a string. And it is actually recommended to use a decimal number to prevent possible conflicts for these menu positions with other plugins or themes.
Please set a 'position' => '63.3' to integer.
or use positions of core menu:
2 Dashboard
4 Separator
5 Posts
10 Media
15 Links
20 Pages
25 Comments
59 Separator
60 Appearance
65 Plugins
70 Users
75 Tools
80 Settings
99 Separator
The position in the menu order this menu should appear.
Hope it solves your problems :)

Resources