• Resolved praiford

    (@praiford)


    I have a plugin that I created that works great with admin but when I try to use it as an editor, I get the message that I cheated and don’t have the right to make these changes. I put in the options page the edit_pages capability which allowed the plugin to show up but if I try to enable the banner and save it. I get the error message.

    This plugin just activates a banner in the heading section of a web page allowing to customize the background color and the text to be displayed as well as to whether it should be enabled or disabled (show up on the page or not). I would like the editor to be able to set this as well as the admin. Here is my code:

    <?php
    
    // Create Options Menu Link
    function enb_options_menu_link(){
    	add_options_page(
    		'Emergency Banner Options',
    		'Emergency Banner Settings',
    		'edit_pages',
    		'enb-options',
    		'enb_options_content'
    	);
    }
    
    // Create Content
    function enb_options_content(){
    	// Init Global Options
    	global $enb_options;
    
    	//Get the color value;
    	$color = $enb_options['color'];
    
    	?>
    		<div class="wrap">
    			<h1>Emergency Banner Settings</h1>
    			<h2>Settings for the Emergency Banner plugin</h2>
    			<form method="post" action="options.php">
    				<?php settings_fields('enb_settings_group'); ?>
    				<table class="form-table enb-table">
    					<tbody>
    						<tr>
    							<th scope="row"><label for="enb_settings[important_news]"><?php _e('Emergency News:','enb-domain');?></label> </th>
    							<td><input name="enb_settings[important_news]" type="text" id="enb_settings[important_news]" value="<?php echo $enb_options['important_news']; ?>" class="regular-text">
    								<p class="description" id="enb_settings[important_news]"><?php _e('Enter your Emergency or Important news here', 'enb-domain');?></p></td>
    						</tr>
    
    						<tr>
    							<th scope="row"><label for="enb_settings[enabled]"><?php _e('Enable Emergency Banner:','enb-domain');?></label> </th>
    							<td><input name="enb_settings[enabled]" type="checkbox" id="enb_settings[enabled]" value="1" <?php checked('1', $enb_options['enabled']); ?> >
    							</td>
    						</tr>						
    
    						<tr>
    							<th scope="row"><label for="enb_settings[color]"><?php _e('Select Emergency Banner color:','enb-domain');?></label> </th>
    							<td><input name="enb_settings[color]" type="radio" id="enb_settings[color]" value="red" <?php if(isset($color) && $color=="red" || !isset($color)) echo "checked"; ?> >Red
    								<input name="enb_settings[color]" type="radio" id="enb_settings[color]" value="yellow" <?php if(isset($color) && $color=="yellow") echo "checked"; ?> >Yellow
    								<input name="enb_settings[color]" type="radio" id="enb_settings[color]" value="green" <?php if(isset($color) && $color=="green") echo "checked"; ?> >Green
    							</td>
    						</tr>	
    
    					</tbody>
    				</table>
    				<p class="submit"><input type="submit" name="submit" id="submit" class="button btn" value="<?php _e('Save Changes', 'enb-domain');?>"></p>
    			</form>
    		</div>
    	<?php
    }
    
    add_action('admin_menu', 'enb_options_menu_link');
    
    // Register Settings
    function enb_register_settings(){
    	register_setting('enb_settings_group', 'enb_settings');
    }
    
    add_action('admin_init', 'enb_register_settings');
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter praiford

    (@praiford)

    Is there no way to make this work? I would like for the editors to be able update this as well as the admin. Does anyone know how to do this?

    Thread Starter praiford

    (@praiford)

    Well, I am still waiting for an answer. Does anybody know how to do this?

    Thread Starter praiford

    (@praiford)

    Ok, well after several days and hours of hunting around the internet and pulling my hair out, I found the answer. It was very simple answer as well. First, after looking at the options.php file, i could see that it forced the capability to manage_options no matter what you set your menu option for. So then I needed to find a way to force it to change the capability without manually rewriting the code in the options.php file. Here is the answer which is placed at the end of the above file just after the add_action section:

    add_filter('option_page_capability_enb_settings_group', create_function(NULL, 'return "edit_theme_options";'));

    That one code allowed me to save my changes as an editor as well as an admin and prevents the other admin settings from showing up. I hope this helps anyone else with the same issue.

    As side note, in the add_option_page block the capability has been changed to edit_theme_options. Also I had to use a code to add the edit_theme_options capability to the editor role.

    `//Get the role object
    $role_object = get_role(‘editor’);

    //add $cap capability to the role object
    $role_object->add_cap(‘edit_theme_options’); ‘

    You can run this in either the functions.php or the plugin file and then remove the code afterwards as you only need to run it once. A better solution which I will probably implement before placing this on my full website would be to set it so that it would remove the added capability if the plugin is disabled.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Change user capability’ is closed to new replies.