CSS and Javascript deregistration

Posted in: Newsmag
Post count: 69

Hi there,
I’ve noticed that the CSS and Javascript are loaded on every site page as well as other all other plugin’s CSS and JS. It’s resource wasting because it requires several TCP connections at every page load and degrading this way the site load performances and the user experience too. Therefore I’m trying to deregister CSS and Javascript where not needed and to load them only on pages where they are needed. For example, since the Contact Form 7 is used only on the ‘contacts’ page, I’ve added the following lines to Newsmag-child theme functions.php:


add_filter( 'wpcf7_load_js', '__return_false' );
add_filter( 'wpcf7_load_css', '__return_false' );
?>

Now, I should add the following lines in the page where the Contact Form 7 is used i.e. “contacts” page:


<?php
    if ( function_exists( 'wpcf7_enqueue_scripts' ) ) {
        wpcf7_enqueue_scripts();
    }
 
    if ( function_exists( 'wpcf7_enqueue_styles' ) ) {
        wpcf7_enqueue_styles();
    }
?>

But I don’t have a “contacts.php” page! So that where I have to add this code?
More in general, could you advice a method by means of which I can always include CSS and JS (mainly due to plugins) only where needed?
Thanks in advance for any appreciated help.
Giuseppe

  • This topic was modified 10 years by gmaio.
  • This topic was modified 10 years by gmaio.
  • This topic was modified 10 years by gmaio.
Post count: 6535

Hi

You will need a set of conditions which check if a specific page is displayed and if the result is true then load a specific css file. You can add the code in functions.php. Please note that this is a complex task which require good coding skills and also many customizations. If you can’t do this by yourself my advice is to hire a professional developer to help you with it as we can’t offer any type of custom work at the moment.

Thanks!

Post count: 43

Hello,

I have the same question. No problem adding the codes below to the newspaper’s functions.php file to disable the Contact Form 7 JS and CSS loading on all pages

add_filter( ‘wpcf7_load_js’, ‘__return_false’ );
add_filter( ‘wpcf7_load_css’, ‘__return_false’ );

The only issue is activating the JS and CSS in specific contact page. There’s no ‘contact.php’ template file in Newspaper Theme. Is there a specific php template I can insert the following code before wp_head is called?

<?php
if ( function_exists( ‘wpcf7_enqueue_scripts’ ) ) {
wpcf7_enqueue_scripts();
}

if ( function_exists( ‘wpcf7_enqueue_styles’ ) ) {
wpcf7_enqueue_styles();
}
?>

Contact Form 7 instructions
https://contactform7.com/loading-javascript-and-stylesheet-only-when-it-is-necessary/

Appreciate your suggestions.

Thanks,

Nik

Post count: 43

Please disregard this message. It was meant for Newspaper theme, not NewsMag.

Nik

Post count: 9544

Normally to do an if/then for a specific ‘page’ in WP, you use the post ID. When you create a page it has an ‘ID’ in the dbase. (You can find that by hovering over the edit button/link in the all pages menu in WP.)

Then your conditional could be setup as along the lines of

if id = contactpageid# then print the css.

🙂

Viewing 5 posts - 1 through 5 (of 5 total)
The forum ‘Newsmag’ is closed to new topics and replies.