0

In a plugin I've created a custom post type that stores xml representing models in the respective posts' content.

I've also created a custom meta box that allows the user to edit the xml with a graphic modeler. The changed xml gets stored in a hidden field's value.

When the user clicks Save Draft or Publish I want to save this value as the post's content. By using the save_post action the content gets saved, however I get redirected to a dead page with ERR_CONNECTION_RESET after a very long loading time when clicking on Save Draft.

This is my save_post action:

class MetaBoxes
{
    .
    .
    public function saveModel($postId, $post, $update)
    {
        if (get_post($postId)->post_type != $this->post_type)
        {
            return;
        }

        if (array_key_exists(self::INPUT_ID, $_POST))
        {
            wp_update_post([
                'ID' => $postId,
                'post_content' => urldecode($_POST[self::INPUT_ID])
            ]);
        }
    }
    .
    .
}

I know there are security issues in the current state. Let's ignore them until the main functionality is fixed.

Thanks in advance!

1

I think you may be in a recursive loop. if you hooked into save_post that will be triggered again when you call wp_update_post.

you can temporarily remove the hook. Not knowing exactly how you have written the hook the below code is a guess.

remove_action( 'save_post', [ $this, 'saveModel'] );

wp_update_post([
    'ID' => $postId,
    'post_content' => urldecode($_POST[self::INPUT_ID])
]);

add_action( 'save_post', [ $this, 'saveModel'] );
| improve this answer | |
  • That does work, thanks! However it doesn't seem like the best solution to me. Shouldn't I rather use another action or a filter? – felixlinker Mar 8 '17 at 11:40
  • @felixlinker This is the recommended method on WordPress Codex. I don't know if there is another way to update the post_content without the use of wp_update_post – Laxmana Mar 8 '17 at 12:28
  • Okay, thanks! I'll stick to this solution. Makes me kind of sad anyways.. – felixlinker Mar 8 '17 at 12:53

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.