• Register Custom Shortcodes
  • $shortcode_tags
  • Enables [url] and [b] shortcodes in comments
  • Display Time - Limited Content, Obfuscate Email Addresses, Members Eyes Only

Register Custom Shortcodes

// Register a new shortcode: [book]
add_shortcode( 'book', 'ssd_sc1_book' );
 
// The callback function that will replace [book]
function ssd_sc1_book() {
    return 'http://www.amazon.com/dp/0470560541';
}
 
 
//shortcode with parameters
// Register a new shortcode: [books title="xxx"]
add_shortcode( 'books', 'ssd_sc2_multiple_books' );
 
// The callback function that will replace [books]
function ssd_sc2_multiple_books( $attr ) {
    switch( $attr['title'] ) {
        case 'xkcd':
            $isbn = '0615314465';
            $title = 'XKCD Volume 0';
            break;
 
        default:
        case 'prowp':
            $isbn = '0470560541';
            $title = 'Profesional WordPress';
            break;
    }
 
    return "<a href='http://www.amazon.com/dp/$isbn'>$title</a>";
}
 
/*The anchor text in the Amazon link will now be parameterized. this time with a second parameter $content , which will receive any enclosed text as a string. Replace [amazon isbn=”xxx”]book title[/amazon] */
// Register a new shortcode: [amazon isbn="123"]link title[/amazon]
add_shortcode( 'amazon', 'ssd_sc3_amazon' );
 
// Callback function for the [amazon] shortcode
function ssd_sc3_amazon( $attr, $content ) {
 
    // Get ISBN or set default
    if( isset( $attr['isbn'] ) ) {
        $isbn = preg_replace( '/[^\d]/', '', $attr['isbn'] );
    } else {
        $isbn = '0470560541';
    }
 
    // Sanitize content, or set default
    if( !empty( $content ) ) {
        $content = esc_html( $content );
    } else {
        if( $isbn == '0470560541' ) {
            $content = 'Professional WordPress';
        } else {
            $content = 'this book';
        }
    }
 
    return "<a href='http://www.amazon.com/dp/$isbn'>$content</a>";
}

Replace [amazonimage] with images from Amazon

<?php
/*
Plugin Name: Shortcode Example 4
Plugin URI: http://example.com/
Description: Replace [amazonimg] with images from Amazon
Version: 1.0
*/
 
// Register shortcodes [amazonimage] and [amazonimg]
add_shortcode( 'amazonimage', 'ssd_sc4_amazonimage' );
add_shortcode( 'amazonimg', 'ssd_sc4_amazonimage' );
 
// Callback function for the [amazonimage] shortcode
function ssd_sc4_amazonimage( $attr, $content ) {
 
    // Get ASIN or set default
    $possible = array( 'asin', 'isbn' );
    $asin = ssd_sc4_find( $possible, $attr, '0470560541' );
 
    // Get affiliate ID or set default
    $possible = array( 'aff', 'affiliate' );
    $aff = ssd_sc4_find( $possible, $attr, 'aff_id' );
 
    // Get image size if specified
    $possible = array( 'size', 'image', 'imagesize' );
    $size = ssd_sc4_find( $possible, $attr, '' );
 
    // Get type if specified
    if( isset( $attr['type'] ) ) {
        $type = strtolower( $attr['type'] );
        $type = ( $type == 'cd' or $type == 'disc' ) ? 'cd' : '';
    }
 
    // Now build the Amazon image URL
    $img = 'http://images.amazon.com/images/P/';
    $img .= $asin;
    // Image option: size
    if( $size ) {
        switch( $size ) {
            case 'small':
                $size = '_AA100';
                break;
            default:
            case 'medium':
                $size = '_AA175';
                break;
            case 'big':
            case 'large':
                $size = '_SCLZZZZZZZ';
        }
    }
    // Image option: type
    if( $type == 'cd' ) {
        $type = '_PF';
    }
    // Append options to image URL, if any
    if( $type or $size ) {
        $img .= '.01.'.$type.$size;
    }
    // Finish building the image URL
    $img .= '.jpg';
 
    // Now return the image
    return "<a href='http://www.amazon.com/dp/$asin'><img src='$img' /></a>";
}
 
// Helper function:
// Search $find_keys in array $in_array, return $default if not found
function ssd_sc4_find( $find_keys, $in_array, $default ) {
    foreach( $find_keys as $key ) {
        if( isset( $in_array[$key] ) )
            return $in_array[$key];
    }
    return $default;
}

$shortcode_tags

