WordPress.org

Forums

enqueue styles for plugin (7 posts)

  1. christian_gnoth
    Member
    Posted 4 years ago #

    i have registered and enqueued my plugin stylesheets accordingly to the description on this page:

    http://codex.wordpress.org/Function_Reference/wp_enqueue_style

    but if i call the plugin admin page the stylesheet is not included in the html header - i can not found an link entry for this file.

    the function is called - i have tested it.

    any idea what could be the problem?

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    Which action is the enqueue hooked on to?

    If it's not hooked onto a specific action how are you specifying when the enqueue is run?

  3. christian_gnoth
    Member
    Posted 4 years ago #

    the function is called. i have checked it - i create a file in the function with file_put_contents - the file is created. so the function is called.

    function bbnuke_plugin_add_option_page()
    {
      $bbnuke_admin_page =
      add_menu_page   (                       'baseballNuke Plugin Options', 'baseballNuke', 8, 'bbnuke-option-page',  'bbnuke_plugin_create_option_page', BBNPURL . 'images/baseballNuke_16x16.gif');
      add_submenu_page( 'bbnuke-option-page', 'Players',                     'Players',      8, 'bbnuke-players',      'bbnuke_plugin_create_players_page');
      add_submenu_page( 'bbnuke-option-page', 'Fields',                      'Fields',       5, 'bbnuke-fields',       'bbnuke_plugin_create_fields_page');
      add_submenu_page( 'bbnuke-option-page', 'Schedule',                    'Schedule',     5, 'bbnuke-schedule',     'bbnuke_plugin_create_schedules_page');
      add_submenu_page( 'bbnuke-option-page', 'Tournaments',                 'Tournaments',  5, 'bbnuke-tournaments',  'bbnuke_plugin_create_tournaments_page');
      add_submenu_page( 'bbnuke-option-page', 'Practices',                   'Practices',    5, 'bbnuke-practice',     'bbnuke_plugin_create_practice_page');
      add_submenu_page( 'bbnuke-option-page', 'Game Results',                'Game Results', 5, 'bbnuke-game-results', 'bbnuke_plugin_create_game_results_page');
      add_submenu_page( 'bbnuke-option-page', 'Uninstsll',                   'Uninstall',    5, 'bbnuke-uninstall',    'bbnuke_plugin_uninstall');
    
      add_action( 'admin_print_scripts-' . $bbnuke_admin_page, 'bbnuke_admin_head' );
    
      return;
    }
    
    function  bbnuke_admin_head()
    {
      wp_enqueue_style( 'bbnuke_admin_styles' );
    }
    function bbnuke_admin_init_method()
    {
    
      wp_register_style('bbnuke_admin_styles', BBNPURL . 'css/bbnuke-admin-plugin.css');
    }
    
    add_action( 'admin_init',   'bbnuke_admin_init_method');
  4. christian_gnoth
    Member
    Posted 4 years ago #

    any idea how to check if the style is registered properly? the enqueue function do not returns any value - so there is no chance to check if the call is successfull or not.

  5. Mark / t31os
    Moderator
    Posted 4 years ago #

    Are you aware the code you have above will only enqueue the stylesheet when it's the parent page, ie. your main plugin page, not any of the rest(subpages)... are you checking the top level page when the enqueue fails? (the bbnuke-option-page page).

    You don't necessarily need to register the style unless you're likely to re-use it elsewhere... just the enqueue is sufficient if you're only using it inside the plugin..

    eg.

    wp_enqueue_style( 'bbnuke_admin_styles', BBNPURL . 'css/bbnuke-admin-plugin.css' );

    So you could actually drop the register style code altogether..

    For checking the registered styles, you can print out the $wp_styles var..

  6. christian_gnoth
    Member
    Posted 4 years ago #

    still not working.

    I have putted it in

    add_action( 'wp_print_styles', 'bbnuke_print_styles');

    function  bbnuke_print_styles()
    {
    
      global $plugin_url;
    
      if ( is_admin() )
      {
    //  wp_register_style('bbnuke_admin_styles', BBNPURL . 'css/bbnuke-admin-plugin.css');
        wp_enqueue_style( 'bbnuke_admin_styles', BBNPURL . 'css/bbnuke-admin-plugin.css');
      }
      else
      {
        wp_register_style('bbnuke_frontend_styles', BBNPURL . 'css/bbnuke-frontend-plugin.php');
        wp_enqueue_style( 'bbnuke_frontend_styles' );
      }
    
      return;
    }

    but it is not printed.

    so i putted it in admin_init and the style is printed.

  7. Mark / t31os
    Moderator
    Posted 4 years ago #

    wp_print_styles is a front side hook (not the admin area), so it's not suited..

    Create hooks for each of your pages..

    $bbnuke_admin_page =
    add_menu_page   (                       'baseballNuke Plugin Options', 'baseballNuke', 8, 'bbnuke-option-page',  'bbnuke_plugin_create_option_page', BBNPURL . 'images/baseballNuke_16x16.gif');
    
    $players_page      =
    add_submenu_page( 'bbnuke-option-page', 'Players',                     'Players',      8, 'bbnuke-players',      'bbnuke_plugin_create_players_page');
    
    $fields_page
    add_submenu_page( 'bbnuke-option-page', 'Fields',                      'Fields',       5, 'bbnuke-fields',       'bbnuke_plugin_create_fields_page');

    ...etc..

    You can then add actions to each of those pages that should load the stylesheet..

    add_action( 'admin_print_styles-' . $bbnuke_admin_page, 'bbnuke_admin_head' );
    add_action( 'admin_print_styles-' . $players_page, 'bbnuke_admin_head' );
    add_action( 'admin_print_styles-' . $fields_page, 'bbnuke_admin_head' );

    ..and so on...

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.