Display reCAPTCHA on multiple specific pages Set up on both posted and fixed pages at the same time

ホームページ制作料金がもの凄く安い会社の実態ホームページ制作

You are creating a site with WP and you probably want to set up a contact form.

I think the most commonly used contact form is Contact Form 7. However, if you set up a contact form, you may start receiving a lot of spam mails.

By setting up “reCAPTCHA,” a service provided by Google, you can reduce the number of unsolicited e-mails at once.

Although reCAPTCHA is a very good system, it has a big problem. That is “the site display speed slows down considerably.

This is because reCAPTCHA-related tags are inserted into the source even on pages without a contact form.

Therefore, we wanted to make the reCAPTCHA appear only on pages with a contact form, so that the speed of other pages would not be affected.

Here’s a further problem.

I searched around and found instructions for setting up reCAPTCHA on “only post pages” or “only fixed pages,” but I could not easily find a way to set it up on “both post pages and fixed pages.

Note that functions.php is touched.

This time, the settings will be entered in “functions.php”.

If you know WP, you know that when you make any changes to functions.php, and especially when you do any validation, please make sure to back up your work.

If you make a mistake, an error will appear on the site, or the site itself will not display.

In case of an error

When we touch functions.php, we first use FTP software to bring the currently used functions.php from the server. In other words, we save it as a backup.

After that, we make another copy of it and touch the copied functions.php.

Both post pages and fixed pages

You can find a lot of codes to display reCAPTCHA “only on specific post pages” or “only on specific fixed pages,” so I will skip them here.

In this article, we will show you how to set it for “both post pages and fixed pages”.

add_action( 'wp_enqueue_scripts', function() {
if(is_page('Fill in the permalinks of the relevant fixed pages')) return;
if(is_single('Fill in the permalink of the relevant posting page')) return;
wp_deregister_script('google-recaptcha');
}, 20);

In other words, suppose the URL where you have a form set up on a fixed page is “https://abc.com/form/”. And suppose you also have a form set up on a post page called “https://abc.com/def/ghi/”.

Then the following will happen.

add_action( 'wp_enqueue_scripts', function() {
if(is_page('form')) return;
if(is_single('ghi')) return;
wp_deregister_script('google-recaptcha');
}, 20);

When we actually set it up in our company, we opened the source of the pages we set up and found that they had multiple tags related to recaptcha, but not a single one on the other pages.

Addition I checked and found a couple of things.

After doing some research, I found a website that provides a detailed introduction to the subject, which is presented below.

How to hide the reCAPTCHA v3 logo in Contact Form 7

Adding reCAPTCHA v3 to Contact Form 7 of WordPress to prevent spam

Since we have not verified this, it is always better to make backups before using the system.

I really want to display the query on every page.

Normally, we do not want to set up a reCAPTCHA.

The reason is that we want the form to appear on every page. I think it is good from a marketing point of view to have an entry point for inquiries from anywhere.

However, the large number of unsolicited e-mails is also a problem.

If you find a good solution in this area, I would like to introduce it again.

タイトルとURLをコピーしました