RT Cunningham

Code Placement in WordPress Using Content Filters

code I wrote about content filters in WordPress a couple of years ago. I’m now going to focus on code placement with content filters.

You can place content almost anywhere. The WordPress Codex has tons of hooks you can investigate on your own. I’m only concerned with three of them.

The Functions File

Most WordPress themes contain a functions file. You don’t have to use it. You can place a properly formatted functions.php file in the wp-content directory and it’ll work. It’s easier to let a plugin do it for you.

Your content filters should be logically ordered in your functions file. You can assign priorities to both action and filter hooks. I like to order mine in the order they affect the page.

In some cases, you don’t even have to use a functions file at all. If your adept at programming, you can add code to the appropriate theme file. It’s sloppy to do it that way, but I did it myself that way in the past because it was quick and easy.

Code in the Head

The wp_head action hook is the best place to add code. An example function looks like this:

add_action('wp_head', 'adsense_head');
function adsense_head() {
    echo '
 // add code here
';
}

Code in the Content Area

Repeating what I’ve already written somewhat, here are three ways to use the content filter.

add_filter('the_content', 'insert_before_content');
function insert_before_content($text) {
    $text = "some code" . $text;
    return $text;
}
add_filter('the_content', 'replace_some_content');
function replace_some_content($text) {
    $text = str_replace('some code', 'some other text', $text);
    return $text;
}
add_filter('the_content', 'add_to_content');
function add_to_content($text) {
    $text .= 'some code';
    return $text;
}

There’s a way to insert code between paragraphs and before certain subheadings. It’s complicated, but you use the PHP explode and implode functions. Here’s one way to place something before the second H2 subheading:

add_filter('the_content', 'before_h2');
function adsense_h2($text) {
    $a = explode('<h2', $text);
    $text = '
// add code here
';
    $a[1] .= $text;
    $text = implode('<h2', $a);
    return $text;
}

For specific paragraphs, you could use the p tag in much the same way.

Code in the Footer

The wp_footer action hook is the best place to add code. An example function looks like this:

add_action('wp_footer', 'footer_scripts', 99);
function footer_scripts() {
    echo '
// add code here
';
}

Output Buffering

This is a powerful way to change anything output by your theme. Examine it closely:

add_action('template_redirect', 'ob_template_redirect');
function ob_template_redirect() {
    ob_start('ob_callback');
}
function ob_callback($buffer) {
    $buffer = str_replace('original', 'replacement', $buffer);
    $temp_buffer = explode("n", $buffer);
    $buffer = '';
    foreach ($temp_buffer as $line) {
        if ($line condition) {
            continue;
        }
        $buffer .= $line . "n";
    }
    return $buffer;
}

You can change items within the $buffer variable or line by line. You can also remove lines from your output, or add lines to it. It’s better to use this function than it is to try to edit theme and plugin files. You have to be careful with code and preformatted text blocks. I place everything under these conditions, for the line by line items:

$skip = 0;
 foreach ($temp_buffer as $line) {
     if (stristr($line, '<pre')  || stristr($line, '<code'))  $skip = 1;
     if (stristr($line, '</pre') || stristr($line, '</code')) $skip = 0;
     if (!$skip) {
     // line by line code here
     }
 }

Theme Manipulation

That’s what this is all about. Theme files, other plugin files, and WordPress core files should always be left as is. Using a custom plugin or a custom functions is the best way to make changes.

WordPress is a beast. It sometimes takes more code than should be necessary to tame it.

Share:    

RT Cunningham
June 18, 2019 1:03 pm
Web Development