Updating the_post content in a plugin is not working

Hi everyone,

I’m writing a plugin that adds a custom post type with some meta fields. Then, using the ‘the_post’ hook, if the current post is that custom post type and if those meta fields are there, I update the $post_content to contain the information from those meta fields in a specifically-formatted way.

I was developing this locally, and testing with the default Twenty Twenty theme and everything worked fine. When I tried to install my plugin on my live site, which uses X theme, the content is not getting updated as I would expect. I also deactivated every other plugin to make sure that a plugin wasn’t causing the problem. On my live site, I temporarily switched to another theme, and the plugin worked as expected. I switched back to X theme, and it’s not. I then installed and activated X theme locally and saw the same result. This was to make sure I hadn’t done anything within the X settings on my live site that would be causing this problem. With a pure vanilla install of X, this issue was still there.

I’m hoping someone can shed some light on what I need to do differently to make this work for x-theme. Here are the details:

Wordpress version: 5.4.2
X version: 7.2.3
Stack: Integrity

Here is the code I am using in my plugin:

function my_custom_post_type_the_post($post_object)
{
    // The post object is passed to this hook by reference so there is no need to return a value.
    if(
        $post_object->post_type == 'my_custom_post_type' &&
        ( is_post_type_archive('my_custom_post_type') || is_singular('my_custom_post_type') ) &&
        ! is_admin()
    ) {

        $video = get_post_meta($post_object->ID, 'my_custom_post_type_video_url', true);
        $mp3 = get_post_meta($post_object->ID, 'my_custom_post_type_mp3_url', true);
        $textfield = get_post_meta($post_object->ID, 'my_custom_post_type_textfield', true);

        // Convert meta data to HTML-formatted media content
        $media_content = $this->create_media_content($video, $mp3, $bible);

        // Prepend $media_content to $post_content
        $post_object->post_content = $media_content . $post_object->post_content;
    }
}
add_action( 'the_post', 'my_custom_post_type_the_post' );

Again, this is working perfectly in the Twenty Twenty default WordPress theme, but not with my X setup for some reason. Any help would be appreciated.

EDIT: I just now tried this plugin with some other themes. It works in Twenty Twenty, but it does not display in Twenty Nineteen or Twenty Seventeen either. I can’t imagine what the problem is.

Hi @elimtaft,

Thanks for reaching out.

As you’ve said your custom code doesn’t work either on Twenty Nineteen or Twenty Seventeen. Regretfully, we can’t provide custom development because it is outside the scope of our theme support. Please ask help for a 3rd party developer or you can avail our One program where it will answer all your questions.

Tips: Use add_filter instead of add_action because modifying post content should be done with filter, the action doesn’t return the processed value even though the changes are added to post_content.

Hope that helps and thank you for understading.

Hi @cramaton,

Thank you for the reply. You are correct that it was an issue with how I was trying to modify the content. I was able to resolve my problem by changing add_action to add_filter, filtering on the_content instead of on the_post, and returning the content instead of trying to update $post->post_content directly. Here is the code that worked for me:

function my_custom_post_type_the_content($content_object)                                                                                                                                                                                 
{
    global $post;

    if( ($post->post_type == 'my_custom_post_type') && (is_singular('my_custom_post_type') || is_archive()) && ! is_admin() )
    {
        $video = get_post_meta($post->ID, 'my_custom_post_type_video_url', true);
        $mp3 = get_post_meta($post->ID, 'my_custom_post_type_mp3_url', true);
        $textfield = get_post_meta($post->ID, 'my_custom_post_type_textfield', true);

        if( is_archive() )
        {
            if ($textfield != '')
            {
                $textfield_div = $this->format_textfield_html($textfield);
            }
            return $textfield_div . '<a class="smet0_exerpt_link" href="' . get_permalink($post) . '">Listen</a>';
        }

        $media_content = $this->create_media_content($video, $mp3, $textfield);
        $content_object = $media_content . $content_object;
    }
    return $content_object;
}
add_filter( 'the_content', 'my_custom_post_type_the_content');

Glad to know that the issue is solved.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.