Answer the question
In order to leave comments, you need to log in
Optimization of connecting scripts/css in Wordpress?
Expensive time of day.
Please advise some plugin for selective connection of scripts, etc. only on those pages where these scripts are called. The same is desirable for CSS. There is no desire to fence tons of templates for pages, and the search for the right plugin is still ineffective.
Answer the question
In order to leave comments, you need to log in
http://codex.wordpress.org/Function_Reference/wp_enqueue_script There is an example on the page
function theme_name_scripts() {
wp_enqueue_style( 'style-name', get_stylesheet_uri() );
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );
In the body of the function, add
global $post;
and then check for id, templates, etc., and connect what you need. What realties better vryat there. Is it a plugin that will most likely act on the same principle
Wouldn't it be better to merge all JS into one file, and, accordingly, all CSS into one too? There are more chances to lose on the time of sending an HTTP request than on high traffic. Plus, JS and CSS are perfectly gzipable.
WordPress, as far as I remember, cannot do this, although I can make mistakes, if css styles are manually clogged, then connect the scripts with your hands.
Have you read about conditional checks? is_category( $cat_id ) for example, there are a lot of them. Full flexibility, ifcho. You check for the necessary condition and load what you need, there is no need to produce templates. As for scripts and styles loaded by plugins, it is always possible to find out on which hook and priority they are loaded and modified. You can turn it off and on manually when and where you need it. Or vice versa, turn off where it is not necessary. In general, there are many ways to do this, without additional templates and plugins.
For example, if in the process of developing a project I see scripts and styles of a plugin that will be 100% site-wide and always, I turn off their dynamic loading by the plugin itself, manually transfer the scripts and styles to my own and merge them. Minus - over time, when the plugin updates the versions of the scripts, you will have to manually change them. But, since we are on support anyway once a month, or as Security fixes appear, we carry out maintenance, this is not a problem. Plus - no extra http requests, combined scripts and styles into one will compress better.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question