WordPress shortcode to insert content of another page

I had the need to use this on a site on which I’m working and thought I’d share it here.

Quite simply it allows you to write a shortcode in a post thus:

This will insert the content from page ID 45 into your post/page.

 * Create a shortcode to insert content of a page of specified ID
 * @param    array        attributes of shortcode
 * @return     string        $output        Content of page specified, if no page id specified output = null
function pa_insertPage($atts, $content = null) {
 // Default output if no pageid given
 $output = NULL;

 // extract atts and assign to array
 "page" => '' // default value could be placed here
 ), $atts));

 // if a page id is specified, then run query
 if (!empty($page)) {
 $pageContent = new WP_query();
 $pageContent->query(array('page_id' => $page));
 while ($pageContent->have_posts()) : $pageContent->the_post();
 // assign the content to $output
 $output = get_the_content();

 return $output;
add_shortcode('pa_insert', 'pa_insertPage');

This shortcode could easily be expanded to allow a lot of other ‘inserts’ and can also be used in your theme’s template files by using do_shortcode() function, e.g.:

<?php echo do_shortcode('[pa_insert page="1228"]');?>