Skip to content

Instantly share code, notes, and snippets.

@thefuxia
Last active March 21, 2021 20:59
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save thefuxia/1584783 to your computer and use it in GitHub Desktop.
Save thefuxia/1584783 to your computer and use it in GitHub Desktop.
Plugin: Defer Contact Form 7 Scripts
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: Defer Contact Form 7 Scripts
* Description: Adds <code>defer='defer'</code> to enqueued javascripts.
* Version: 1.0
* Required: 3.3
* Author: Fuxia Scholz
* Author URI: https://fuxia.me
* License: GPL
* License URI: http://www.gnu.org/copyleft/gpl.html
*
*
* Defer Scripts, Copyright (C) 2011 Thomas Scholz
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
if ( ! function_exists( 'add_defer_to_cf7' ) )
{
function add_defer_to_cf7( $url )
{
if ( // comment the following line out add 'defer' to all scripts
FALSE === strpos( $url, 'contact-form-7' ) or
FALSE === strpos( $url, '.js' )
)
{ // not our file
return $url;
}
// Must be a ', not "!
return "$url' defer='defer";
}
add_filter( 'clean_url', 'add_defer_to_cf7', 11, 1 );
}
@nirodhasoftware
Copy link

Can you please specify which file/function this goes into? Thanks!

Copy link

ghost commented Oct 19, 2014

can we add async instead of defer? I tried adding it but it is placed at the end of the <script tag, It needs to be placed at the beginning to be effective

Copy link

ghost commented Oct 19, 2014

also this does not seem to work if i use esc_url, clean_url has been deprecated

@bustapaladin
Copy link

How to install this?

@diggy
Copy link

diggy commented Jun 20, 2016

As of WP 4.1+ you can use the script_loader_tag filter hook, see Toscho's answer @ http://wordpress.stackexchange.com/a/38335/25765

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment