Skip to content

Instantly share code, notes, and snippets.

@leogopal
Last active March 1, 2018 07:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leogopal/2eb5008f33ccb322edf2146d43bec94a to your computer and use it in GitHub Desktop.
Save leogopal/2eb5008f33ccb322edf2146d43bec94a to your computer and use it in GitHub Desktop.

Note that you will not see the encoded email address in the inspector but it will display as encoded in the source code, view source will display it appropriately.

<?php
/**
* Shortcode to wrap an email in the antispambot() function.
* returns the encoded email address with option as working mailto link.
*
* Usage: [emailantispam is_link="true"]john@example.com[/emailantispam]
* is_link default is false.
**/
function email_antispambot_link_shortcode( $atts , $content = null ) {
if ( ! is_email( $content ) ) {
return;
}
// Options
$options = shortcode_atts(
[
'is_link' => 'false',
],
$atts
);
$email = antispambot( $content );
if ($options['is_link'] === 'false') {
return esc_html( $email );
} else if ($options['is_link'] === 'true') {
$email_link = sprintf( 'mailto:%s', $email );
return sprintf( '<a href="%s">%s</a>', esc_url( $email_link, array( 'mailto' ) ), esc_html( $email ) );
}
}
add_shortcode( 'emailantispam', 'email_antispambot_shortcode' );
<?php
/**
* Shortcode to wrap an email in the antispambot() function.
* returns only the encoded email address.
*
* Usage: [emailantispam]john@example.com[/emailantispam]
**/
function email_antispambot_shortcode( $atts , $content = null ) {
if ( ! is_email( $content ) ) {
return;
}
return esc_html( antispambot( $content ) );
}
add_shortcode( 'emailantispam', 'email_antispambot_shortcode' );
/**
* Shortcode to wrap an email in the antispambot() function.
* returns the encoded email address as a working mailto link.
*
* Usage: [emailantispamlink]john@example.com[/emailantispamlink]
**/
function email_antispambot_link_shortcode( $atts , $content = null ) {
if ( ! is_email( $content ) ) {
return;
}
$content = antispambot( $content );
$email_link = sprintf( 'mailto:%s', $content );
return sprintf( '<a href="%s">%s</a>', esc_url( $email_link, array( 'mailto' ) ), esc_html( $content ) );
}
add_shortcode( 'emailantispamlink', 'email_antispambot_link_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment