0

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

enter image description here

3
  • So the page you're mentioning is not a page created a wp page but an extra page with a physical file? otherwise, if it's a wp page, the .js and .css files should be enqueued, except if some plugins are enqueuing scripts and styles only for specific post_types.. Commented Jan 16, 2020 at 14:59
  • Surely the plugin in question queues for post_types. How can I do? And anyway yes, my pages are all personalized but they use the basic WordPress code. Commented Jan 16, 2020 at 15:24
  • the screenshot shows js errors that can depend on other issues, check the source code of your page and see in <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 Commented Jan 16, 2020 at 16:49

1 Answer 1

0

In 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.org/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.org/reference/functions/wp_enqueue_script/ https://developer.wordpress.org/reference/functions/wp_enqueue_style/

(IT) https://softrade.it/WP/wp_enqueue_script/

6
  • Thanks a lot for your answer, I have previously tried this answer to insert code after code but it doesn't work anyway. Commented Jan 16, 2020 at 16:42
  • this must work, if there are issues can be surely solved Commented Jan 16, 2020 at 16:45
  • I updated the ticket, I see the problems shown in the image. Commented Jan 16, 2020 at 16:46
  • except if in your page template you don't use get_header(), in this case that will not work Commented Jan 16, 2020 at 16:46
  • I confirm that get_header () is on the page. Perhaps the problem may be due to the fact that the article code calls it from an AJAX request? Commented Jan 16, 2020 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.