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"]');?>


  • Andrew

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

    • 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″]

  • Andrew

    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!

    • Hey Andrew, very glad that was useful for you. Thanks for dropping by to comment as well 🙂

  • Todd


    Thanks for that.. very helpfull!


    • Glad it was useful, cheers for commenting 🙂

  • Wasp

    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.

    • 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.

  • Carmen

    Thanks!! This is just what I needed! I appreciate you sharing.

    • Awesome! Very happy to be of assistance and thanks for leaving a comment 🙂

  • chicagola

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

    • 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/


      • chicagola

        Got it to work, thank you. I appreciate your help 🙂

  • Carlos Vergel

    Just saved my life.

    • Awesome! Very glad to have been of assistance 🙂

  • Justin

    This is pretty cool – but in my test all of the , , , etc. tags are bring striped out.

    • Justin

      hmmm… p tags, br tags, h1 tags, etc tags…

      • 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.. 🙂

        • B

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

  • pravesh

    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

    • 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.

  • Andrew Sepic

    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.

    • 🙂

      Brilliant, really glad to have helped out!

    • Henning

      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!

      • 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.

    • Brilliant, thanks!

  • Pingback: Custom written shortcode not working (wordpress) - How-To Video()

  • skweekah

    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.

    • Hi Skweekah,

      Delighted to hear this helped you out!

  • Joshua Watson

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

    • You’re most welcome – glad it was useful to you 🙂

  • Randall Addison

    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.

    • 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!


  • Matt Daniels

    Still useful! Thanks.

  • Pingback: WordPress shortcode to insert content of another page - jpollock()

  • ailsa

    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!)

  • Enda

    Thank you very much – exactly what I needed

  • Alex, this is beyond cool! It opens so many possibilities. Thanks a lot!

    • Awesome! Delighted it’s been of use 🙂 Can’t believe this post is nearly 4 years old now 😀

  • Muzza

    that’s fucken outstanding!! thanks mate – works a charm

  • 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!