I’m making a navigation plugin which adds animations to your wordpress navigation.
I read in WordPress Codex that I would need to use wp_enqueue_style().
First, How do I use this code for multiple css files?
in my main plugin file.
Now according to this question, I would add stylesheets like this:
function add_my_stylesheet()
{
wp_enqueue_style( 'myCSS', plugins_url( '/css/myCSS.css', __FILE__ ) );
}
add_action('admin_print_styles', 'add_my_stylesheet');
Since my plugin requires multiple stylesheets to be added, which of the two codes should I use?
function add_my_stylesheet()
{
wp_enqueue_style( 'myCSS', plugins_url( '/css/myCSS.css', __FILE__ ) );
}
add_action('admin_print_styles', 'add_my_stylesheet');
function add_my_stylesheet1()
{
wp_enqueue_style( 'myCSS1', plugins_url( '/css/myCSS1.css', __FILE__ ) );
}
add_action('admin_print_styles', 'add_my_stylesheet1');
Or
function add_my_stylesheet1()
{
wp_enqueue_style( 'myCSS1', plugins_url( '/css/myCSS.css', __FILE__ ) );
wp_enqueue_style( 'myCSS1', plugins_url( '/css/myCSS1.css', __FILE__ ) );
}
add_action('admin_print_styles', 'add_my_stylesheet1');
Now I know where to add this code. But how do I call the stylesheet I want to use?
Also, just to clear what my plug would do:
The main plugin file would only load the stylesheet required.
And the options page would allow the user to select which of the stylesheet to require.
for example, in php format I would use the following:
<link href="nav-<?php echo $name; ?>.css">
The $name should be the one selected from the options page, but how do I call in the required stylesheet?
I hope my question is clear enough.
To answer your first question, you would use the second style, i.e.
What the
add_action()
function does is tell WordPress, “When theadmin_print_styles
action occurs, run thisadd_my_stylesheet()
function.” Confusingly, the practice of using theadmin_print_styles
andadmin_enqueue_styles
actions to enqueue stylesheets is incorrect – as counter-intuitive as it is, you should useadmin_enqueue_scripts
instead.The calls to
wp_enqueue_style()
then add the specified stylesheets to a list of stylesheets that will be loaded (if you specify the dependencies argument, then WordPress will also take care of making sure that your stylesheets load in the correct order). You don’t need to “call” an enqueued stylesheet as you suggested – if it is enqueued, then it will be printed out in an HTML<link ...>
element in the<head></head>
section in the same manner that WordPress loads it’s own stylesheets.In order to select which stylesheet will be loaded, you simply add your logic to your
add_my_stylesheet()
function; if you don’t wish for a stylesheet to be used, then you don’t enqueue it, i.e.:Use an if to check if it conforms to your criteria, for example
That way it’ll only pull the CSS that you want on the desired page / when the desired admin option is selected.
Or: