Support » Developing with WordPress » Update post_content on backend custom post opening

  • Hello,
    I need to implement a function that substitutes the post_content of the current custom post as soon as it is opened in admin editor.
    What is the correct action/hook that allows me to get the informations about the currently opened post and modify it?

    I have developed this function but it is not working:

    function save_team() {
       $post = get_post();
       $ID_post = $post->ID;
       $team = get_post_meta ($ID_post, 'hometeam', true);
       $post_content = $post->post_content;
       //code that modifies post_content here by adding $team at some point
       $my_post = array(
            'ID'           => $ID_post,
            'post_content' => $post_content,);
    	  
         	wp_update_post( $my_post );
    }

    [Moderator note: code fixed. Please wrap code in the backtick character or use the code button.]

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator bcworkz

    (@bcworkz)

    Similar to the Loop, the post edit screen uses global $post; You can hook an action like “edit_form_top”, declare $post global, then do what you want with $post->post_content after verifying $post->post_type is the correct type and that the content had not already been added in.

    Thank you, it is working but I can only see the modified post content only after the second time that I open a post.
    It seems that the content is updated on the first opening but it’s not instantly visible inside the editor.
    So, at the very first time that I open a post, it still shows empty content but I need it to already be there.
    How can I achieve it?

    Moderator bcworkz

    (@bcworkz)

    The hook should impact all instances of the edit screen being opened, 1st, 2nd, 100th. Perhaps there is a flaw in the code that verifies post type and if the content has already been added or not? If you removed all conditionals, you should see the content is always added, even the first time, in all cases. Of course it’ll also be added to occurrences of the same content and other inappropriate conditions, but it is confirmation the hook is valid and where the problem lies.

    If that is not the case, you likely have your theme or a plugin interfering somehow. I’ve confirmed the hook works on the first instance of a new post in a new default installation.

    This is my sample code, if you confirm that there isn’t any flaw inside of it then it can possibly be a theme or plugin interference.
    I erased the empty post_content condition check but the content is still only showing after a second post opening.

    add_action( 'edit_form_top' , 'update_preset');
    
    function update_preset() {
    global $post;
    $ID_post = $post->ID;
    if($post->post_type == "match" && $post->post_status == "future"){
    $customContent = "some modified content";
    $my_post = array(
          'ID'           => $ID_post,
          'post_content' => $customContent,);
    	  
      	wp_update_post( $my_post );
    }
    }
    Moderator bcworkz

    (@bcworkz)

    Post status for new posts at ‘edit_form_top’ is “auto-draft” in my tests. You also need to add $post->post_content .= $customContent;. Saving the post does not update the current instance of $post. A good way to check values you’re not sure about is to use error_log(). For example:

    // verify post status
    error_log("Post status at 'edit_form_top' action: {$post->post_status}");

    then check the latest entry in your error logs.

    I was only missing $post->post_content .= $customContent;, saving the post was not enough indeed.
    Thank you so much!
    In the meanwhile, I was testing the “content_edit_pre” filter. This filter seems to work fine too, can you tell me if it’s an equally valid option or if it has any downside compared with “edit_form_top” action?

Viewing 6 replies - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.