I needed a quick way to automatically include external page content in my WordPress Rising Stars post.
There are two tables with latest plugin standings that are automatically generated and I wanted the post to be updated automatically when they update too.
Easiest way to accomplish this is through WordPress shortcode API with a simple code that you can add into the theme functions.php file:
Here is the code:
function show_file_func( $atts ) {
extract( shortcode_atts( array(
'file' => ''
), $atts ) );
if ($file!='')
return @file_get_contents($file);
}
add_shortcode( 'show_file', 'show_file_func' );
This allows you to write something like this anywhere in your post:
Last updated: February 19th, 2019
Num | Plugin | Author | Score |
---|
The snippet above will automatically include the contents of given page into your post. Handy, isn't it?
More like this:
- Check if the post is older than X days before placing ads
- WordPress Fundamentals for Newbies
- How to get images from posts (and videos)
Posted in: WordPress
TAGS:articles wordpress shortcodes, how wordpress read shortcode, recent articles shortcode, recent comments shortcode, short code comment wordpress, wordpress article shortcode, wordpress import content, wordpress include post shortcode, wordpress post shortcode, wordpress shortcode article, wordpress shortcode file, wordpress shortcode import content, wordpress shortcode page content, wordpress shortcode plugin, wordpress shortcode snippet, wordpress snippet
3 Comments
This is a very dangerous shortcode that could allow authors to read any file on the server filesystem. You can make it safer by using WP’s HTTP api instead of file_get_contents(), but it’ll still be dangerous.
[ show_file file="/etc/passwd" ] !
interesting code, thanks for always staying on top of things :)
is there anyway to use this code to embed the remote file into a wordpress widget or the sidebar, instead of a post? Or maybe a wordpress plugin that keeps the last post on the homepage sticky, instead of the first like other plugins ive seen. thanks.