1function create_shortcode(){
2 return "<h2>Hello world !</h2>";
3}
4add_shortcode('my_shortcode', 'create_shortcode');
5// Use [my_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// 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');
12
1The pairs should be considered to be all of the attributes
2which are supported by the caller and given as a list. The
3returned attributes will only contain the attributes in the $pairs list.
4
5If the $atts list has unsupported attributes, then they will be ignored
6and removed from the final returned list.
7
8<?php
9function shortcode_atts( $pairs, $atts, $shortcode = '' ) {
10 $atts = (array) $atts;
11 $out = array();
12 foreach ( $pairs as $name => $default ) {
13 if ( array_key_exists( $name, $atts ) ) {
14 $out[ $name ] = $atts[ $name ];
15 } else {
16 $out[ $name ] = $default;
17 }
18 }
19
20 if ( $shortcode ) {
21 /**
22 * Filters shortcode attributes.
23 *
24 * If the third parameter of the shortcode_atts() function is present then this filter is available.
25 * The third parameter, $shortcode, is the name of the shortcode.
26 *
27 * @since 3.6.0
28 * @since 4.4.0 Added the `$shortcode` parameter.
29 *
30 * @param array $out The output array of shortcode attributes.
31 * @param array $pairs The supported attributes and their defaults.
32 * @param array $atts The user defined shortcode attributes.
33 * @param string $shortcode The shortcode name.
34 */
35 $out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts, $shortcode );
36 }
37
38 return $out;
39}
40
41?>
42