Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: Add CSS to Specific Admin Options Page

  1. #1
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,997

    Default Add CSS to Specific Admin Options Page

    Currently, I am using the following hook to add CSS for a plugin options page:

    PHP Code:
    add_action('admin_head', array(&$this'cbnetmyplugin_css')); 
    (The function, of course, then holds the added CSS.)

    But, what if I want to target my own plugin options page, specifically? How can I do that?

    Say, for instance, the URL for my options page is:

    PHP Code:
    /wordpress/wp-admin/options-general.php?page=cbnet-my-plugin-name 
    I would like to add an if statement, like such:

    PHP Code:
    $mypluginoptionpageslug 'cbnet-my-plugin-name';
    $adminurl foo//what goes here?
    $ismypluginoptionpage = ( strpos($adminurl,$mypluginoptionpageslug) !== false 'true' 'false' );
     
    if ( 
    $ismypluginoptionpage == 'true' ) {
         
    add_action('admin_head', array(&$this'cbnetmyplugin_css'));

    So the question is, what goes in "foo":

    PHP Code:
    $adminurl foo//what goes here? 
    That would give me the current URL, or other way of identifying which option page I'm currently on?
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

  2. #2
    Utkarsh is offline Hello World
    Join Date
    Nov 2009
    Posts
    73

    Default

    Try
    PHP Code:
    $_SERVER['REQUEST_URI'
    or
    PHP Code:
    $_SERVER['QUERY_STRING'
    One of them should work.

    Or maybe you could do a $_GET['page'] === 'cbnet-my-plugin-name' ?

  3. #3
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    865

    Default

    If statements are lame. Use conditional hooks instead.

    First, use get_plugin_page_hook to find out what your page's "hook" is. You can do this like so:

    PHP Code:
    $hook get_plugin_page_hook('cbnet-my-plugin-name''options-general.php'); 
    Then you add an action using that hook value:

    PHP Code:
    add_action("admin_head-{$hook}"whatever); 
    Voila.

    This needs to happen after you've called "add_options_page", otherwise your hook won't exist yet.

    Note, the add_options_page call also returns the $hook. So you can do it right then and there if you want, like so:
    PHP Code:
    $hook add_options_page(...);
    add_action("admin_head-{$hook}"whatever); 

  4. #4
    andreasnrb's Avatar
    andreasnrb is offline Kegger
    Join Date
    Jun 2009
    Posts
    595

    Default

    This method is much better =).
    PHP Code:
    $page=add_*_page(...);// *=options,menu,submenu
    add_action("admin_print_styles-$page", .... );
    add_action("admin_print_scripts-$page", ... ); 
    [EDIT]
    Missed you wanted to add plain CSS. Use Ottos add_action("admin_head-{$page}", whatever);
    Hook: admin head
    Last edited by andreasnrb; 02-16-2010 at 10:43 AM.

  5. #5
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,997

    Default

    Quote Originally Posted by Utkarsh View Post
    Try
    PHP Code:
    $_SERVER['REQUEST_URI'
    or
    PHP Code:
    $_SERVER['QUERY_STRING'
    One of them should work.

    Or maybe you could do a $_GET['page'] === 'cbnet-my-plugin-name' ?
    What about:

    PHP Code:
    __FILE__ 
    or
    PHP Code:
    $_SERVER['PHP_SELF'
    With respect to pulling out my plugin options page slug from the URI, what are the practical differences among these variables?
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

  6. #6
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,997

    Default

    Quote Originally Posted by Otto View Post
    If statements are lame. Use conditional hooks instead.

    First, use get_plugin_page_hook to find out what your page's "hook" is. You can do this like so:

    PHP Code:
    $hook get_plugin_page_hook('cbnet-my-plugin-name''options-general.php'); 
    Then you add an action using that hook value:

    PHP Code:
    add_action("admin_head-{$hook}"whatever); 
    Voila.

    This needs to happen after you've called "add_options_page", otherwise your hook won't exist yet.

    Note, the add_options_page call also returns the $hook. So you can do it right then and there if you want, like so:
    PHP Code:
    $hook add_options_page(...);
    add_action("admin_head-{$hook}"whatever); 
    And these answers are why I ask the questions: to learn how to advance my PHP skills from lame to not-so-lame. :)
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

  7. #7
    andreasnrb's Avatar
    andreasnrb is offline Kegger
    Join Date
    Jun 2009
    Posts
    595

    Default

    adambrown has excellent hook database and wpseek is also good.

  8. #8
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,997

    Default

    Quote Originally Posted by andreasnrb View Post
    adambrown has excellent hook database and wpseek is also good.
    I love WPSeek!
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

  9. #9
    Otto's Avatar
    Otto is offline On The Rocks
    Join Date
    Apr 2009
    Location
    Memphis, TN
    Posts
    865

    Default

    Few other tips in this area:

    - Adding your options page should happen on the "admin_menu" action hook.

    - If you want to reduce overhead for adding actions and such, hook your admin page setup function into the "load-{$hook}" action. You can also hook to just the "{$hook}" action. load-hook happens before the admin-header, the plain $hook happens afterwards.

    - "admin_head-{$hook}" happens in the admin header itself. But the admin_print_styles-{$hook} and admin_print_scripts-{$hook} also both happen at the same time as the admin_head one. Ideally you would use the print styles/scripts hooks for printing CSS/Javascripts, respectively. In practice, it rarely makes a hell of a lot of difference. Still, it's good to have things in their proper places.

    - Need to do stuff in the admin footer? You've got admin_footer, admin_print_footer_scripts, and admin_footer-{$hook}.

    - Need to enqueue some special scripts? admin_enqueue_scripts is an action which has the $hook as the first parameter. You can use it like so:
    PHP Code:
     add_action('admin_enqueue_scripts','func');
    function 
    func($hook) {
    if (
    $hook == "hook I'm expecting") {
    // enqueue stuff
    }


  10. #10
    chipbennett's Avatar
    chipbennett is offline WordPress Legend
    Join Date
    Feb 2009
    Location
    St. Louis, MO
    Posts
    1,997

    Default

    Quote Originally Posted by Otto View Post
    Few other tips in this area:

    - Adding your options page should happen on the "admin_menu" action hook.

    - If you want to reduce overhead for adding actions and such, hook your admin page setup function into the "load-{$hook}" action. You can also hook to just the "{$hook}" action. load-hook happens before the admin-header, the plain $hook happens afterwards.

    - "admin_head-{$hook}" happens in the admin header itself. But the admin_print_styles-{$hook} and admin_print_scripts-{$hook} also both happen at the same time as the admin_head one. Ideally you would use the print styles/scripts hooks for printing CSS/Javascripts, respectively. In practice, it rarely makes a hell of a lot of difference. Still, it's good to have things in their proper places.

    - Need to do stuff in the admin footer? You've got admin_footer, admin_print_footer_scripts, and admin_footer-{$hook}.

    - Need to enqueue some special scripts? admin_enqueue_scripts is an action which has the $hook as the first parameter. You can use it like so:
    PHP Code:
     add_action('admin_enqueue_scripts','func');
    function 
    func($hook) {
    if (
    $hook == "hook I'm expecting") {
    // enqueue stuff
    }

    Have you ever thought of updating the related Codex articles? This is good stuff!
    WP TurnKey - Turn-Key WordPress installation and maintenance services
    WordPress user since 2005 | @chip_bennett | chipbennett.net | cbnet Plugins

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •