Last active
February 5, 2017 20:36
-
-
Save aldolat/6e234d0baf479f5c6775 to your computer and use it in GitHub Desktop.
This shortcodes for WordPress hides email addresses from spam bot.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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="[email protected]"] | |
* @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