$shortcode_tags = array (wp_caption’ = > ‘img_caption_shortcode’,
‘caption’ = > ‘img_caption_shortcode’,
‘gallery’ = > ‘gallery_shortcode’,
‘embed’ = > ‘__return_false’,
‘amazonimage’ = > ‘ssd_sc4_amazonimage’,
‘amazonimg’ = > ‘ssd_sc4_amazonimage’,
)

A "bb code" for Comments Plugin You can now code a new plugin to enable BB - like tags in comments: Instead of using regular HTML tags such as < a > or < b > , commenters need to use [url] and [b] like in most forums. The plugin will also have the following traits: It should not change how authors write their posts (with HTML tags as usual). It should not apply to comments shortcodes otherwise egistered for posts, such as [amazonimage] in your previous plugin or [gallery] . Simple Start

/*
Plugin Name: Shortcode Example 5
Plugin URI: http://example.com/
Description: Recursive [b] and [i] shortcodes
Version: 1.0
*/
 
// add shortcodes [b] and [i]
add_shortcode( 'i', 'ssd_sc5_italic' );
add_shortcode( 'b', 'ssd_sc5_bold' );
 
// callback function: return bold text
function ssd_sc5_bold( $attr, $content ) {
    return "<strong>".do_shortcode( $content )."</strong>";
}
 
// callback function: return italic text
function ssd_sc5_italic( $attr, $content ) {
    return "<em>".do_shortcode( $content )."</em>";
}

ALGO 1. Plugin does not register new shortcodes [url] and [b] directly from the start; otherwise, they would interfere with the post contents. Instead, the plugin starts with capturing each comment’s contents. 2. The comment processing function, unregisters all shortcodes after making a copy of them. 3. New shortcodes are then registered: [url] and [b]. 4. The comment content, kept in the variable $comment , is expurgated from regular HTML tags and then applied to the newly registered shortcodes. 5. Shortcode callback function for bold text recursively calls do_shortcode() , enabling for nested structures. 6. Original shortcodes are restored; the comment shortcodes [url] and [b] are unregistered by the way. 7. The formatted comment content is returned for display.

<?php
/*
Plugin Name: Shortcode Example 6
Plugin URI: http://example.com/
Description: Enables [url] and [b] shortcodes in comments
Version: 1.0
*/
 
// Hook into 'comment_text' to process comment content
add_filter( 'comment_text', 'ssd_sc6_comments' );
 
// This function processes comment content
function ssd_sc6_comments( $comment ) {
 
    // Save registered shortcodes:
    global $shortcode_tags;
    $original = $shortcode_tags;
 
    // Unregister all shortcodes:
    remove_all_shortcodes();
 
    // Register new shortcodes:
    add_shortcode( 'url', 'ssd_sc6_comments_url' );
    add_shortcode( 'b', 'ssd_sc6_comments_bold' );
    add_shortcode( 'strong', 'ssd_sc6_comments_bold' );
 
    // Strip all HTML tags from comments:
    $comment = wp_strip_all_tags( $comment );
 
    // Process comment content with these shortcodes:
    $comment = do_shortcode( $comment );
 
    // Unregister comment shortcodes, restore normal shortcodes
    $shortcode_tags = $original;
 
    // Return comment:
    return $comment;
}
 
// the [b] or [strong] to <strong> callback
function ssd_sc6_comments_bold( $attr, $text ) {
    return "<strong>".do_shortcode( $text )."</strong>";
}
 
// the [url] to <a> callback
function ssd_sc6_comments_url( $attr, $text ) {
    $text = esc_url( $text );
    return "<a href=\"$text\">$text</a>";
}

Display Time - Limited Content

<?php
/*
Plugin Name: Shortcode Example 8
Plugin URI: http://example.com/
Description: Various shortcodes: [24hours], [members], [email]
Version: 1.0
*/
 
add_shortcode( '24hours', 'ssd_sc8_24hours' );
 
function ssd_sc8_24hours( $attr, $content ) {
    $now = time();
    $post_time = get_the_date( 'U' );
    if( ( $now - $post_time ) > 86400 ) {
        return 'Offer has expired!';
    } else {
        return $content;
    }
}
 
add_shortcode( 'members', 'ssd_sc8_loggedin' );
 
function ssd_sc8_loggedin( $attr, $content ) {
    if( is_user_logged_in() ) {
        return $content;
    } else {
        return "<p>Members Eyes Only</p>";
    }
}
 
add_shortcode( 'email', 'ssd_sc8_email' );
 
function ssd_sc8_email( $attr, $content ) {
    if( is_email( $content ) ) {
        return sprintf( '<a href="mailto:%s">%s</a>',
            antispambot( $content ),
            antispambot( $content )
        );
    } else {
        return '';
    }
}

Leave a Comment

Fields with * are required.

Please enter the letters as they are shown in the image above.
Letters are not case-sensitive.