WordPress Shortcode for File Inclusion

In Resources by Doug9 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:

    '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'); ?>


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


  1. 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’);

    1. Author

      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!

  2. 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!

Leave a Comment