Function Reference/add shortcode

来自WordPress中文文档
跳转至: 导航搜索

Description

Adds a hook for a shortcode tag.

Usage

%%%<?php add_shortcode( $tag , $func ); ?>%%%

Parameters

tt$tag/tt
(string) (required) Shortcode tag to be searched in post content
Default: Nonenoinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

tt$func/tt
(callable) (required) Hook to run when shortcode is found
Default: Nonenoinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Notes

This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter

The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..

name
The name of the parameter.
datatype
The datatype that should be given for this parameter when called.
  • string
  • integer
  • boolean
  • mixed
description
A short description of the parameter.
importance
Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
  • required
  • optional
default
If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.

wordpress.org.cn /div /noinclude

Return Values

(none)

Examples

Simplest example of a shortcode tag using the API: [footag foo="bar"]

function footag_func( $atts ) {
     return "foo = {$atts['foo']}";
}
add_shortcode('footag', 'footag_func');

Example with nice attribute defaults: [bartag foo="bar"]

function bartag_func( $atts ) {
     extract( shortcode_atts( array(
	      'foo' => 'no foo',
	      'baz' => 'default baz'
     ), $atts ) );
     return "foo = {$foo}";
}
add_shortcode( 'bartag', 'bartag_func' );

Example with enclosed content: [baztag]content[/baztag]

function baztag_func( $atts, $content="" ) {
     return "content = $content";
}
add_shortcode( 'baztag', 'baztag_func' );

If your plugin is designed as a class write as follows:

class MyPlugin {
     function baztag_func() {
            return "content = $content";
     }
}
add_shortcode( 'baztag', array( 'MyPlugin', 'baztag_func' ) );

Notes

There can only be one hook for each shortcode. Which means that if another plugin has a similar shortcode, it will override yours or yours will override theirs depending on which order the plugins are included and/or ran.

Shortcode attribute names are always converted to lowercase before they are passed into the handler function. Values are untouched.

Note that the function called by the shortcode should never produce output of any kind. Shortcode functions should return the text that is to be used to replace the shortcode. Producing the output directly will lead to unexpected results. This is similar to the way filter functions should behave, in that they should not produce expected side effects from the call, since you cannot control when and where they are called from.

Change Log

Since: 2.5

Source File

add_shortcode() is located in onlyincludecodewp-includes/shortcodes.php/code/onlyinclude

div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9

Template Description

Link to the source code on http://core.trac.wordpress.org/browser/.

Parameters

  1. filename
  2. (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.

Usage

Link to the stable version: pre检查到模板循环:模板:Trac/pre

Link to trunk: pre检查到模板循环:模板:Trac/pre

/div

wordpress.org.cn.

Resources

Related

Shortcode: do_shortcode, add_shortcode, remove_shortcode, remove_all_shortcodes, shortcode_atts, strip_shortcodes

includeonlydiv style=clear:both; background-color:#F7F7F7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;See also index of Function Reference and index of Template Tags./div/includeonlynoinclude

Description

This Template is used by Codex:Template Messages.

Usage

pre 检查到模板循环:模板:Message /pre

Result

检查到模板循环:模板:Message

/noinclude