Implementing A WordPress Settings Class for Options Pages - wordpress

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 :)

Related

How will i add an option under customizer woocommerce section?

What is the customizer section name? I want to add a field option under customizer woocommerce section. Or how will I add a field under woocommerce option? Can someone help me with that?
$wp_customize->add_section( 'woocommerce_secction_name' , array(
'title' => __( 'My Section Name', 'starter' ),
'priority' => 30
) );
$wp_customize->add_setting( 'starter_new_setting_name' , array(
'default' => '#000000',
'transport' => 'refresh',
) );
$wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'link_color', array(
'label' => __( 'Header Color', 'starter' ),
'section' => 'woocommerce_secction_name',
'settings' => 'starter_new_setting_name',
) ) );
// Add section to WooCommerce Panel
$wp_customize->add_section( 'SECTION_NAME',
array(
'title' => __( 'SECTION TITLE'),
'panel' => 'woocommerce',
'capability' => '',
'priority' => 500,
)
);
The important part is the panel name as you can see selecting woocommerce it will add to the woocomerce panel

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

WordPress customizer multiple add_control 's in section

Quick question.
I'm trying to add multiple controls within a WordPress customizer section.
$wp_customize->add_section( 'lr_panel2', array(
'title' => esc_html__( 'Panel 2', 'lr' ),
'active_callback' => 'is_front_page',
'panel' => 'lr_theme_options',
'description' => esc_html__( 'Add a background image to your panel by setting a featured image in the page editor. If you don’t select a page, this panel will not be displayed.', 'lr' ),
) );
$wp_customize->add_setting( 'lr_panel2', array(
'default' => false,
'sanitize_callback' => 'lr_sanitize_numeric_value',
) );
$wp_customize->add_control( 'lr_panel2', array(
'label' => esc_html__( 'Panel Content', 'lr' ),
'section' => 'lr_panel2',
'type' => 'dropdown-pages',
) );
So this one is working fine and dandy. I try to add a second one and neither render. I assumed I could just repeat the add_control class, something like:
$wp_customize->add_control( 'lodestar_panel2', array(
'label' => esc_html__( 'Panel Layout', 'lr' ),
'section' => 'lr',
'type' => 'select',
'choices' => array(
),
) );
But that's not working how I want it too, Has anyone done this before?
Thanks!
You are adding the control in another section. Section should be same
'section' => 'lr_panel2',
This is the section of first control you've added and
'section' => 'lr',
This is the section of second control you've added
Also a control wouldn't show unless you've added somethings in it.

retrieve customize_register setting from wordpress

I'm trying to retrieve some customizer settings that I initiated.
function notop_customize_register( $wp_customize ) {
$wp_customize->add_section( 'header_images', array(
'title' => 'Header Images',
'priority' => 2,
'description' => "Add images with links to be redirected to when the images are clicked",
) );
// Add color scheme setting and control.
$wp_customize->add_setting( 'header_image_1_setting', array(
'capability' => 'edit_theme_options',
'type' => 'option',
) );
$wp_customize->add_control(new WP_Customize_Upload_Control($wp_customize,'header_image_1_control', array(
'label' => __( 'Import Image', 'notop' ),
'section' => 'header_images',
'settings' => 'header_image_1_setting',
) ) );
}
add_action( 'customize_register', 'notop_customize_register' );
How do I get the url of header_image_1?
I thought it might be get_theme_mod('header_image_1_setting'); but that hasn't worked so far. Also, I'm not sure if I'm actually supposed to call the customize_register file in my index somewhere or if it's okay the way it is.
cale_b's comment is the answer.
get_option('header_image_1_setting')
=> http://...

Fatal error when adding setting to customizer in wordpress

I am trying to add an extra setting in the Wordpress customizer. Can you add extra settings in the site title & tagline section or do you have to create your own section? This is the code I have so far in my functions.php file:
function mytheme_customize_register( $wp_customize )
{
$wp_customize->add_control(
new WP_Customize_Control(
$wp_customize,
'your_setting_id',
array(
'label' => __( 'Your Label Name', 'theme_name' ),
'section' => 'title_tagline',
'settings' => 'your_setting_id',
'type' => 'text'
)
)
);
}
add_action( 'customize_register', 'mytheme_customize_register' );
If I try to go to the customize theme page in the admin area though I get the following error:
Fatal error: Call to a member function check_capabilities() on a non-object in C:\xampp\htdocs\one-gas\wp-includes\class-wp-customize-control.php on line 161
Yes, you can add to the title and tagline section. Try using this code:
$wp_customize->add_control(
new WP_Customize_Control(
$wp_customize,
'your_setting_id',
array(
'label' => __( 'Your Label Name', 'theme_name' ),
'section' => 'title_tagline',
'settings' => 'your_setting_id',
'type' => 'text'
)
)
)
);
Read more about the Theme Customization API at the WordPress Codex.
I fixed it in the end it's because I had to define a setting first so this is the working code:
function mytheme_customize_register( $wp_customize )
{
$wp_customize->add_setting('subtagline', array(
'default' => 0,
'type' => 'option'
));
$wp_customize->add_control(
new WP_Customize_Control(
$wp_customize,
'subtagline',
array(
'label' => __( 'Sub Tagline', 'One Gas' ),
'section' => 'title_tagline',
'settings' => 'subtagline',
'type' => 'text'
)
)
);
}
add_action( 'customize_register', 'mytheme_customize_register' );

Resources