breich

WordPress Shortcode Boilerplate

May 15th, 2012
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.39 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * WordPress Shortcode Boilerplate.
  5.  *
  6.  * Shortcode functions accepts the following parameters:
  7.  *
  8.  * $atts (array) : When a user writes a shortcode into their WordPress post
  9.  * which triggers your shortcode function, any attributes they assign to that
  10.  * shortcode will be passed as an associative array to your function. For
  11.  * example the shortcode [caption caption="foobar"] would result in an $atts
  12.  * being assigned the associative array "array( 'caption' => 'foobar')"
  13.  *
  14.  * $content (string) : The content of the shortcode.  Like HTML tags a
  15.  * shortcode can be either self-terminating (like the <img/> tag) or provide
  16.  * opening and closing tags (like <p>...</p>). When a shortcode provides an
  17.  * opening and closing tag, $content is assigned the data between the tags.
  18.  * For example if we write the following shortcode [caption]This is the
  19.  * content of my shortcode[/caption], $content would be assigned the string
  20.  * "This is the content of my shortcode"
  21.  *
  22.  * $tag (string) : The name of the shortcode that triggered the function.
  23.  * Multiple shortcodes can actually be assigned the same function, and $tag
  24.  * allows us to determine programmatically which shortcode triggered the
  25.  * function. For example we might create a function called headers() and
  26.  * assign it to the shortcodes h1, h2, h3, h4, h5, h6. Using the shortcode
  27.  * [h1]...[/h1] would call headers() with $tag being assigned the value "h1",
  28.  * [h2]...[/h2] would call headers() with $tag being assigned the value "h2",
  29.  * etc.
  30.  *
  31.  * Shortcode functions return a value. They do not output text.
  32.  *
  33.  * @param array  $atts    Attributes assigned to the shortcode.
  34.  * @param string $content The text between the opening and closing tags.
  35.  * @param string $tag     The shortcode tag that triggered the function.
  36.  *
  37.  * @return string Returns the text generated by the shortcode.
  38.  */
  39. function shortcode_func( $atts, $content = null, $tag = '' )
  40. {
  41.     /*
  42.      * The following lines utilize the shortcode_atts() WordPress function
  43.      * to mix-in default values for your shortcode's attributes.  For
  44.      * example if your shortcode accepts an attribute named "foo" it will
  45.      * be assigned the value "something" if the user did not specify a
  46.      * value.
  47.      *
  48.      * The PHP extract() function is then used to convert the shortcode's
  49.      * attributes into local variables. So we can refer to the "foo"
  50.      * attribute by checking the variable $foo rather than referring to
  51.      * $atts['foo'].
  52.      */
  53.     extract( shortcode_atts( array(
  54.         'foo' => 'something',
  55.         'bar' => 'something else'
  56.     ), $atts ) );
  57.    
  58.     /*
  59.      * This is where we do custom processing for our shortcode. I'm just
  60.      * assigning the variable $html the content of the shortcode. The code
  61.      * that falls here is totally up to you.
  62.      */
  63.     $html = $content;
  64.    
  65.     /*
  66.      * Process any nested shortcodes and return our processed data. If your
  67.      * shortcode has a start and end tag and contains content, this step is
  68.      * neccessary, otherwise nested shortcodes may display unprocessed to
  69.      * your readers!
  70.      */
  71.     return do_shortcode( $html );
  72. }
  73.  
  74. /*
  75.  * Use the WordPress add_shortcode() function to register a shortcode with
  76.  * WordPress. The first parameter specifies the shortcode tag that will be
  77.  * used to trigger your function. The second parameter specifies the name
  78.  * of the function to call when the shortcode is encountered.
  79.  */
  80. add_shortcode( 'shortcode', 'shortcode_func' );
Add Comment
Please, Sign In to add comment