I currently have a custom post type with a sub menu page via add_submenu_page
. On this page I have a form with some basic settings such as a text editor and some radio buttons.
function programme_enable_pages() {
add_submenu_page(
'edit.php?post_type=programmes',
'content',
'Archive content',
'edit_pages',
basename(__FILE__),
'archiveContent'
);
}
add_action( 'admin_menu', 'programme_enable_pages' );
function register_programme_settings() {
register_setting( 'programme_content', 'programme_content' );
register_setting( 'programme_content', 'programme_branding' );
}
add_action( 'admin_init', 'register_programme_settings' );
function archiveContent() { ?>
<div class="wrap">
<form method="post" action="options.php">
<?php
settings_fields( 'programme_content' );
do_settings_sections( 'programme_content' );
wp_editor(
get_option( 'programme_content' ),
'programme_content',
$settings = array(
'textarea_name' => 'programme_content'
)
); ?>
<label><b>Branding options</b></label><br>
<input type="radio" name="programme_branding" id="branding_blue" value="blueBranding" <?php checked('blueBranding', get_option('programme_branding'), true); ?> checked="checked">Blue Branding<br>
<input type="radio" name="programme_branding" id="branding_red" value="redBranding" <?php checked('redBranding', get_option('programme_branding'), true); ?>/>Red Branding<br>
<input type="radio" name="programme_branding" id="branding_orange" value="orangeBranding" <?php checked('orangeBranding', get_option('programme_branding'), true); ?>/>Orange Branding
<?php submit_button(); ?>
</form>
</div>
<?php }
When a user with a role of administrator edits any of the content and saves the changes, the content saves with no issues. But when a user with a role of editor saves the changes, they are directed to a page which states:
Sorry, you are not allowed to manage these options.
Can anyone please point me in the right direction on how I can make anyone with a user role of editor save these options?
I currently have a custom post type with a sub menu page via add_submenu_page
. On this page I have a form with some basic settings such as a text editor and some radio buttons.
function programme_enable_pages() {
add_submenu_page(
'edit.php?post_type=programmes',
'content',
'Archive content',
'edit_pages',
basename(__FILE__),
'archiveContent'
);
}
add_action( 'admin_menu', 'programme_enable_pages' );
function register_programme_settings() {
register_setting( 'programme_content', 'programme_content' );
register_setting( 'programme_content', 'programme_branding' );
}
add_action( 'admin_init', 'register_programme_settings' );
function archiveContent() { ?>
<div class="wrap">
<form method="post" action="options.php">
<?php
settings_fields( 'programme_content' );
do_settings_sections( 'programme_content' );
wp_editor(
get_option( 'programme_content' ),
'programme_content',
$settings = array(
'textarea_name' => 'programme_content'
)
); ?>
<label><b>Branding options</b></label><br>
<input type="radio" name="programme_branding" id="branding_blue" value="blueBranding" <?php checked('blueBranding', get_option('programme_branding'), true); ?> checked="checked">Blue Branding<br>
<input type="radio" name="programme_branding" id="branding_red" value="redBranding" <?php checked('redBranding', get_option('programme_branding'), true); ?>/>Red Branding<br>
<input type="radio" name="programme_branding" id="branding_orange" value="orangeBranding" <?php checked('orangeBranding', get_option('programme_branding'), true); ?>/>Orange Branding
<?php submit_button(); ?>
</form>
</div>
<?php }
When a user with a role of administrator edits any of the content and saves the changes, the content saves with no issues. But when a user with a role of editor saves the changes, they are directed to a page which states:
Sorry, you are not allowed to manage these options.
Can anyone please point me in the right direction on how I can make anyone with a user role of editor save these options?
In the wp-admin/options.php
file you find filter with name created from your settings group to control permissions:
$capability = apply_filters( "option_page_capability_{$option_page}", $capability );
to fix issue, add filter with your desired permission level like:
add_filter( 'option_page_capability_programme_content', 'my_settings_permissions', 10, 1 );
function my_settings_permissions( $capability ) {
return 'edit_pages';
}
To save options the user needs to have the manage_options
capability, which editors lack.
Your use of edit_pages
capability for the menu, lets the admin page be accessible to editors, but setting submit goes to the option handler code which is not taking into account the context of the admin page.
How to fix? I would say that no one except for an admin should be able to change site wide setting, so your approach is just wrong... more constructive options are not to use the settings API and handle the form submission yourself, or use a role manager type of plugin to give the user the manage_option
capability.