Create wordpress plugin that adds custom text to the bottom of the single article content

To make this plugin we need to follow those steps:

Step 1. Create plugin folder, main php file and fill it with standard values.

Step 2. Create a menu where custom text can be added (in this case we will create a submenu of Generic Settings) add_options_page() with callback function that will generate the whole content of this menu page

Step 3. Add settings function with admin_init hook to include all settings methods

Add section add_settings_section()

Register settings so that the value can be saved in the database register_setting()

Add settings field add_settings_field() with the function to create this field in html (or php)

Step 4. Edit callback function form step 2 to create a form that can send data to database

Step 5. Add function to change the content and add_filter(’the_content’)

<?php
/*
Plugin Name: Add Text 
Description: It adds some text to the content
Author: pikademia
Version: 1.0.0
Author URI: https://pikademia.pl
*/
if ( !defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

// Step 2 Add submenu with createPageATPI callback function
add_action('admin_menu', 'addSubmenuATPI');

function addSubmenuATPI(){
    add_options_page(
        'Add text plugin',
        'Add text',
        'manage_options',
        'addTextPage',
        'createPageATPI');
}

// Step 3
add_action('admin_init', 'createSettingsATPI');

function createSettingsATPI(){
    add_settings_section('ATPIsection',null,null,'addTextPage');
    register_setting('ATPIsettingsGroup','myText');
    add_settings_field('myTextField', 'Add your custom text:','createFieldHtmlATPI', 'addTextPage', 'ATPIsection');
}

function createFieldHtmlATPI(){
    ?>
    <input type="text" name="myText" value="<?php echo esc_attr(get_option('myText')) ?>">
    <?php
}

// Step 2 nad 4
function createPageATPI(){
    ?>
    <div class="wrap">
      <h1>Add Text Plugin</h1>
      <form action="options.php" method="POST">
      <?php
        settings_fields('ATPIsettingsGroup');
        do_settings_sections('addTextPage');
        submit_button();
      ?>
      </form>
    </div>
    <?php
}

// Step 5
add_filter('the_content', 'changeContent');

function changeContent($c){
    if (is_main_query() AND is_single()){
        $myText = esc_attr(get_option('myText'));
        $c .= "<p class='atpi'>$myText</p>";
    }    
    return $c;
}

If you want to be able to add html markup so that you can add styles to your input, just get rid off esc_attr() in the changeContent function but it’s not recommended.
If you, or somebody else type javascript code it might be dangerous. For example if you typed <script>alert(„Hey, you were hacked!”);</script> the popup would show up.
You can always style your custom text output with css based on the class provided.

Scroll to Top