Embedding remote content in WordPress

Introduction

I wrote some C programs that I stored in Subversion and that I wanted to show in this article.

Initially, as an xfce4-terminal user, I was cat-ing the program, selecting the displayed text with the mouse, right-clicking, selecting ‘Copy as HTML’ and pasting into an empty pre-formatted block in WordPress.

Repeating that procedure each time I edited the C programs quickly became a pain but I did not want the version of the C programs in Subversion and the version of the C programs in WordPress to become misaligned. I had to find another way.

Things I tried that did not work

  • The ideal solution would probably be HTML5 supports imports, but I did not find support for them in WordPress or a plugin.
  • JSM’s file_get_contents() Shortcode plugin looked promising, but it mangled all occurrences of ‘<‘ and ‘>’ so that this:
    #include <stdio.h>    /* included for printf() declaration */

    became this:

    #include     /* included for printf() declaration */

My incremental improvement to David’s solution

  1. Edit wp-functions.php or wp-config.php and add the following:
    function show_remote_content_func( $atts ) {
        extract( shortcode_atts( array(
            'url' => '',
            'esc' => '',
            'pre' => ''
        ), $atts ) );
      
        if ($url == '') {
            return;
        }
        $contents = @file_get_contents($url);
        if ($esc == 'true') {
            $contents = esc_html($contents);
        }
        if ($pre == 'true') {
            $contents = '<pre>' . $contents . '</pre>';
        }
        return $contents;
    }
    add_shortcode( 'show_remote_content', 'show_remote_content_func' );
  2. In the page where you want to embed to remote content add:
    [show_remote_content url="<some-url>" pre="true" esc="true"]

    Omit pre="true" and/or esc="true" as required.

See also