1function create_shortcode(){
2 return "<h2>Hello world !</h2>";
3}
4add_shortcode('my_shortcode', 'create_shortcode');
5// Use [my_shortcode]
1// function that runs when shortcode is called
2function wpb_demo_shortcode() {
3
4// Things that you want to do.
5$message = 'Hello world!';
6
7// Output needs to be return
8return $message;
9}
10// register shortcode
11add_shortcode('greeting', 'wpb_demo_shortcode');
1function wp_demo_shortcode() {
2
3//Turn on output buffering
4ob_start();
5$code = 'Hello World';
6ob_get_clean();
7
8 // Output needs to be return
9return $code;
10}
11
12// register shortcode
13add_shortcode('helloworld', 'wp_demo_shortcode');
1// Simplest example of a shortcode tag using the API: [footag foo="bar"]
2add_shortcode( 'footag', 'wpdocs_footag_func' );
3function wpdocs_footag_func( $atts ) {
4 return "foo = {$atts['foo']}";
5}
6
7
8// Example with nice attribute defaults: [bartag foo="bar"]
9add_shortcode( 'bartag', 'wpdocs_bartag_func' );
10function wpdocs_bartag_func( $atts ) {
11 $atts = shortcode_atts( array(
12 'foo' => 'no foo',
13 'baz' => 'default baz'
14 ), $atts, 'bartag' );
15
16 return "foo = {$atts['foo']}";
17}
18
19// Example with enclosed content: [baztag]content[/baztag]
20add_shortcode( 'baztag', 'wpdocs_baztag_func' );
21function wpdocs_baztag_func( $atts, $content = "" ) {
22 return "content = $content";
23}
24
25// If your plugin is designed as a class write as follows:
26add_shortcode( 'baztag', array( 'MyPlugin', 'wpdocs_baztag_func' ) );
27class MyPlugin {
28 public static function wpdocs_baztag_func( $atts, $content = "" ) {
29 return "content = $content";
30 }
31}