Forums

[resolved] Struggling to exclude 'admin_head' for some admin pages! (3 posts)

  1. zezepedro
    Member
    Posted 2 years ago #

    Hello Forum,

    I have a plugin that needs mootools to be included in the head section of the plugin admin pages. To avoid problems with the jQuery framework used by Wordpresss I only want to include the mootools script on the respective plugin admin pages.

    I'm using the "admin_menu" hook but with no success. I've red in the codex there's a way to do that for specific admin pages only but I'm experiencing some difficulties.

    I'm doing something like this in my plugin file:

    function mootools(){
      $url = get_settings('siteurl');
      echo '<script type="text/javascript" src="'.$url.'/wp-content/plugins/myplugin/mootools.v1.11.js"></script>'."\n";
    }
    
    add_action('admin_head-????', 'mootools');

    ???? this is where I'm getting confused. What should I put here?

    I don't know if it has anything to do with it but this is the way I'm creating the admin buttons/pages for my plugin:

    function myplugin_add_admin_pages(){
      add_menu_page('Lojas', 'Lojas', 8, __FILE__, 'pluginname_overview');
      add_submenu_page(__FILE__, 'LojasAdicionar', 'Adicionar', 8, 'subpage1', 'pluginname_view');
      add_submenu_page(__FILE__, 'LojasApagar', 'Apaagr', 8, 'subpage2', 'pluginname_add');
    }

    add_action('admin_menu', 'myplugin_add_admin_pages');

    Help appreciated. Thanks. (using WP 2.2.1)

  2. harknell
    Member
    Posted 2 years ago #

    I had a similar problem. I would not suggest using the admin_head hook though--it's only valid in Wordpress 2.2, and most people aren't using that version yet, and it really didn't seem to work right anyway.

    What I did was actually simply place the javascript code directly onto the page write for my plugin admin page and it worked fine. I was surprised, but it works. If you want to see it grab either the AWSOM Pixgallery plugin or the News Announcement plugin from awsom.org and look at how the Visual Editor was added to the admin page.

  3. zezepedro
    Member
    Posted 2 years ago #

    Yes Harknell, that works. Thank you.

    However I also found another way to solve my problem. It uses an if statement just before the path to the script is printed. Check the code below to get the idea.

    function myplugin_add_mootools(){
    
      $url = get_settings('siteurl');
    
      $plugin_page = stripslashes($_GET['page']);
      $plugin_page = plugin_basename($plugin_page);
    
      if($plugin_page=="pagename")
        echo '<script type="text/javascript" src="'.$url.'/wp-content/plugins/myplugin/mootools.v1.11.js"></script>';
    }
    
    //----------------------------------------------
    
    add_action('admin_head', 'myplugin_add_mootools');

    I'm using this solution. Mootools script is only being included in my plugin admin pages. The rest of the WP admin interface remians unchanged.

Topic Closed

This topic has been closed to new replies.

About this Topic