shortcode wordpress php code example
Example 1: wordpress do shortcode
<?php echo do_shortcode('[name_of_shortcode parameters=""]'); ?>
Example 2: wordpress create shortcode
function wpb_demo_shortcode() {
$message = 'Hello world!';
return $message;
}
add_shortcode('greeting', 'wpb_demo_shortcode');
Example 3: php shortcode wordpress return content with shortcodes
return do_shortcode($content);
Example 4: shortcode_atts wordpress
The pairs should be considered to be all of the attributes
which are supported by the caller and given as a list. The
returned attributes will only contain the attributes in the $pairs list.
If the $atts list has unsupported attributes, then they will be ignored
and removed from the final returned list.
<?php
function shortcode_atts( $pairs, $atts, $shortcode = '' ) {
$atts = (array) $atts;
$out = array();
foreach ( $pairs as $name => $default ) {
if ( array_key_exists( $name, $atts ) ) {
$out[ $name ] = $atts[ $name ];
} else {
$out[ $name ] = $default;
}
}
if ( $shortcode ) {
$out = apply_filters( "shortcode_atts_{$shortcode}", $out, $pairs, $atts, $shortcode );
}
return $out;
}
?>