In its default settings, Contact Form 7 loads its JavaScript and CSS stylesheet on every page. You might think it would be redundant or wasteful, and want to load them only on those pages that contain contact forms. I understand the feeling, but there is a technical difficulty for a plugin in knowing whether the page contains contact forms or not at the start of loading. However, I can show you a way to work around that.

默认的,Contact Form 7 在每个页面载入他的 javascript 和 CSS。你或许认为这是多余的,浪费,你想在有表单的页面才载入。我能理解这种感受,但是在插件里很难找到页面里是否有表单在载入的时候。但是,我可以告诉你一个办法解决。

Step 1: Stop loading the JavaScript and CSS stylesheet on all pages

第一步:在所有页面停止载入 javascript 和 CSS。

When the value of WPCF7_LOAD_JS is set to false (default: true), Contact Form 7 does not load the JavaScript. You can set the value of this constant in your wp-config.php like this:

当 WPCF7_LOAD_JS 的值设置为 false (默认:true),Contact Form 7 就不会加载 Javascript, 你可以在 wp-config.php 里设置这个静态变量:

define('WPCF7_LOAD_JS', false);

Likewise, you can control the loading of the CSS stylesheet with WPCF7_LOAD_CSS. Contact Form 7 does not load the CSS stylesheet when the value of WPCF7_LOAD_CSS is false (default: true). You can set it in the wp-config.php like this:

同样的,你可以用 WPCF7_LOAD_CSS 控制 CSS 的载入。当 WPCF7_LOAD_CSS 设置为 false (默认: true) 时,Contact Form 7就不会载入 CSS 样式表。你可以在 wp-config.php 里设置这个静态变量:

define('WPCF7_LOAD_CSS', false);

Now you have succeeded in stopping the loading of the JavaScript and CSS stylesheet, but, unfortunately, you’ve also killed them on pages that contain contact forms — so we really need to get them back on the right pages. So, the next step is what you’ll need to load the files on to the explicit pages in which you need them.

Step 2: Load the files on pages which contain contact forms

For example, let’s say you have a page named “Contact” and it is the only page that contains a contact form. And suppose that you have a template file for the “Contact” page named ‘contact.php’ in your theme folder. Now you will need to load Contact Form 7′s JavaScript and CSS stylesheet specifically on the “Contact” page.

To do this, you must edit the ‘contact.php’ template file and insert the following lines into it:

	if ( function_exists( 'wpcf7_enqueue_scripts' ) ) {

Note that wpcf7_enqueue_scripts() and wpcf7_enqueue_styles() must be called before wp_head() is called.

3,744 thoughts on “在需要的时候加载Javascript 和 CSS (Contact form 7)

  1. Movie Terbaru

    Today, I went to the beach with my kids.
    I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She
    put the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear.

    She never wants to go back! LoL I know this is totally off topic but
    I had to tell someone!

  2. Arianne

    Hi there, i read your blog occasionally and i own a similar one and
    i was just curious if you get a lot of spoam feedback?
    If so hhow do yyou reduce it, any plugin or anyrhing you can recommend?

    I get so much lately it’s driving mee mad so any assistance
    is verdy much appreciated.

  3. Best Webcams

    It’s actually very complex in this full of activity life to listen news on Television, so I simply use thhe web for that reason, and obtain the hottest information.

  4. Traffic Exchange PHP Script

    Great post. I was checking continuously this blog and I’m
    impressed! Very useful information specifically the
    last part 🙂 I care for such information a lot. I was seeking this
    particular information for a long time. Thank you and good luck.

  5. dewamovie

    Hey! I just wanted to ask if you ever have any trouble with hackers?
    My last blog (wordpress) was hacked and I ended up losing
    many months of hard work due to no data backup.
    Do you have any methods to stop hackers?

  6. cheap jerseys

    cheap jerseys
    Burrell, Nowell, Farrell it all added up to Scottish hell.
    The English substitutions that caused some pundits angst in Paris were easy to make
    here. True, there was a repeat bungle at an attacking line out thrown to by Dylan Hartley’s replacement Tom Youngs,
    but overall the red rose was is in clover

    cheap jerseys She could do a lot of things, so we really had to prepare for her.
    It was an era in the IAAM that I’ll never forget and I’ll treasure, because that was a time we really had to fight tooth and nail to beat them.

    It came as a shock to us that the school was closing. cheap jerseys

    cheap nfl jerseys With all the interest in Jarryd Hayne in the US, David Pocock thinks rugby union has an opportunity to jump on the bandwagon ahead of the Wallabies’ historic Test against the USA Eagles.
    “As an athlete, it’s pretty amazing to see what he’s doing and we’ve all been watching with interest.” Pocock notedrugby union was one of the fastest growing sports in the
    US, and with the general inability of the Americans to differentiate from Hayne’s rugby league, there could be some added interest in the Wallabies
    Testand the sport itself. “With Sevens in the Olympics now, there’s a lot more funding and I think it’s the kind of game that if the Americans decide to take an interest and got their heads around it, they could be a real powerhouse,” Pocock said..
    cheap nfl jerseys

    Cheap Jerseys from china Draw two vertical lines from the end of each horizontal line
    each line should extend downward half an inch.
    Draw a curved line curving upward from the end of one vertical line to the end of the other.
    You have just completed the outline for the head and shoulders..
    Cheap Jerseys from china

    Filling: 1) Peel the potatoes and chop them into 1 inch pieces.
    Place in a saucepan, cover with water, and boil until very soft.
    Drain and transfer to a medium sized bowl. Joseph A.