Shortcodes In-Depth Micah Wood @wpscholar https://wpscholar.com/wcn2016
Shortcodes In-Depth
Micah Wood @wpscholarhttps://wpscholar.com/wcn2016
Built-In WordPress Shortcodes• [embed] – Allows you to embed various types of content into your posts
and pages.
• [caption] – Allows you to wrap captions around content (like images).
• [gallery] – Allows you to add one or more image galleries.
• [audio] – Allows you to embed audio files within an HTML5 audio player.
• [video] – Allows you to embed video files within an HTML 5 video player.
Shortcode Example Use Cases• Inject AdWords conversion tracking code on a page
• Insert a Google Map
• Insert a widget into the content of a page
• Create a footnote
• Display a random image
Create the Callback<?php
function callback( $atts = [], $content = null, $tag = '' ) { return $content;
}
add_shortcode( 'shortcode', 'callback' );
?>
Setup Attributes<?php
function callback( $atts = [], $content = null, $tag = '' ) { $defaults = array( 'id' => 0 ); $atts = shortcode_atts( $defaults, $atts, $tag );
return $content;
}
add_shortcode( 'shortcode', 'callback' );
?>
Ensure Attributes are Lowercase<?php
function callback( $atts = [], $content = null, $tag = '' ) { $defaults = array( 'id' => 0 ); $atts = array_change_key_case( $atts, CASE_LOWER ); $atts = shortcode_atts( $defaults, $atts, $tag );
return $content;
}
add_shortcode( 'shortcode', 'callback' );
?>
Do Custom Stuff<?php
function callback( $atts = [], $content = null, $tag = '' ) {
$defaults = array( 'id' => 0 ); $atts = array_change_key_case( $atts, CASE_LOWER ); $atts = shortcode_atts( $defaults, $atts, $tag );
if ( $atts['id'] ) {
$post = get_post( $atts['id'] ); if ( $post ) {
$content = '<a href="' . esc_url( get_permalink( $post->ID ) ) . '">' . $content . '</a>'; }
}
return $content; }
add_shortcode( 'shortcode', 'callback' );
?>
Allow for Nested Shortcodes<?php
function callback( $atts = [], $content = null, $tag = '' ) {
$defaults = array( 'id' => 0 ); $atts = array_change_key_case( $atts, CASE_LOWER ); $atts = shortcode_atts( $defaults, $atts, $tag );
if ( $atts['id'] ) {
$post = get_post( $atts['id'] ); if ( $post ) {
$content = '<a href="' . esc_url( get_permalink( $post->ID ) ) . '">' . $content . '</a>'; }
}
$content = do_shortcode( $content );
return $content; }
add_shortcode( 'shortcode', 'callback' );
?>
The Traditional Approach<?php if( function_exists( 'shortcode_callback' ) ) { shortcode_callback( $atts, $content );} ?>
Implementing the Modern Approach<?php
add_shortcode('shortcode', 'shortcode_callback');
function shortcode_render( $atts = array(), $content = '' ) { echo shortcode_callback( $atts, $content );}
add_action( 'shortcode', 'shortcode_render', 10, 2 );
?>
Shortcode Resources• Plugin Handbook - Shortcodes
• WordPress Codex – Shortcodes
• WordPress Codex – Shortcode API
• Smashing Magazine – WordPress Shortcodes: A Complete Guide
• Generate WP – Shortcode Generator
• Shortcake Plugin (Shortcode UI)
• Digging into WordPress – Call a Widget with a Shortcode
Shortcodes In-Depth
Micah Wood @wpscholarhttps://wpscholar.com/wcn2016