Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Contributor Addons for Contact Form 7

    (@contactform7addons)

    Hi @usat009

    You can add this to your functions.php. It will dequeue all the plugin scripts and styles except on pages with slug ‘contact’ or ‘form’. You will need to change the if condition to suit your pages.

    function my_dequeue_cf7md_assets() {
        if( ! is_page( 'contact' ) && ! is_page( 'form' ) ) {
            wp_dequeue_script( 'md-components-js' );
            wp_dequeue_script( 'autosize' );
            wp_dequeue_script( 'cf7-material-design' );
            wp_dequeue_style( 'cf7md_roboto' );
            wp_dequeue_style( 'cf7-material-design' );
        }
    }
    add_action( 'wp_enqueue_scripts', 'my_dequeue_cf7md_assets', 20 );

    Thanks,
    Angus

    Thread Starter usat009

    (@usat009)

    there’s no code that looks for if the CF7 shortcode exists on a page?
    I don’t have a single contact page or a single contact form.
    I have contact forms all over the place.
    For example, I have a contact form in the sidebar of all blog posts.
    and a contact form embedded as a pop-up in all product pages.

    I’m looking for something that says “if CF7 shortcode exists, then run script”.

    • This reply was modified 4 years, 5 months ago by usat009.
    Plugin Author GusRuss89

    (@gusruss89)

    Unfortunately no there’s not, at least not that I’m aware of. If there was a way to do that I’d have done it by default in the plugin itself.

    You might be better off opting specific pages in to this function rather than all but the contact page. E.g. if ( is_page( 'about' ) || is_page( 'pricing' ))

    Thanks,
    Angus

    Thread Starter usat009

    (@usat009)

    is_page doesn’t affect posts correct? and not product pages from woocommerce either right? If it’s only standard type = page, then yes, I think it would be best for me to use a script that just opts out specific pages, that could suit my purposes pretty well.

    If yes on both my initial questions, how can I specific our homepage that doesn’t have a slug?

    • This reply was modified 4 years, 5 months ago by usat009.
    Plugin Author GusRuss89

    (@gusruss89)

    You have total control and creativity over the if condition. If you’re not confident with PHP and WordPress functions, feel free to describe what you want here, and I’ll write the condition for you. For specific pages, the ID is probably the best way to target them.

    Thanks,
    Angus

    Thread Starter usat009

    (@usat009)

    Yes, i suck at that. Can you write one that excludes these three post ID’s?

    Post ID’s 7, 373, 539

    I should be able to modify it and add more on my own from there.

    Plugin Contributor Addons for Contact Form 7

    (@contactform7addons)

    Hi @usat009

    No worries. Here you go.

    
    function my_dequeue_cf7md_assets() {
        if( in_array( get_queried_object_id(), [7, 373, 539] ) ) {
            wp_dequeue_script( 'md-components-js' );
            wp_dequeue_script( 'autosize' );
            wp_dequeue_script( 'cf7-material-design' );
            wp_dequeue_style( 'cf7md_roboto' );
            wp_dequeue_style( 'cf7-material-design' );
        }
    }
    add_action( 'wp_enqueue_scripts', 'my_dequeue_cf7md_assets', 20 );
    

    This will dequeue the scripts and styles on posts with ID 7, 373 and 539.

    Thanks
    Angus

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Prevent Scripts from Loading on All Pages’ is closed to new replies.