Top Banner
Shortcodes In-Depth Micah Wood @wpscholar https://wpscholar.com/wcn2016
64

Shortcodes In-Depth

Jan 13, 2017

Download

Technology

Micah Wood
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Shortcodes In-Depth

Shortcodes In-Depth

Micah Wood @wpscholarhttps://wpscholar.com/wcn2016

Page 2: Shortcodes In-Depth

What is a shortcode?

Page 3: Shortcodes In-Depth

Text

Page 4: Shortcodes In-Depth

A text shortcut

Page 5: Shortcodes In-Depth

A text shortcut dynamically replaced

Page 6: Shortcodes In-Depth

A text shortcut dynamically replaced

with content

Page 7: Shortcodes In-Depth

A text shortcut dynamically replaced

with content on render.

Page 8: Shortcodes In-Depth

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.

Page 9: Shortcodes In-Depth

WordPress.com / JetPackhttps://en.support.wordpress.com/shortcodes/

Page 10: Shortcodes In-Depth

Shortcode Syntax

Page 11: Shortcodes In-Depth

Basic Shortcode[shortcode]

Page 12: Shortcodes In-Depth

Shortcode with Attributes[shortcode attribute=“value”]

Page 13: Shortcodes In-Depth

Shortcode with Content[shortcode]content[/shortcode]

Page 14: Shortcodes In-Depth

Compound Shortcode[shortcode attribute=“value”]content[/shortcode]

Page 15: Shortcodes In-Depth

Escaped Shortcode[[shortcode]]

Page 16: Shortcodes In-Depth

When should I use shortcodes?

Page 17: Shortcodes In-Depth

When Embedding Objects or iFrames

Page 18: Shortcodes In-Depth

When Code is Required

Page 19: Shortcodes In-Depth

Complex HTML Markup

Page 20: Shortcodes In-Depth

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

Page 21: Shortcodes In-Depth

Where can I use shortcodes?

Page 22: Shortcodes In-Depth

Content Editor[shortcode]

Page 23: Shortcodes In-Depth

Theme Templates<?php echo do_shortcode(‘[shortcode]’); ?>

Page 24: Shortcodes In-Depth

Excerpts<?php add_filter( 'the_excerpt', 'do_shortcode'); ?>

Page 25: Shortcodes In-Depth

Text Widgets<?php add_filter( ‘widget_text', 'do_shortcode'); ?>

Page 26: Shortcodes In-Depth

Comments<?php add_filter( ‘comment_text', 'do_shortcode'); ?>

Page 27: Shortcodes In-Depth

Anywhere They Are Enabled<?php echo do_shortcode($content); ?>

Page 28: Shortcodes In-Depth

How do I create a shortcode?

Page 29: Shortcodes In-Depth

Register the Shortcode<?php add_shortcode(‘shortcode’, ‘callback’); ?>

Page 30: Shortcodes In-Depth

Create the Callback<?php

function callback( $atts = [], $content = null, $tag = '' ) { return $content;

}

add_shortcode( 'shortcode', 'callback' );

?>

Page 31: Shortcodes In-Depth

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' );

?>

Page 32: Shortcodes In-Depth

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' );

?>

Page 33: Shortcodes In-Depth

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' );

?>

Page 34: Shortcodes In-Depth

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' );

?>

Page 35: Shortcodes In-Depth

Why doesn’t my shortcode work?

Page 36: Shortcodes In-Depth

Return, Don’t Echo

Page 37: Shortcodes In-Depth

Shortcode Names Can’t Use Reserved Characters:

& / < > [ ] =

Page 38: Shortcodes In-Depth

Shortcode Nesting is up to the Author

Page 39: Shortcodes In-Depth

Can’t Nest Shortcodes with the Same Name

Page 40: Shortcodes In-Depth

Can’t Mix Enclosing and Non-Enclosing Shortcodes with the Same Name

Page 41: Shortcodes In-Depth

Best Practices

Page 42: Shortcodes In-Depth

Provide Detailed Documentation

Page 43: Shortcodes In-Depth

Display Helpful Messages

Page 44: Shortcodes In-Depth

Prefix Shortcode Names

Page 45: Shortcodes In-Depth

Sanitize Input, Escape Output

Page 46: Shortcodes In-Depth

Avoid Side Effects

Page 47: Shortcodes In-Depth

Shortcode API

Page 48: Shortcodes In-Depth

add_shortcode()Registers a new shortcode

Page 49: Shortcodes In-Depth

do_shortcode()Processes all shortcodes in the content

Page 50: Shortcodes In-Depth

remove_shortcode()Disables a specific shortcode

Page 51: Shortcodes In-Depth

remove_all_shortcodes()Disables all shortcodes

Page 52: Shortcodes In-Depth

has_shortcode()Checks if a shortcode exists in the content

Page 53: Shortcodes In-Depth

shortcode_exists()Checks if a specific shortcode has been registered

Page 54: Shortcodes In-Depth

shortcode_atts()Combines default and provided shortcode attributes

Page 55: Shortcodes In-Depth

Advanced Topics

Page 56: Shortcodes In-Depth

Shortcodes in Plugins vs. Themes

Page 57: Shortcodes In-Depth

What is Wrong with This? <?php echo do_shortcode(‘[shortcode]’); ?>

Page 58: Shortcodes In-Depth

What is Wrong with This? <?php shortcode_callback( $atts, $content ); ?>

Page 59: Shortcodes In-Depth

The Traditional Approach<?php if( function_exists( 'shortcode_callback' ) ) { shortcode_callback( $atts, $content );} ?>

Page 60: Shortcodes In-Depth

The Modern Approach<?php do_action( 'shortcode', $atts, $content ); ?>

Page 61: Shortcodes In-Depth

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 );

?>

Page 62: Shortcodes In-Depth

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

Page 63: Shortcodes In-Depth

Questions?

Page 64: Shortcodes In-Depth

Shortcodes In-Depth

Micah Wood @wpscholarhttps://wpscholar.com/wcn2016