0

I'm hooking into the_editor_content to add a signature to new posts. However, when a user edits their post the content is lost and replaced with the signature.

I tried something like the following in the hopes that the the_editor_content would only fire for new posts:

add_action( 'new_to_publish', 'my_new_post');
function my_new_post( $post ) {
    function my_editor_content( $content ) {
        $current_user = wp_get_current_user();
        $author_name = $current_user->display_name;
        $editor_content = '<br><br><br><br><br>--<br>'.$author_name;
        return $editor_content;
    }
    add_filter( 'the_editor_content', 'my_editor_content', 'tinymce' );
}

But it isn't working, I don't get the signature for new posts or any posts. Can this even be done this way? Note that my_editor_content works fine by itself.

Thanks in advance.

0

OK, so the new_to_publish hook is probably not the right hook to use here.

The status_to_status hooks run during a posts transition from the first status to the second. By this stage, the editor is no longer in play.

If my understanding of the_editor_content is correct, this filters the default content displayed in the editor when the post edit screen is loaded.

Do you need the signature to be appended to the actual post content within the database or just to display the on the front end?

If the latter, you could use the the_content filter...

function mh_add_signature_after_content( $content ) {
    global $post;

    $signature = '';

    // Specify the post type on which to display, otherwise you'll see on pages also!
    if ( 'post' == $post->post_type )   {

        $author_name = get_the_author_meta( 'display_name', 25 );

        $signature = '<br><br><br><br><br>--<br>' . $author_name;

    }

    return $content . $signature;
} // kbs_after_article_content
add_filter( 'the_content', 'mh_add_signature_after_content', 999 );

The result of this would be that in the post edit screen, the editor will display the post content, no signature. But when loaded on the front end of your site, the post will include the signature.

UPDATED

This should achieve what you want...

add_action( 'save_post', 'my_new_post');
function my_new_post( $post_id ) {

    // Specify the post type on which to run
    if ( 'post' == $post->post_type )   {

        // Prevent loops
        remove_action( 'save_post', 'my_new_post' );

        // Make sure we haven't already run for this post
        if ( get_post_meta( $post_id, 'signature_added', true ) )   {
            return;
        }

        $signature = '';
        $post      = get_post( $post_id );

        $author_name = get_the_author_meta( 'display_name', $post_id );

        $signature = '<br><br><br><br><br>--<br>' . $author_name;

        $update = wp_update_post( array(
            'ID'           => $post_id,
            'post_content' => $post->post_content . $signature
        ) );

        // Add a placeholder so we know the signature was added
        if ( ! is_wp_error( $update ) ) {
            add_post_meta( $post_id, 'signature_added', true, true );
        }

        add_action( 'save_post', 'my_new_post' );

    }

}
3
  • No, I need it actually in the database. I tried the_content but doesn't work for what I need. – jetgo Jan 13 '17 at 22:46
  • OK @jetgo I have updated my answer with another solution for you – Mike Jan 13 '17 at 23:08
  • thank you so much, this looked great, but it's not working. It's not appending the signature at the end of the content. – jetgo Jan 15 '17 at 19:48
-1

I went with a different approach to solve this (probably not the cleanest one but it works for me).

I parsed the current url and added the signature with the_editor_content if on the page for new posts.

If on the page for editing posts, I parsed the url again to get the post id and return the post content in the editor.

I have frontend posting forms so it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.