Shortcodes Creation
[video_embed src=”” width=”” height=””]
// Add Shortcode
function video_embed_shortcode( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'src' => '',
'width' => '',
'height' => '',
),
$atts,
'video_embed'
);
// Return custom embed code
return '<embed
src="' . $atts['src'] . '"
width="' . $atts['width'] . '"
height="' . $atts['height'] . '"
type="application/x-shockwave-flash"
allowscriptaccess="always"
allowfullscreen="true">';
}
add_shortcode( 'video_embed', 'video_embed_shortcode' );
[recent-posts posts=”5″]
// Add Shortcode
function recent_posts_shortcode( $atts , $content = null ) {
// Attributes
$atts = shortcode_atts(
array(
'posts' => '5',
),
$atts,
'recent-posts'
);
// Query
$the_query = new WP_Query( array ( 'posts_per_page' => $atts['posts'] ) );
// Posts
$output = '<ul>';
while ( $the_query->have_posts() ) :
$the_query->the_post();
$output .= '<li>' . get_the_title() . '</li>';
endwhile;
$output .= '</ul>';
// Reset post data
wp_reset_postdata();
// Return code
return $output;
}
add_shortcode( 'recent-posts', 'recent_posts_shortcode' );
[link-to-post id=””]
// Add Shortcode
function post_link_shortcode( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'id' => '',
),
$atts,
'link-to-post'
);
// Return only if has ID attribute
if ( isset( $atts['id'] ) ) {
return '<a href="' . get_permalink( $atts['id'] ) . '">' . get_the_title( $atts['id'] ) . '</a>';
}
}
add_shortcode( 'link-to-post', 'post_link_shortcode' );
[img width=”” height=””] http://… [/img]
// Add Shortcode
function img_shortcode( $atts , $content = null ) {
// Attributes
$atts = shortcode_atts(
array(
'width' => '',
'height' => '',
),
$atts,
'img'
);
// Return image HTML code
return '<img src="' . $content . '" width="' . $atts['width'] . '" height="' . $atts['height'] . '">';
}
add_shortcode( 'img', 'img_shortcode' );
[b] bold text [/b]
// Add Shortcode
function blod_text_shortcode( $atts , $content = null ) {
return '<strong>' . $content . '</strong>';
}
add_shortcode( 'b', 'blod_text_shortcode' );
[cloak email=”me@email.com”]
// Add Shortcode
function email_cloaking_shortcode( $atts ) {
// Attributes
$atts = shortcode_atts(
array(
'email' => '',
),
$atts,
'cloak'
);
// Return cloaked email
return antispambot( $atts['email'] );
}
add_shortcode( 'cloak', 'email_cloaking_shortcode' );