WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to tell if we're on the dashboard? (6 posts)

  1. Jick
    Member
    Posted 5 years ago #

    Is there a way to know when we're on the main dashboard of WordPress? I'm developing a new plugin for the upcoming WordPress 2.7 and I'm adding a widget to the dashboard that needs some custom styling included in the <head> area. It only needs to be included if we're on the main dashboard page.

    I've tried this:

    if(strpos($_SERVER['REQUEST_URI'], 'index.php') {
      // Add styles...
    }

    But that only works when you're actually on /wp-admin/index.php and not when you're just on /wp-admin/. Also, I was really looking for a more reliable method than that. Like a built-in function such as is_page() or something...

    Any help on this issue would be greatly appreciated...

  2. Familiar
    Member
    Posted 5 years ago #

    add_action('admin_head-index.php', 'your_function');
    
    function your_function() {
    	// Do stuff;
    }

    This works for all of the core WordPress PHP files which have a user-facing administration element (i.e. post.php, post-new.php, etc.)

    Also you could use $_SERVER['REQUEST_URI'] and compare against bloginfo('wpurl').'/wp-admin/' -- this will also work, as WordPress always appends a trailing slash to the URI.

  3. Jick
    Member
    Posted 5 years ago #

    add_action('admin_head-index.php', 'your_function');
    That works perfectly! Thank you so much!

    I never knew about that... Do you know if there is any documentation on that or is it just one of those "known only by those who already know about it" things? :P

    I'll also keep in mind your other suggestion.

    Thanks again!

  4. Jick
    Member
    Posted 5 years ago #

    Oh, one other thing...

    Do you know if that same functionality can be used for plugin pages? So, like, something like this:

    add_action('admin_head-my-plugin-page', 'your_function');

    Thanks again for your help. :)

  5. Familiar
    Member
    Posted 5 years ago #

    No problem.

    Anyway, the first solution, no that is not a well-documented feature. But to answer your second question -- that can be done, and that IS documented.

    Have a look here:
    Action: admin_head-(plugin_page)

    You'll notice that (plugin_page) is assigned as a variable prior to setting up the action. You have probably already written that line of code; just assign it to a variable, and you will be able to create the action from it.

  6. Jick
    Member
    Posted 5 years ago #

    That's great! Thanks for the link. I really appreciate it. Thank you very much!

Topic Closed

This topic has been closed to new replies.

About this Topic