How to add antispambot function using shortcode

PHP Code Snippets/Functions

In this tutorial, you will learn how to add anti-spam bot in your website.

  1. Create a shortcode.php in your ftp and include it in our functions.php.
  2. In your shortcode.ph add this code.
function sm_antispam_script_sc($atts, $content){
	extract( shortcode_atts( array(
		"email" => ''
	), $atts));

	ob_start();
		?>
			
		<?php
		$email = antispambot($email);
		return '<a href="mailto:' . $email . '">' . $email . '</a>';

		// return do_shortcode( ob_get_clean() );
	}
add_shortcode('sm_antispam_script', 'sm_antispam_script_sc');

note: by adding this code we will then have this shortcode [sm_antispam_script email=”youremail@gmail.com”]

  1. After creating your shortcode, go to your post/page where you displayed your email and replace it with shortcode block.
  2. Then in your shortcode black add this shortcode [sm_antispam_script email=”youremail@gmail.com”] and then click save then you are done.

Here is how you can check if the code above is working.

  1. Go to the page where you added your shortcode.
  2. In your keyboard click CTRL+U
  3. Find the code of your email and you will see that your email has been converted to a random character to prevent spam. (see screenshot below)

That is the end of the tutorial. If you have any questions, just contact the author of this post.

Leave a Reply

Your email address will not be published. Required fields are marked *