Doug Daulton

Storyteller (Words & Images)

  • Craft
    • Influences
    • Ethics
    • Kit
  • Projects
    • Bokeh (Feature Film)
    • PhotoFocus
    • Project 365
  • Resources
  • Travel
  • Sidebar

CONNECT

  • Facebook
  • Instagram
  • Tumblr
  • Twitter

© 2021 Doug Daulton
All rights reserved.

WordPress Shortcode for File Inclusion

August 1, 2009 by Doug 9 Comments

I have a project that requires a lot of complex, yet static Pages in WordPress … think lots of tabular data with heavy CSS styling. This sort of content is best written in an external editor like Zend IDE or Dreamweaver and then dropped into WordPress.

However, if you’ve ever done this, testing the page as you develop it is a huge pain in the backside. It involves cutting and pasting from the editor to WP … saving and refreshing … make changes in the external editor … rinse and repeat.

Not fun. Not efficient.

The solution is fairly straightforward — keep these files static HTML and simply include them somehow in the page. I researched the available plugins and they all seemed like overkill which added overhead. Then, in WP IRC, Fris suggested writing a custom shortcode to do the heavy lifting. 90 minutes later, I have precisely what I need and a renewed respect for WordPress and the community supporting it.

For those interested, here is the code:

 NULL,
    'file' => NULL
    ), $atts ) );

    // Set file path
    $path_base = ABSPATH."wp-content/inc_static/";
    $path_file = ($subdir == NULL) ? $path_base.$file : $path_base.$subdir."/".$file;

    // Load file or, if absent. throw error
    if (file_exists($path_file)) {
        $file_content = file_get_contents($path_file);
        return $file_content;
    }
    else {
        trigger_error("'$path_file' file not found", E_USER_WARNING);
        return "FILE NOT FOUND: ".$path_file."
SUBDIR = ".$subdir."
FILE = ".$file."

"; } } add_shortcode('static_html', 'sc_static_html'); ?>

USE CASE

[static_html subdir="testdir" file="dirtest.html"]

SHARE:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to email this to a friend (Opens in new window)
  • Click to print (Opens in new window)

Related

Subjects: Random

Comments

  1. Super Error User says

    March 23, 2010 at 5:11 am

    Thanks for sharing Doug. This worked like a charm. Does it only work with static HTML but not the ability to include CSS or javascript?

    Additional tip for newbies. Don’t forget to add the following to functions.php if you want it to work in widgets too.

    add_filter(‘widget_text’, ‘do_shortcode’);

    Reply
    • Doug says

      March 23, 2010 at 6:11 am

      I have not tried it with CSS or JS. My gut tells me that it would load it as a TXT file and not execute it. If you want the file to execute, you want to use the appropriate HTML tags. THanks for the widget improvement as well!

      Reply
  2. writemcodeboy says

    May 15, 2012 at 5:53 am

    Doug, this worked great and saved me a ton of time.

    I was able to tweak it minimally for my use.
    Also, fwiw – including JS and CSS was no problem if (as you stated) you add it using the appropriate html (script, style) tags.

    WP v3.2

    Thank you again for sharing!

    Reply
    • Doug Daulton says

      June 3, 2012 at 5:23 pm

      Glad you found it useful!

      Reply
  3. Raam Dev says

    July 9, 2012 at 4:14 am

    Brilliant! Thank you for this. I used it on the page linked to my name above. 🙂

    Reply
    • Doug Daulton says

      July 9, 2012 at 11:33 am

      Raam – I am glad you found it useful!

      Reply
  4. Steph says

    July 10, 2012 at 7:33 pm

    Thank you – very useful and will save me a lot of time.

    Reply
  5. sreejith says

    August 14, 2012 at 11:34 pm

    where to write this code? and where in which directory i need to keep the html file?

    Reply
    • Doug Daulton says

      October 5, 2012 at 6:15 pm

      Add the code in functions.php and set the path to HTML directory in the settings. You can put it anywhere you like.

      Reply

Leave a Reply Cancel reply

Tweets

My Tweets
loading Cancel
Post was not sent - check your email addresses!
Email check failed, please try again
Sorry, your blog cannot share posts by email.