WordPress 插件开发手册

title: "Basic Shortcodes" post_status: publish comment_status: open taxonomy: category: - developer-plugins-handbook post_tag: - Basic Shortcodes - Shortcodes - Repos


Basic Shortcodes

Add a Shortcode

It is possible to add your own shortcodes by using the Shortcode API. The process involves registering a callback $func to a shortcode $tag using add_shortcode().

add_shortcode(
  string $tag,
  callable $func
);

[wporg] is your new shortcode. The use of the shortcode will trigger the wporg_shortcode callback function.

add_shortcode( 'wporg', 'wporg_shortcode' );
function wporg_shortcode( $atts = [], $content = null) {
  // do something to $content
  // always return
  return $content;
}

Remove a Shortcode

It is possible to remove shortcodes by using the Shortcode API. The process involves removing a registered $tag using remove_shortcode().

remove_shortcode(
  string $tag
);

Make sure that the shortcode have been registered before attempting to remove. Specify a higher priority number for add_action() or hook into an action hook that is run later.

Check if a Shortcode Exists

To check whether a shortcode has been registered use shortcode_exists().