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: February 19th, 2018

NumPluginAuthorScore
1Ninja Tables – Best Responsive Table Plugin for WordPressWPManageNinja178
2Clearfy – WordPress optimization plugin and disable ultimate tweakerWebcraftic 174
3Click to Chat for WhatsApp ChatHoliThemes128
4Elementor Essential AddonsCodetic108
5PT Elementor Addons Liteparamthemes105
6Login No Captcha reCAPTCHARobert Peake105
7Force HTTPS (SSL Redirect & Fix Insecure Content)LittleBizzy105
8CMP – Coming Soon & Maintenance Plugin by NiteoThemesNiteoThemes102
9Contact Form 7 RedirectionQuery Solutions83
10ACF: Rus-To-LatAndrey Pavluk 83
11TranslatePress – Translate Multilingual sitesCozmoslabs, Razvan Mocanu, Madalin Ungureanu, Cristophor Hurduban82
12Drip Marketing Automation PluginDrip79
13Post and Page Builder by BoldGrid – Visual Drag and Drop EditorBoldGrid.com 78
14Shortcodes and extra features for Phlox themeaverta78
15Cherry SearchZemez78
16Customer Reviews for WooCommerceivole76
17Popup Maker – WordPress lightbox Pop upPopup Maker75
18SEO Boostercleverplugins.com72
19WordPress TablesIan Sadovy71
20WP Content Security PluginDylan Downhill69

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 10:02 PM

    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 11:31 AM

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

  3. kevin
    Mar 3rd, 2011 7:34 AM

    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.