• Resolved noobiestrikesagain

    (@noobiestrikesagain)


    Hey,

    I’m developing a plugin and using the following code to allow admin access it:

    if( !is_admin() ) :
    	include( 'inc/front-end.php' );
    else:
    	include( 'inc/back-end.php' );
    endif;

    It works fine for admins but I would like to extend this to Editors of site as well.

    I have tried current_user_can(edit_others_posts) instead of is_admin() but no luck.

    Any suggestions to fix this?

    Thanks!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Hi, is_admin() function is not a capability check function, it tells if current page is frontend or backend (look into Codex: https://codex.ww.wp.xz.cn/Function_Reference/is_admin).

    use current_user_can with proper cap from the list [there is a role -> cap list], or use levels (Codex again: https://codex.ww.wp.xz.cn/Roles_and_Capabilities)

    but strange, current_user_can( 'edit_others_posts' ); should work for Editors

    Thread Starter noobiestrikesagain

    (@noobiestrikesagain)

    Hey,

    Should I use is_admin() && current_user_can( 'edit_others_posts' );

    It would check for backend as well as current user capability.

    Exactly, that is correct, but in your case is_admin() returns false for Editors? Are both Admin and Editor accessing this code from dashboard? Maybe try to compare content of $GLOBALS['current_screen'] for Editor and Admin, this global variable contains data used by is_admin() function

    Thread Starter noobiestrikesagain

    (@noobiestrikesagain)

    I would try that.
    But while I was testing the code, Editors are getting this error with is_admin() :

    Sorry, you are not allowed to access this page.

    Here’s my backend.php initial code:

    <?php
    
    function gret_init()
    {
      load_plugin_textdomain( 'gret', '', dirname( plugin_basename( ___FILE___ ) ) . '/lang' );
    }
    add_action( 'admin_init','gret_init' );
    
    include( 'fields.php' );
    
    function gret_settings_action_links( $links, $file )
    {
    	array_unshift( $links, '<a href="' . admin_url( 'admin.php?page=gret_add_friend' ) . '">' . __( 'Add new friend', 'gret' ) . '</a>' );
    	return $links;
    }
    add_filter( 'plugin_action_links_'.plugin_basename( ___FILE___ ), 'gret_settings_action_links', 10, 2 );
    
    function gret_admin_menu()
    {
    	
    	global $menu;
    	$pos = null;
    	for( $i=9; $i>=0; $i-- ){
    		if( !isset( $menu[$i] ) ) {
    			$pos = $i;
    			break;
    		}
    	}
    	// All my pages
    	add_menu_page( __( 'Friends list', 'gret' ), __( 'Friendship', 'gret' ), 'manage_options', 'gret_list_friends', 'gret_list_codes', plugins_url( '/images/icon.png', ___FILE___ ), $pos );
    	add_submenu_page( 'gret_list_friends', __( 'Add new friends', 'gret' ), __( 'Add new friends', 'gret' ), 'manage_options', 'gret_list_friends', 'gret_list_codes' );
    	add_submenu_page( 'gret_list_friends', __( 'Suggested Friends', 'gret' ), __( 'Suggested New friends', 'gret' ), 'manage_options', 'gret_rand_friends', 'gret_rand_friends' );
    	add_submenu_page( 'gret_list_friends', __( 'Current Friends', 'gret' ), __( Current Friends', 'gret' ), 'manage_options', 'gret_list_friends', 'gret_list_friends' );
    	add_submenu_page( 'gret_list_friends', __( 'Some options', 'gret' ), __( 'Some Options', 'gret' ), 'manage_options', 'gret_settings', 'gret_settings_page' );
    	// and registered settings
    	register_setting( 'gret_list_code', 'gret_field_code', 'gret_fields_cb' );
    	register_setting( 'gret_rand_friends', 'gret_field_prefix', 'gret_fields_cb2' );
    	register_setting( 'gret_settings', 'gret_fields' );
    }
    add_action( 'admin_menu', 'gret_admin_menu' );	

    For all menu subpages (and page also) you are setting ‘manage_options’ as a required capability to access it. Try to change it in ‘add_submenu_page’ and ‘add_menu_page calls’ to ‘edit_others_posts’.

    • This reply was modified 9 years, 7 months ago by PiotrSolarz.
    Thread Starter noobiestrikesagain

    (@noobiestrikesagain)

    Works like charm. Thank you very much.

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Allow Admin Options to Editor’ is closed to new replies.