Out of necessity I should add all the CSS and JS files present in any WordPress article on a specific page through code.
Do you know how to add all these files?
If I try through the normal get_header()
code, the necessary files are added for the page and not for the articles, as it should normally be.
I would like to make this change because I embed an article on a page and the codes of a plugin are not presented if the page is not actually read as an article.
Thanks a lot to those who will help me.
This is the answer I get if I try to add files after files, that is, all CSS and JS
Out of necessity I should add all the CSS and JS files present in any WordPress article on a specific page through code.
Do you know how to add all these files?
If I try through the normal get_header()
code, the necessary files are added for the page and not for the articles, as it should normally be.
I would like to make this change because I embed an article on a page and the codes of a plugin are not presented if the page is not actually read as an article.
Thanks a lot to those who will help me.
This is the answer I get if I try to add files after files, that is, all CSS and JS
Share Improve this question edited Jan 16, 2020 at 16:45 Matteo Feduzi asked Jan 16, 2020 at 14:03 Matteo FeduziMatteo Feduzi 291 silver badge9 bronze badges 3 |1 Answer
Reset to default 0In your theme's file functions.php:
add_action('wp_enqueue_scripts','STACK_356524_add_css_js_on_page',99);
function STACK_356524_add_css_js_on_page(){
global $post;
if($post->post_type=='page') { // check if the current wp object is a `page` also is_page() can be used: see https://codex.wordpress/Conditional_Tags
wp_enqueue_script( 'js_handler', 'url_of_your_script', array(), '' );
wp_enqueue_style('css_handler','url_of_your_style',array(), '1.0.0', 'all');
}
}
Up to you adjust the correct path to the .css and .js you need to enqueue, you can have a look at the source page where the scripts and files are present and use that string.
More granular adjustment can be done ( versioning, dependencies -I.E. jquery or even enqueue only on specific pages -i.e: if( post->ID ==12345){}
.
For more info:
https://developer.wordpress/reference/functions/wp_enqueue_script/ https://developer.wordpress/reference/functions/wp_enqueue_style/
(IT) https://softrade.it/WP/wp_enqueue_script/
<head>
tag if the needed styles and scripts are present. In this case the enqueuing is ok but the errors are caused by the .js themselves.CodeMirror is not defined
is likely to means that a some more scripts are missing. Also important the order with wich you enqueue the scripts.CodeMirror
evidently must be defined before it can be used – Andrea Somovigo Commented Jan 16, 2020 at 16:49