0

I need a code snippet to load css (extra style sheet?) when a author is logged in. This is for a custom dashboard and post "page".

enter image description here

The screenshot is what i mean by post page. I want to hide stuf by css for only authors. Things within the red circles are things i want to hide.

1
  • 1
    Can you tell me what do you mean by "Custom dashboard"? Is it a WordPress page? Second thing is that what do you mean by post "page". Does it mean all the pages (post_type of 'page')?
    – Omar Tariq
    Commented Dec 19, 2015 at 17:49

3 Answers 3

0

If a user is not logged in then admin_head probably won't run. So let's just check their capabilities.

function my_custom_admin_head() {
    if ( ! current_user_can( 'have-fun' )) :
    ?><style>
        #welcome-panel{display: none !important;}
        #wp-content-editor-tools{display: none !important;}
    </style>
    <?php endif; // cant' have-fun
}
add_action( 'admin_head', 'my_custom_admin_head' );
1
  • This is it! So i place my own css in the code and replace have-fun by administrator. Great! Always amazing how stange people help each other on these forums. Thanks everybody.
    – Arnolddew
    Commented Dec 19, 2015 at 22:16
1

Change $url according to your requirements. In the current state the $url links to a stylesheet custom-style.css located in your current active theme's subdirectory admin. I tested this code and it is working fine. Enjoy :-)

/**
 * This function registers and enqueues styles on front-end if
 * logged-in user has a role 'author'.
 */
function customStylesheetForAuthorsOnly() {

    /* Unique slug for the specific resource. */
    $handle = 'unique-css-stylesheet-handle';

    /* URL of the stylesheet (resource). */
    $url = get_template_directory_uri() . '/admin/custom-style.css';

    /* Array containing the handles of all the dependencies. */
    $dependencies = array();

    wp_register_style($handle, $url, $dependencies);

    global $current_user;
    get_currentuserinfo();

    /* Check if current user has 'author' in his roles */
    if(in_array('author', $current_user->roles) === true) {
        wp_enqueue_style($handle);
    }

}
add_action('admin_enqueue_scripts', 'customStylesheetForAuthorsOnly');
3
  • Thanks. I will look in to it. For now it breaks my site. I also posted a screenshot for more info. Thanks again.
    – Arnolddew
    Commented Dec 19, 2015 at 19:14
  • @Arnolddew I've updated my answer.
    – Omar Tariq
    Commented Dec 19, 2015 at 19:43
  • You are right. This is the answer to my original Q. Thanks Omar.
    – Arnolddew
    Commented Dec 20, 2015 at 9:07
0

Get current user ID and compare it with post author ID. If they are equal then load extra style sheet.

To get current user ID use $user_ID = get_current_user_id();

To get post author ID use $author_ID = get_the_author();

The best practice to load style sheet is to use wp_enqueue_style() function.

1
  • Thanks for your answer. Css and html are not a problem to me. But coding is. Do you maybe have a sugestion for a snipet? What about below, am i going in the right direction? codefunction wpdocs_enqueue_custom_admin_style() { wp_register_style( 'custom_wp_admin_css', get_template_directory_uri() . '/css/authorlayout.css', false, '1.0.0' ); wp_enqueue_style( 'custom_wp_admin_css' ); } add_action( 'admin_enqueue_scripts', 'wpdocs_enqueue_custom_admin_style' );code
    – Arnolddew
    Commented Dec 19, 2015 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.