1. Home
  2. Docs
  3. Application Framework
  4. Annotations Reference
  5. Shortcode

Shortcode

@MWP\WordPress\Shortcode( name="shortcode_tag" )

Using this annotation will register your function as a callback for a WordPress shortcode of the name you specify. It is analogous to using add_shortcode() in WordPress.

This annotation can also be used on a controller class to allow the controller to be invoked with a shortcode instead of being assigned to a specific page. When this annotation is used on a controller class, any do_action() handlers on the controller will receive three parameters when they are invoked. $atts, $content, $name

@Params

name="shortcode_tag" (required) – The shortcode tag to be replaced in the post content

@Example

/**
 * Output bold and italic text
 *
 * @MWP\WordPress\Shortcode( name="emphatic" )
 *
 * @param   array    $atts        The attributes added inside the shortcode
 * @param   string  $content     The content enclosed within the shortcode
 * @return  string
 */
public function makeEmphatic( $atts, $content )
{
    /**
     * Load the html template located at 'templates/shortcode/emphatic.php' 
     * while passing it the $content variable 
     */
    echo $this->getTemplateContent( 'shortcode/emphatic', array( 'content' => $content ) );
}

@Controller Example

/**
 * Create a shortcode to display the current users name
 *
 * @MWP\WordPress\Shortcode( name="say_my_name" )
 */
class ShortcodeController
{
    public function do_index( $atts, $content, $name ) {
        $user = wp_get_current_user();
        return $user->display_name;
    }
}
Was this article helpful to you? Yes No

How can we help?