WordPress Shortcode for File Inclusion

Reading Time: 2 minutes

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

Comments

9 responses to “WordPress Shortcode for File Inclusion”

  1. Super Error User Avatar
    Super Error User

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

    1. Glad you found it useful!

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

    1. Raam – I am glad you found it useful!

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

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *