General

WordPress – Add Custom Content Plugin

Editing posts directly within the wordpress editor can be a nuisance when wordpress inserts ‘p’ tags and the like. Sometimes I want to edit HTML in actual HTML editor which allows me greater control over the presentation of the content.

To facilitate this I have added a plugin which inserts HTML from a custom file based on the post id.

Content is added a file within the ‘custom’ folder:
“twentyfifteen-child\Custom\Content\Custom-Content-82.php

This file can be edited and viewed in a standard HTML editor. The number within the file name is the post id which can be found from url’s and other ways. When the content is shown, the content within the wordpress editor is shown first followed by the content within the file.

Here is the plugin code as to date:

<?php
/**
 * Plugin Name:       Include Custom Content
 * Plugin URI:        https://dalestake.com/2019/11/26/wordpress-add-custom-content-plugin/
 * Description:       Inserts files stored in 'Custom/Content/' into the content section (ie within the body of the post)
 * Version:           0.1
 * Author:            Dale Anderson
 * Author URI:        NA
 * License:           GPL v2 or later
 * License URI:       https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain:       include-custom-content
 */

// Check if a custom content file exists... 

add_filter('the_content', 'InsertCustomContent');
function InsertCustomContent($content)
{
    $CurID = get_the_ID();
    $FilePath = get_stylesheet_directory()."\Custom\Content\Custom-Content-".$CurID.".php";

    if(file_exists ( $FilePath))
    {
        $content = $content.file_get_contents($FilePath);
    }
    else
    {
        $content = $content;
    }
    return $content;
}

My intent is to make adjustments to allow similar files to be included such as scripts and css.

Leave a Reply

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