GO BACK

Contact Us

5-E / 22 BP , 2nd Floor
Near BK Chowk
121001
Faridabad (Haryana), INDIA

Home Forums WordPress Add content in Short Code? Reply To: Add content in Short Code?

#1920 Reply

admin
Keymaster

You have to use two functions. The first one is WordPress’ built-in shortcode_atts() function, which combines user shortcode attributes with native attributes and fills in the defaults where needed. The second function is the extract() PHP function, which does what its name suggests: it extracts the shortcode attributes.

function row_function($atts){
extract(shortcode_atts(array(
‘bg_color’ => ”,
), $atts));

You can take our shortcode one step further and add the ability to pass some content as an argument, which in this case will be a heading for the list of recent posts. To do this, we use a second parameter, $content, in the callback function and add it as an h3 heading before the list. The new function is as follows:

function row_function($atts, $content = null) {
extract(shortcode_atts(array(
‘bg_color’ => ”,
), $atts));
$return_string = ”;
$return_string .= ‘

‘;
$return_string .= $content;
$return_string .= ‘

‘;

return $return_string;
}

Now, we tell WordPress that this function is a shortcode:

function register_shortcodes(){
add_shortcode(‘row’, ‘row_function’);
}

In order to execute our register_shortcodes() function, we will tie it to WordPress’ initialization action:

add_action( ‘init’, ‘register_shortcodes’);

Regards

Tech9logy Creators

img