ThemeShaper Forums » Thematic

Optimize Contact Form 7 Plugin [functions.php]

(2 posts)
  • Started 13 years ago by Baudesign
  • Latest reply from Baudesign
  • This topic is resolved
  1. Baudesign
    Member

    Hello everyone,

    I came upon a great article that describes how to optimize your blog that uses the Contact Form 7 plugin: http://coding.cglounge.com/2009/06/optimize-style-contact-form-7-wordpress/

    The idea of reducing the servers' requests, and the idea of using my own stylesheet is quite appealing. Especially that I use a dark blue background in my child theme.

    But for some unkown reason, the code suggested:

    /**
     * Functions:	Optimize and style Contact Form 7 - WPCF7
     *
     */
    // Remove the default Contact Form 7 Stylesheet
    function remove_wpcf7_stylesheet() {
    	remove_action( 'wp_head', 'wpcf7_wp_head' );
    }
    
    // Add the Contact Form 7 scripts on selected pages
    function add_wpcf7_scripts() {
    	if ( is_page('contact') )
    		wpcf7_enqueue_scripts();
    }
    
    // Change the URL to the ajax-loader image
    function change_wpcf7_ajax_loader($content) {
    	if ( is_page('contact') ) {
    		$string = $content;
    		$pattern = '/(<img class="ajax-loader" style="visibility: hidden;" alt="ajax loader" src=")(.*)(" \/>)/i';
    		$replacement = "$1".get_template_directory_uri()."/images/ajax-loader.gif$3";
    		$content =  preg_replace($pattern, $replacement, $string);
    	}
    	return $content;
    }
    
    // If the Contact Form 7 Exists, do the tweaks
    if ( function_exists('wpcf7_contact_form') ) {
    	if ( ! is_admin() && WPCF7_LOAD_JS )
    		remove_action( 'init', 'wpcf7_enqueue_scripts' );
    
    	add_action( 'wp', 'add_wpcf7_scripts' );
    	add_action( 'init' , 'remove_wpcf7_stylesheet' );
    	add_filter( 'the_content', 'change_wpcf7_ajax_loader', 100 );
    }

    ...does not work at all. And i checked, double checked that the form is indeed in the "contact" page, which is the case.

    Can we improve on this code? What am I doing wrong?

    I use WordPress 2.8.4 and CF7 2.0.5

    Thanks

    Pat

    Posted 13 years ago #
  2. Baudesign
    Member

    Ok, after pulling my hair for many hours, Here is the solution I found, thanks to Justin Tadlock http://justintadlock.com/archives/2009/08/06/how-to-disable-scripts-and-styles

    /*
     * Functions:	Optimize Contact Form 7
     *
     */
    
    function deregister_cf7_js() {
       if ( !is_page('contact') ) {
    	wp_deregister_script( 'contact-form-7' );
         }
    }
    add_action( 'wp_print_scripts', 'deregister_cf7_js', 100 );
    
    function deregister_ct7_styles() {
    	wp_deregister_style( 'contact-form-7' );
    }
    add_action( 'wp_print_styles', 'deregister_ct7_styles', 100 );

    It does most of what I was looking for.

    Now, I only need to figure out how to change the ajax-loader.gif icon to match my dark blue background.

    Pat

    Posted 13 years ago #

RSS feed for this topic

Reply

You must log in to post.