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.
    Fiorenza, President, National Conference of Catholic Bishops, 1999.
    Well, there are numerous reasons against capital punishment.

    Cheap Jerseys china I’ve been in Sydney seven years now and I have really adapted to the local lifestyle.
    I’ve been away from Ireland so long, I think if I moved back things would
    be very different. A lot of my friends have moved on, married and started having children. Cheap Jerseys china

    Men wear shorts and tank tops that match in style and color,
    although they do not have to be an actual uniform for play in USA Volleyball.
    Generally, players go barefoot, but the referee may allow socks when the
    sand is burning hot. You need comfortable clothing that will not
    interfere with your movements.

    wholesale jerseys Turning this feature ON, your camera
    device will analyze the image while it has been shot, to check for any pixels that are incorrectly rendered to fix them up.
    This process may take some time, usually as long
    as the exposure time. So, if you have time, you can do it even on long exposures to improve image quality..
    wholesale jerseys

    Cheap Jerseys from china His achievements put him on collision course with Boardman, who within five days of Obree’s hour
    record had set a new one himself. Now it is back with Obree, and the rivalry is growing.
    The two of them will be in opposition again in the world championship in Sicily
    in a fortnight.. Cheap Jerseys from china

    Cheap Jerseys china Having only recently emerged from the Feudal Age, indentured servitude was a standard practice across Europe.
    Many of the first American settlers secured passage aboard
    ships bound for the New World by indenturing themselves
    to landowners and businessmen. In the 16th and 17th Centuries, slavery
    was a common world wide practice. Cheap Jerseys china

    wholesale nfl jerseys Just to be somewhat safer Big
    Brother is everywhere, no getting around that you might want to disable the feature manually.
    On Vizio televisions, open the TV’s Menu option and click on System.

    Go to Reset Admin, then click on Smart Interactivity.
    wholesale nfl jerseys

    Cheap Jerseys free shipping PESCA: That was hardly an option for a black player in the 1940s.
    Tunnell, who grew up in an integrated neighborhood near
    Philadelphia, attended the University of Toledo and then Iowa, with a stint in the Coast
    Guard in between. Rams. Like all creative arts, photography does require practice
    on our part. First, you need to sharpen your senses to the world around you,
    notice small details, motion, colors, and shadows.
    Secondly, you need to sharpen your skills required so
    that the basic camera controls of your camera become second hand nature to you Cheap Jerseys free shipping.

  7. ngo aangan

    Nice post. I learn something totally new and challenging on blogs I stumbleupon every day.
    It’s always useful to read through content
    from other authors and practice something from their websites.

  8. Ai beauty clinic review

    Excellent pieces. Keep writing such kind of information on your page.

    Im really impressed by your blog.
    Hey there, You’ve performed a fantastic job. I will certainly digg it and in my view recommend to
    my friends. I am confident they will be benefited from this site.

  9. poker idn deposit 10rb

    “Sometimes giving simply your schedule can really generate somebody else smile,” according to
    the website. Internet marketing is cheaper than printed ads, radio or TV.
    Use online follow-up methods including email and be happy to use direct mail.

  10. Http://Banecompany.Com/

    Just want to say your article is as astonishing.
    The clearness in your post is simply spectacular and i can assume you’re an expert on this subject.
    Well with your permission allow me to grab your RSS feed to keep up
    to date with forthcoming post. Thanks a million and please continue the enjoyable work.

  11. ai beauty

    It is appropriate time to make some plans for the future and it’s
    time to be happy. I’ve read this post and if I could I
    wish to suggest you few interesting things or tips. Maybe you could
    write next articles referring to this article.
    I wish to read more things about it!

  12. Vorstellungsgespräch

    My brother recommended I would possibly like this website. He was entirely right.
    This publish truly made my day. You cann’t consider just how much time I had spent for this
    information! Thank you!

  13. Lace Wigs

    What I have noticed is it is so easy for these doctors and
    psychologists to say it is ADD or this or that. And maybe it
    is. But this is frustrating to me as a sufferer.