2

I have a custom field in a theme that contains an email address and was pleased to find antispambot()

<p><a href="mailto:<?php antispambot(the_field('queries_e-mail')); ?>"> <?php echo antispambot(the_field('queries_e-mail'));?></a></p>

It looks fine and works as intended (ie. it opens a compose mail window) but when I view the code source it doesn't appear to be encoded.

<p><a href="mailto:john.doe@mysite.com"> john.doe@mysite.com</a></p>

According to the Codex the default usage is :

<?php echo antispambot("john.doe@mysite.com"); ?>

which should output:

&#106;&#111;h&#110;&#46;&#100;&#111;&#101;&#64;mysit&#101;.&#99;&#111;&#109;

However the encoding doesn't appear to be working in my case.

  • I'm curious how effective this method actually is. I realize that it's a couple more steps to decode the email address on a page, but a spam bot empowered with some regular expressions tied to popular validation patterns could circumvent this whole function and implementation altogether. The likelihood and nature of spam bots wouldn't put this out of the realm of possibility. – Mike Kormendy Oct 20 '15 at 15:09
  • I usually only use this when a client insists on having the email visible on the front end of the site but I haven't had requests to remove the emails so it must work to some extent. – mantis Oct 20 '15 at 21:20
2

I think the problem is echo vs return,

i.e. that you are using the_field instead of get_field.

What you're trying to do is similar to this:

 antispambot( echo( 'john.doe@mysite.com' ) )

but echo returns nothing.

Please try:

<?php $email = antispambot( get_field( 'queries_e-mail' ) ); ?>

<p>
    <a href="mailto:<?php echo $email; ?>"> 
        <?php echo $email; ?>
    </a>
</p>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.