create shortcode in wordpress code example
Example 1: wordpress do shortcode
<?php echo do_shortcode('[name_of_shortcode parameters=""]'); ?>
Example 2: how to create wordpress shortcodes
function wpb_demo_shortcode() {
$message = 'Hello world!';
return $message;
}
add_shortcode('greeting', 'wpb_demo_shortcode');
Example 3: wordpress shortcode
function wpb_demo_shortcode() {
$message = 'Hello world!';
return $message;
}
add_shortcode('greeting', 'wpb_demo_shortcode');
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;
}
?>
Example 5: generate shortcode wordpress plugin
add_filter( 'widget_text', 'shortcode_unautop' );
add_filter( 'widget_text', 'do_shortcode' );