Site icon Doug Daulton

WordPress Shortcode for File Inclusion

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"]
Exit mobile version