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]
Awesome, so I understand where to put the shortcode, but where do you put the script to enable this functionality? Thanks.
LikeLike
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″]
LikeLike
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!
LikeLike
Hey Andrew, very glad that was useful for you. Thanks for dropping by to comment as well 🙂
LikeLike
Awesome,
Thanks for that.. very helpfull!
Cheers
LikeLike
Glad it was useful, cheers for commenting 🙂
LikeLike
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.
LikeLike
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.
LikeLike
Thanks!! This is just what I needed! I appreciate you sharing.
LikeLike
Awesome! Very happy to be of assistance and thanks for leaving a comment 🙂
LikeLike
Thanks for the info. I could really use this functionality. But where do I put this code, inside a page in the html section?
LikeLike
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
LikeLike
Got it to work, thank you. I appreciate your help 🙂
LikeLike
Cool. Glad to hear you got it sorted!
LikeLike
Just saved my life.
LikeLike
Awesome! Very glad to have been of assistance 🙂
LikeLike
This is pretty cool – but in my test all of the , , , etc. tags are bring striped out.
LikeLike
hmmm… p tags, br tags, h1 tags, etc tags…
LikeLike
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.. 🙂
LikeLike
You are the MAN!! Been looking for this forever. NOw I can run this shortcode within a shortcode without trouble. Thanks!
LikeLike
Brilliant! Glad to be able to help 🙂
LikeLike
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
LikeLike
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.
LikeLike
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.
LikeLike
🙂
Brilliant, really glad to have helped out!
LikeLike
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!
LikeLike
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.
LikeLike
Brilliant, thanks!
LikeLike
[…] Code taken from : alex.leonard.ie/2010/09/09/wor… */ function pa_insertPage($ atts, $ content = null) { // Default output if no pageid given $ output […]
LikeLike
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.
LikeLike
Hi Skweekah,
Delighted to hear this helped you out!
LikeLike
thank you so much! such a useful little function, cant believe wordpress doesnt have this as a standard. stoked 🙂
LikeLike
You’re most welcome – glad it was useful to you 🙂
LikeLike
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.
LikeLike
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
LikeLike
Still useful! Thanks.
LikeLike
Hey Matt,
Glad it was still of help 🙂
LikeLike
[…] WordPress shortcode to insert content of another page. […]
LikeLike
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!)
LikeLike
Thank you very much – exactly what I needed
LikeLike
Great! Glad it was useful 🙂
LikeLike
Alex, this is beyond cool! It opens so many possibilities. Thanks a lot!
LikeLike
Awesome! Delighted it’s been of use 🙂 Can’t believe this post is nearly 4 years old now 😀
LikeLike
that’s fucken outstanding!! thanks mate – works a charm
LikeLike
Brilliant – happy it was of use!
LikeLike
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!
LikeLike