Make WordPress Core


Ignore:
Timestamp:
08/20/2017 07:43:58 PM (9 years ago)
Author:
DrewAPicture
Message:

Docs: Improve documentation for add_shortcode() by:

  • Removing inline examples already listed in the Code Reference
  • Improving the summary and description to explain how tag conflicts are handled
  • Supplement the docs for the $func parameter by describing the three arguments passed to a shortcode callback.

Props grapplerulrich for the initial patch.
Fixes #37222.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/shortcodes.php

    r41026 r41281  
    4343
    4444/**
    45  * Add hook for shortcode tag.
    46  *
    47  * There can only be one hook for each shortcode. Which means that if another
    48  * plugin has a similar shortcode, it will override yours or yours will override
    49  * theirs depending on which order the plugins are included and/or ran.
    50  *
    51  * Simplest example of a shortcode tag using the API:
    52  *
    53  *     // [footag foo="bar"]
    54  *     function footag_func( $atts ) {
    55  *         return "foo = {
    56  *             $atts[foo]
    57  *         }";
    58  *     }
    59  *     add_shortcode( 'footag', 'footag_func' );
    60  *
    61  * Example with nice attribute defaults:
    62  *
    63  *     // [bartag foo="bar"]
    64  *     function bartag_func( $atts ) {
    65  *         $args = shortcode_atts( array(
    66  *             'foo' => 'no foo',
    67  *             'baz' => 'default baz',
    68  *         ), $atts );
    69  *
    70  *         return "foo = {$args['foo']}";
    71  *     }
    72  *     add_shortcode( 'bartag', 'bartag_func' );
    73  *
    74  * Example with enclosed content:
    75  *
    76  *     // [baztag]content[/baztag]
    77  *     function baztag_func( $atts, $content = '' ) {
    78  *         return "content = $content";
    79  *     }
    80  *     add_shortcode( 'baztag', 'baztag_func' );
     45 * Adds a new shortcode.
     46 *
     47 * Care should be taken, either through prefixing or other means,to ensure
     48 * that the shortcode tag being added is unique and will not conflict with
     49 * other, already-added shortcode tags. In the event of a duplicated tag, the
     50 * tag loaded last will take precedence.
    8151 *
    8252 * @since 2.5.0
     
    8555 *
    8656 * @param string   $tag  Shortcode tag to be searched in post content.
    87  * @param callable $func Hook to run when shortcode is found.
     57 * @param callable $func The callback function to run when the shortcode is found.
     58 *                       Every shortcode callback is passed three parameters by default,
     59 *                       including an array of attributes (`$atts`), the content
     60 *                       the shortcode was found in (`$content`), and finally
     61 *                       the shortcode tag itself (`$shortcode_tag`), in that order.
    8862 */
    8963function add_shortcode($tag, $func) {
Note: See TracChangeset for help on using the changeset viewer.