WordPress adding content into different sections

Don’t do that! There are many other options to achieve this that are not hacky and messy. Here is what I would suggest.


Overview

First, create a child theme. This will allow you to make edits to your theme without losing them during an update.

Once your child theme is setup, add a custom field to your admin pages. I don’t use a ton of plugins but when it comes to adding custom fields I use ACF because its super easy and you can create professional admin layouts in minutes. If you want to add the custom field manually check this out.

Now that you have your custom field added, all that’s left is to pull in the data from the custom field into your page template.


Basic Code Example

This will change a bit depending on your theme but you should get the idea. For this example I will use the Twenty Seventeen theme. In this theme, the content for Pages is coming from /template-parts/page/content-page.php, so I duplicate that in my child theme.

With ACF Plugin (recommended)

Lets say I used ACF to create a new editor section called “more_content”. I would just place the following code in my child theme’s content-page.php and your done.

<div id="more-content">
    <?php
        $value = get_field("more_content");

        if($value) {
            echo $value;
        }
    ?>
</div>

Without ACF Plugin

If you didn’t want to use ACF to create the custom field you would need to create the field using code like this…

add_action('add_meta_boxes',  function() { 
    add_meta_box('my_meta_box', 'Meta Box Title', 'my_metabox_function');
});

function my_metabox_function($post) {
    $text= get_post_meta($post->ID, 'my_metabox_name' , true );
    wp_editor(htmlspecialchars_decode($text), 'meta_box_id', $settings = array('textarea_name'=>'my_input_name') );
}

add_action( 'save_post', function($post_id) {
    if (!empty($_POST['my_input_name'])) {
        $data=htmlspecialchars($_POST['my_input_name']); //make sanitization more strict !!
        update_post_meta($post_id, 'my_metabox_name', $data );
    }
}); 

Then display the data in your page template like…

<?php
    $more_content = get_post_meta($post->ID, 'my_metabox_name', true);
    echo $more_content;
?>