Register WordPress Shortcodes

10/08/2011

WordPress’s shortcodes makes it possible to add useful functionality directly into the wysiwyg editor. This will enhance the functionality of your WordPress theme.

We are now going to create a shortcode that produces a set of links to sub pages of a particular parent page. So when new pages are added under this parent page, the list of links that this shortcode produces will update automatically.

First we will register the shortcode in the WordPress theme’s function file.

add_shortcode('formslinks', 'formslinksfunction');

The two parameters ‘formslinksfunction’, ‘formslinks’ contain references to the function and the name of the shortcode respectively.

Now it’s time to write the function in our functions.php file that our short code ‘formlinks’ will call.

function formslinksfunction() {

 $args = array(
	'depth'        => 0,
	'show_date'    => '',
	'date_format'  => get_option('date_format'),
	'child_of'     => 1782,
	'exclude'      => '',
	'include'      => '',
	'title_li'     => __(''),
	'echo'         => 0,
	'authors'      => '',
	'sort_column'  => 'menu_order, post_title',
	'link_before'  => '',
	'link_after'   => '',
	'walker' => '' );

$list = wp_list_pages( $args );

return $list;

}

This function produces a list of sub or child WordPress pages for the parent WordPress page with an id of 1782.

Remember to return the variable or else your WordPress shortcode will produce nothing.

Now we have our function, we just have to put the shortcode in the WordPress wysiwyg in WordPress pages.

[formslinks]

Once the page is published the links to the WordPress sub pages will be shown.