Skip to content

Instantly share code, notes, and snippets.

@aldolat
Last active February 5, 2017 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aldolat/6e234d0baf479f5c6775 to your computer and use it in GitHub Desktop.
Save aldolat/6e234d0baf479f5c6775 to your computer and use it in GitHub Desktop.
This shortcodes for WordPress hides email addresses from spam bot.
<?php
/**
* Hides email addresses from spam bot.
*
* @param array $atts {
* The various options. The only required is "address".
*
* @type string $type The type of protocol to be used.
* Default: 'email'.
* Accepts: 'email', 'jabber'.
* @type string $address The address to be used (required).
* @type string $class The CSS class for the HTML element.
* @type string $subject The default subject for the email when the user clicks on the link.
* @type string $text The text to be linked with the email/Jabber address.
* #type boolean $link If the HTML element should be a link or a span element.
* }
* @example [email address="name@example.com"]
* @return string The formatted HTML email link or span.
*/
function ubnsc_email( $atts ) {
extract( shortcode_atts( array(
'type' => 'email',
'address' => '',
'class' => '',
'subject' => '',
'text' => '',
'link' => 1
), $atts ) );
switch ( $type ) {
case 'email' : $protocol = 'mailto:'; break;
case 'jabber' : $protocol = 'xmpp:'; break;
}
$address = sanitize_email( $address );
if ( $class ) $class = ' class="' . sanitize_html_class( esc_attr( $class ) ) . '"';
if ( $subject ) $subject = '?subject=' . esc_attr( $subject );
/*
Do not escape the last $text variable because it can contain an HTML image:
$text = empty( $text ) ? $text = antispambot( $address ) : esc_html( $text );
*/
$text = empty( $text ) ? $text = antispambot( $address ) : $text;
if ( 1 == $link ) {
$output = '<a' . $class . ' href="' . $protocol . antispambot( $address, 1 ) . $subject . '">' . $text . '</a>';
} else {
$output = '<span' . $class . '>' . $text . '</span>';
}
return $output;
}
add_shortcode('email', 'ubnsc_email');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment