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: [insert page=”45″]

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

[php]/**
* 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
extract(shortcode_atts(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();
endwhile;
}

return $output;
}
add_shortcode(‘pa_insert’, ‘pa_insertPage’);[/php]

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]<?php echo do_shortcode(‘[pa_insert page="1228"]’);?>[/php]

46 thoughts on “WordPress shortcode to insert content of another page”

  1. Awesome, so I understand where to put the shortcode, but where do you put the script to enable this functionality? Thanks.

    Like

    1. Most themes have a functions.php file. That’s where I put my iteration of this function. It works great, too! And, you put the shortcut in the document where you want the repeated content to appear. For example, if I want a WordPress page with the ID # of 215, the shortcut would be:

      [pa_insert page=”215″]

      Like

  2. Actually, never mind. The answer is: within the functions.php file, just after the list of shortcodes and widgets. This is awesome and a lifesaver. Thank you so much!

    Like

  3. Am new to WP and one of the first things I would need was this. Works fine, thanks!

    One question though: How would I enhance it, so that a page which contains shortcodes itself will parse these shortcodes as well?

    At the moment if I insert another page which has shortcodes in its content, they are displayed as plain-text shortcodes instead being parsed.

    Like

    1. Hey, sorry for the delay in replying. I've been travelling. I'll have to double check but it's probably related to using apply_filters or a similar function. I'll check as soon as I get a chance but could take a while as I'm moving around a lot the next two weeks.

      Like

  4. Thanks for the info. I could really use this functionality. But where do I put this code, inside a page in the html section?

    Like

    1. Hi Chicagola,

      Take the first batch of code and copy it to your theme’s “functions.php” file.

      The you can activate it within the normal WordPress editor window (either visual or html view) by typing [pa_insert page=”xxx”] where xxx = the page id which you wish to insert.

      Hope that makes sense!

      If you’re not sure how to edit functions.php perhaps this article will help: http://www.binaryturf.com/edit-functionsphp-wordpress/

      Cheers,
      Alex

      Like

      1. Hey Justin,

        I’m not at my desktop right now, but off the top of my head perhaps it might work with:

        $output = apply_filters( ‘the_content’, get_the_content() );

        Whenever stuff is getting stripped away apply_filters is my first port of call.. 🙂

        Like

      2. You are the MAN!! Been looking for this forever. NOw I can run this shortcode within a shortcode without trouble. Thanks!

        Like

  5. hi i installed a plugin whose contents are in another page but i want to put those in new page how to do that .please help

    Like

    1. Hi Pravesh

      I am sorry for the delay in replying – I was moving country a month ago and I remember seeing the notification at the time but I completely forgot to reply to it.

      Did you get your problem sorted out? I’m not sure I fully understand the situation.

      Like

  6. Incredible! $output = apply_filters( ‘the_content’, get_the_content() ); works perfectly and the content is output with shortcodes active.

    Thank you! I’m uber grateful to have found this. Peace.

    Like

    1. AWESOME!
      Thanks Alex for the fantastic code and Andrew for making it work with shortcodes. Exactly what I needed. Guys like you make WP so great! (and I didn’t need to melt my brain to get a working function 😀 )

      Very much appreciated!

      Like

      1. Brilliant. Delighted it was of use! So very happy to be able to provide any assistance when I’ve received so much help over the years.

        Like

  7. Beautiful! Worked a treat! Initially, I tried to use echo to return a page content but it always ended up at the top of the page due to the way echo returns the output.

    Using your method with return worked a treat. Thankyou for sharing.

    Like

  8. thank you so much! such a useful little function, cant believe wordpress doesnt have this as a standard. stoked 🙂

    Like

  9. This works great, but I’m not understanding where to put the code

    $output = apply_filters( ‘the_content’, get_the_content() );

    If I am reading this right, it will make it so the HTML markup will be transfered, correct?
    As I have it now the markup ( etc.) is not being recognized.

    Like

    1. Off the top of my head I would think you would replace ‘$output = get_the_content();’ with the new line.

      It’s line 20 of my example.

      Hope that helps!

      Cheers,
      Alex

      Like

  10. I know that was an old post but i used this code for a page and it works perfectly! What would I need to change if I had a custom post type called ‘dogs’ – as the code didn’t work with my post type. I presume I’d need to query a custom post type? Many thanks,
    Ailsa (ps I’m useless at php!)

    Like

  11. This is a very informative post and allows WP developers to make the maintenance of sites so much more efficient. I have a site where staff bios appear on several different pages. With this function, I can now plug in each bio everywhere it needs to go and I only have to maintain one occurrence of it, when it needs to be updated, instead of each instance where it occurs. Thank you!

    Like

Leave a comment