Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Created February 20, 2014 22:29

Revisions

  1. wpscholar revised this gist Feb 20, 2014. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions shortcode.php
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,6 @@

    /**
    * Class My_Shortcode
    *
    * @package mPress
    */
    class My_Shortcode {

  2. wpscholar created this gist Feb 20, 2014.
    91 changes: 91 additions & 0 deletions shortcode.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    <?php

    /**
    * Class My_Shortcode
    *
    * @package mPress
    */
    class My_Shortcode {

    /**
    * The shortcode attributes
    *
    * @var array
    */
    protected $_atts = array();

    /**
    * The shortcode content
    *
    * @var string
    */
    protected $_content = '';

    /**
    * The default shortcode attributes
    *
    * @var array
    */
    protected static $_default_atts = array();

    /**
    * The shortcode callback to be registered
    *
    * @param array $atts
    * @param string $content
    * @return string
    */
    public static function callback( $atts = array(), $content = '' ) {
    $shortcode = new self( wp_parse_args( $atts ), $content );
    return $shortcode->process();
    }

    /**
    * Render the shortcode immediately
    *
    * @param array $atts
    * @param string $content
    */
    public static function render( $atts = array(), $content = '' ) {
    echo self::callback( $atts, $content );
    }

    /**
    * Setup the shortcode's properties
    *
    * @param array $atts
    * @param string $content
    */
    public function __construct( array $atts = array(), $content = '' ) {
    $this->initialize();
    $this->_atts = shortcode_atts( self::$_default_atts, $atts );
    $this->_content = $content;
    }

    /**
    * Function used for setting up dynamically generated default attributes.
    */
    public function initialize() {

    }

    /**
    * Process the shortcode and return the output
    *
    * @return string
    */
    public function process() {
    // Do stuff
    return '';
    }

    }

    // Register the [my_shortcode] shortcode
    add_shortcode( 'my_shortcode', array( 'My_Shortcode', 'callback' ) );

    // Enable the ability to call the 'my_shortcode' action
    add_action( 'my_shortcode', array( 'My_Shortcode', 'render' ), 10, 2 );

    // Example usage of the 'my_shortcode' action in a theme
    do_action( 'my_shortcode', $atts = array(), $content = '' );