raunak-gupta
10/8/2016 - 9:50 AM

How to Create Your Own WordPress Shortcodes

How to Create Your Own WordPress Shortcodes

<?php

//Simple shortcodes

function HelloWorldShortcode() {
	return '<p>Hello World!</p>';
}
add_shortcode('helloworld', 'HelloWorldShortcode');

//usage
//Enter [helloworld] somewhere within a page or post to output the result of the 
//in php file -> HelloWorldShortcode().



//Parameterized shortcodes
// [bartag foo="foo-value"]
function bartag_func( $atts ) {
    $a = shortcode_atts( array(
        'foo' => 'something',
        'bar' => 'something else',
    ), $atts );

    return "foo = {$a['foo']}";
}
add_shortcode( 'bartag', 'bartag_func' );