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.

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

    • http://alex.leonard.ie Alex Leonard

      Hey Andrew, very glad that was useful for you. Thanks for dropping by to comment as well :-)

  • Todd


    Thanks for that.. very helpfull!


    • http://alex.leonard.ie Alex Leonard

      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.

    • http://alex.leonard.ie Alex Leonard

      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.

    • http://alex.leonard.ie/ Alex Leonard

      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?

    • http://alex.leonard.ie/ Alex Leonard

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

        • http://alex.leonard.ie/ Alex Leonard

          Cool. Glad to hear you got it sorted!

  • Carlos Vergel

    Just saved my life.

    • http://alex.leonard.ie/ Alex Leonard

      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…

      • http://alex.leonard.ie/ Alex Leonard

        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!

          • http://alex.leonard.ie/ Alex Leonard

            Brilliant! Glad to be able to help :)

  • 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

    • http://alex.leonard.ie/ Alex Leonard

      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.

    • http://alex.leonard.ie/ Alex Leonard


      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 :-D )

      Very much appreciated!

      • http://alex.leonard.ie/ Alex Leonard

        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.

    • http://www.obrienphoto.net/ Pat O’Brien

      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.

    • http://alex.leonard.ie/ Alex Leonard

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

    • http://alex.leonard.ie/ Alex Leonard

      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.

    • http://alex.leonard.ie/ Alex Leonard

      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.

    • http://alex.leonard.ie/ Alex Leonard

      Hey Matt,

      Glad it was still of help :)

  • 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

    • http://alex.leonard.ie/ Alex Leonard

      Great! Glad it was useful :)

  • http://computaal.nl/ Guido Makor

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

    • http://alex.leonard.ie/ Alex Leonard

      Awesome! Delighted it’s been of use :) Can’t believe this post is nearly 4 years old now :D

  • Muzza

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

    • http://alex.leonard.ie/ Alex Leonard

      Brilliant – happy it was of use!