Skip to content

Instantly share code, notes, and snippets.

@jkudish
Forked from robertdall/emailshortcode.php
Created December 7, 2012 17:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jkudish/4235056 to your computer and use it in GitHub Desktop.
Save jkudish/4235056 to your computer and use it in GitHub Desktop.
Email Short Code
<?php
// Hide Email from Spam Bots
function cwc_mail_shortcode( $atts , $content = null ) {
if ( ! is_email( $content ) )
return;
for ( $i = 0; $i < strlen( $content ); $i++ )
$encodedmail .= "&#" . ord( $content[$i] ) . ';';
return '<a href="' . esc_url( 'mailto:' . $encodedmail ) . '">Email</a>';
}
add_shortcode( 'email', 'cwc_mail_shortcode' );
@jkudish
Copy link
Author

jkudish commented Dec 7, 2012

Improved @robertdall's shortcode by:

  • using WordPress coding standards
  • removing the image in the link, that seemed very specific to the Robert's use case and not general use
  • adding validation that the passed string in $content is an email address, and if not, just return out of the function, avoiding potential issues if it wasn't an email address
  • added proper output escaping to the link, for security reasons

@robertdall
Copy link

Sorry I forgot to remove the image links when I uploaded it last night :-) That's what I get for multi-tasking and copy and pasting at the meetup… 

@chrismccoy
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment