WordPress topic creation Tips 2 [actively add anti-spam Code]

Source: Internet
Author: User

Like many park friends, my independent blog Based on WordPress was also plagued by spam messages at the beginning, and I also used several anti-spam plug-ins including akismet and some Chinese please, however, I do not want to see a lot of plug-ins in the background, no matter how fast the blog is loaded or the stability of version updates is not good. Later, I went online to find a method that does not use plug-ins, after comprehensive modification, I have built a simple and practical message verification function on my website, which has been used for a long time. If you don't like plug-ins, you can check them out.

This method uses the rand () function to generate two natural numbers. After the comments input the sum of the two, the webpage uses JSCodeIt is similar to PHP code to verify the input results. The principle is as simple as none, but it is stable and practical. The user experience is smooth, and the effect of Blocking spam messages is also good.

First, open comments. php In the template file, and insert a verification entry (I inserted it above the submit button) where you think it is appropriate in the comment submission form. The Code is as follows:

<? PHP If (! $ User_id ):?> <? PHP $ Lt_sub_a = Rand (0, 5 ); $ Lt_sub_ B = Rand (3, 9);?> <P> <input type = "text" size = "17" name = "lt_sub_ AB" id = "lt_sub_ AB" tabindex = "4"/>=<? PHP Echo   $ Lt_sub_a ;?> + <? PHP Echo   $ Lt_sub_ B ;?> <Label For = "Check"> & nbsp; complete the equation * </label> <input name = "lt_sub_a" id = "lt_sub_a" value = "<? PHP echo $ Lt_sub_a ;?> "Type =" hidden "/> <input name =" lt_sub_ B "id =" lt_sub_ B "value =" <? PHP echo $ Lt_sub_ B ;?> "Type =" hidden "/> </P> <? PHP Endif ;?>

It's the message form on my blog.

Then, the verification code entered by the message holder in the form is verified. To make the verification process complete on the current page to achieve quick and direct results, you need to perform JS verification in comments. php. The Code is as follows (here I only paste the Code related to the verification code ):

 
FunctionTrim (STR ){//Delete spaces at both endsReturnStr. Replace (/(^ \ s *) | (\ s * $)/g ,"");}FunctionValidate_form (thisform ){With(Thisform ){If(Validate_checknum (lt_sub_ AB, "Enter the sum of the two numbers correctly...") =False) {Email. Focus ();Return False}}}

Remember to add the verification function (onsubmit = "Return validate_form (this);") to the onsubmit attribute of the form );").

In this way, I can use it, but I am still not at ease. In the current network environment, Some browsers will impose some restrictions on JS execution. For the stability of the verification process and user experienceProgramA layer of verification is added. This verification only appears when the JS verification is restricted.

Open the comments submission page/wp-comments-post.php under the WordPress program root directory, find

 
If(''=$ Comment_content) Wp_die (__('Error: Please type A comment .'));

Insert the following code before the Code. (to avoid modifying the program, you can also use hooks to insert the Code. For simplicity, this article will not elaborate ).

 If (! $ User -> ID ){  $ Lt_sub_a = Trim ( $ _ Post  [Lt_sub_a]);  $ Lt_sub_ B = Trim ( $ _ Post  [Lt_sub_ B]);  $ Lt_sub_ AB = Trim ( $ _ Post  [Lt_sub_ AB]);  If ((( $ Lt_sub_a + $ Lt_sub_ B )! = $ Lt_sub_ AB ) |Empty ( $ Lt_sub_ AB  ) {Wp_die (__( 'Error: Enter the sum of the two numbers ...' ));}} 

OK. All are completed.

View the demo on my blog?Yes./No.(My message form is hidden by default. You need to click the comments link at the bottom of the page to expand it)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.