Remove Contact Form 7 CSS and JS Unless Contact form 7 shortcode is used in the page

Remove Contact Form 7 CSS and JS Unless Contact form 7 shortcode is used in the page
Question :
Want to show the css and javascript only when the shortcode is used in that page. If the short code not present in the wordpress page then the js and css of contact form should not be shown.
Solution code for : Remove Contact Form 7 CSS and JS Unless Contact form 7 shortcode is used in the page
Here is the answer for your question. If there is not shortcode the css and js of contact form will be removed and if there is shortcode css and js will be added.
function aft_lwp_contactform_css_js() {
    global $post;
    if( is_a( $post, 'WP_Post' ) && has_shortcode( $post->post_content, 'contact-form-7') ) {
        wp_enqueue_script('contact-form-7');
         wp_enqueue_style('contact-form-7');

    }else{
        wp_dequeue_script( 'contact-form-7' );
        wp_dequeue_style( 'contact-form-7' );
    }
}
add_action( 'wp_enqueue_scripts', 'aft_lwp_contactform_css_js');

No comments:

Post a Comment