WordPress shortcode snippet to display external files

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: November 24th, 2014

NumPluginAuthorScore
1Ultimate Social Media and Share Icons (Twitter, Facebook, Google+, Instagram, Pinterest etc.)UltimatelySocial198
2WPSSO Place and Location Meta - for Facebook "Location" and Pinterest "Place"Jean-Sebastien Morisset187
3Video Galleryhttp://huge-it.com/141
4Cookie Notice by dFactorydFactory121
5WP Antivirus Site Protection (by SiteGuarding.com)SiteGuarding.com (SafetyBis Ltd.)116
6WP Mail Bank - PHPMailer & SMTP MailerTech Banker111
7Enhanced Media LibrarywpUXsolutions111
8WPL Real EstateRealtyna Softwares105
9Favicon by RealFaviconGeneratorPhilippe Bernard104
10Master Slider - Responsive Touch Slideraverta104
11RSS Post Importerfeedsapi103
12qTranslate PlusPapa Salvatore Mirko (Originally created by Qian Qin)103
13WP Social SharingArjun Jain101
14All-in-One WP MigrationServMask100
15Image GalleryHuge-IT96
16Akeeba Backup CORE for WordPressAkeeba Ltd96
17WPSSO App Meta - for Apple's mobile Safari and Twitter's App CardJean-Sebastien Morisset95
18Simple Membershipsmp7, wp.insider92
19Leverage Browser Caching NinjasCustomWPNinjas84
20iFeature SliderCyberChimps inc.82

The snippet above will automatically include the contents of given page into your post. Handy, isn't it?


More like this:


Posted in: WordPress
TAGS:, , , , , , , , , , , , , , ,
Both comments and trackbacks are currently closed.

3 Comments

  1. Jan 2nd, 2013

    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" ] !

  2. Mar 18th, 2011

    interesting code, thanks for always staying on top of things :)

  3. kevin
    Mar 3rd, 2011

    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.

  • ManageWP