Home Forums General Issues Email obfuscation


Email obfuscation

  • Hi there,

    Can you tell me how to obfuscate this email entry (with exact code)? Preferably using WordPress’s default antispambot function.

    <div class="staff-email">Email:<?php the_sub_field ('staff_email_address'); ?></div>

    Wordpress Codex:

    And if not with default wordpress function can you make this work? using the Cryptx Plugin?
    <?php echo get_encryptx_meta($post->ID, $key, true); ?>

  • Untested but this should work:

    <div class="staff-email">Email:
          echo antispambot(the_sub_field('staff_email_address'));
  • Nope 🙁
    what about something like..

    Email:<a href="mailto:<?php echo get_encryptx_meta (the_sub_field('staff_email_address')); ?>"> <?php the_sub_field('staff_name'); ?></a>

  • oops, sorry about that. “Untested” 🙂 Try this, also untested!

    <div class="staff-email">Email:
        <a href="mailto:<?php echo antispambot(the_sub_field('staff_email_address')); ?>">
    <?php echo antispambot(the_sub_field('staff_email_address')); ?>

    If that’s working, make sure that the_sub_field('staff_email_address') is actually outputting an email address.

  • Hi Will,

    Thanks for the efforts. The page renders correctly for the visitors. However, if I view source the email address is clearly visible as plain text. I assume that should not be the case.


  • tl;dr Forum isn’t having any part of displaying the encoded text as anything but output. Save the email address as a variable first and then pass it to antispambot to make all of you obfuscation dreams come true. If you view source on this forum post and search for “foo” you can see it in action.

    Interesting. I’m not sure why but I see a way around it. Perhaps @elliot can explain the discrepancy.

    Note: the_field("test_email") was saved as "[email protected]"
    <h2> The Email: <?php echo antispambot( "[email protected]" ) ?> </h2>
    <h2> The Email: <?php echo antispambot( the_field("test_email") ) ?> </h2>
    <h2> The Email: </h2>
    <h2> The Email: [email protected] </h2>

    THIS CODE: (Save as variable first)
    <h2> The Email: <?php echo antispambot( "[email protected]" ) ?> </h2>
    <?php $email = get_field("test_email"); ?>
    <h2> The Email: <?php echo antispambot( $email ) ?> </h2>

    OUTPUTS: What you (we!) were expecting:
    <h2> The Email: </h2>
    EDIT: Can’t get it to display properly in the forum but this email address is obfuscated.
    <h2> The Email: </h2>

    edited for clarity.

  • That is clear as mud to me. 🙁
    Also, I am dealing with repeater fields so I believe I need to use get_sub_field

  • @jeffreyd00 Here, with less noise:

        $theEmail = get_sub_field('staff_email_address');
        <a href="mailto:<?php echo antispambot( $theEmail ) ?>">
           <?php the_sub_field('staff_name'); ?>
  • Hi Will, That is def. clearer though I am getting a parse error on this line.
    <a href="mailto:<?php echo antispambot( $theEmail ) ?>">

  • Solved with this:

    <?php $theEmail = get_sub_field('staff_email_address'); ?>
    <a href="mailto:<?php echo antispambot( $theEmail ) ?>">
            <?php the_sub_field('staff_name'); ?>
  • oops, forgot the closing ?> on that first line.

  • it’s all good, i really thank you for helping me with it. truly.

  • Hello,

    i did try the solutions you explained -and even some encoding plugins- but cannot get it working using a mailto template that include subject line and body, someone could please help me to get it working this way:

    <a title=click here to open the email" href="mailto:<?php the_field('emailforrequest'); ?>?subject=the subject line&Body=my body content that may call other php function such as <?php the_title(); ?>"><img class="imgclass" title="imgtitle" alt="imgalt" src="imgsource.png" width="000" height="000" /></a>

    Where ‘<?php the_field(’emailforrequest’); ?>’ is my php code to call the custom field ’emailforrequest’ that contains the email address, it is different email address for each post.

    Thanks ahead for your time

  • Hi I’ve been trying all day to get this to work, but no dice. Here is my code, but it always displays the plaintext email in HTML.

    $email = get_field('pro_email');
    <a href="mailto:' .  antispambot(get_field('pro_email')) . '">'. get_field('pro_email') .'</a>

    Any help would be greatly appreciated!

  • @ttwebco

    I am by far an expert have you tried something like this?

    <?php $theEmail = get_field('pro_email'); ?>
    <a href="mailto:<?php echo antispambot( $theEmail ) ?>">
    Email Me
  • Sadly changing the link text doesn’t make any difference.

    <a href="mailto:'.antispambot($email).'">email me</a></p>

    Same problem. Unless there was another point in your reply which I am missing (I am VERY far from an expert 😉

  • Sigh, that was indeed my point.
    How about not using the reserved $email and instead using $theEmail like I did?

    Just copy my code and try it at least. =)

    Past that you’ll need to wait and see if some other good soul will help you.

  • Thanks, tried it exactly as you had it. Nothing. 🙁

  • I’ll post the my full code here in hopes that seeing all of it might reveal some issues. I am extremely new to PHP so this may look like a train wreck. (I use the genesis framework).

     * Template Name: Professional Single
     //* Adding ACF on prefessionals pages 
    add_action( 'genesis_before_entry_content', 'rma_pro_single_acf' );
    function rma_pro_single_acf() {
    // If fields are present
    if( get_field('headshot') ||
    	get_field('pro_role') ||
    	get_field('pro_office_phone') ||
    	get_field('pro_cell_phone') ||
    	get_field('pro_email') ||
    $theEmail = get_field('pro_email');
    '<div class="professional-details clearfix">
    			<div class="one-third first">
    				<img src="' . get_field('headshot') . '">
    			<div class="two-thirds contact-details">
    				<h6>' . get_field('pro_role') . '</h6>
    				<p><span class="data">Office:</span>' . get_field('pro_office_phone') . '</p>
    				<p><span class="data">Mobile:</span>' . get_field('pro_cell_phone') . '</p>
    				<p><span class="data">Email:</span><a href="mailto:'.antispambot($theEmail).'">' . $theEmail . '</a></p>
    				<p><span class="data">Connect:</span><a href="' . get_field('pro_linkedin') . '"><i class="fa fa-linkedin-square fa-lg"></i> Linkedin</a></p>
  • as a test, did you try removing all that and just adding the test code i provided?

  • Not to highjack the thread but this is what I am trying to do as well. I have a field head_coach_email and am trying to get a mailto link to display on the front end. It’s not working. Can someone please help me? I am pretty much a php beginner.

  • @ericaeide if you read the thread and are still unable to get it to work you’ll need to post the code you have so we can help.

  • For anyone else reading this, what Jefferey said here works:

    <?php $theEmail = get_field('pro_email'); ?>
    <a href="mailto:<?php echo antispambot( $theEmail ) ?>">
    Email Me
Viewing 23 posts - 1 through 23 (of 23 total)

The topic ‘Email obfuscation’ is closed to new replies.