Support » Developing with WordPress » What is the best way to temporarily save draft post data on custom admin page

  • Resolved josiahmann

    (@josiahmann)



    I’m building a plugin that uses a custom admin page and submits a form. Currently, my form logic is hooked into the form’s custom admin_action hook, and if the validation fails, I’m redirecting back to my custom page with a serialized array of error codes in the url parameters.

    The problem is that, when redirecting back to the custom page, I no longer have access to the $_POST vars I submitted initially, so I don’t know the best way to store those values the user tried to submit.

    Would it make more sense to move that logic to the top of my custom page and call do_action('wp_insert_post', 'wp_insert_post'); directly so I can access the $_POST data still?

    The page I need help with: [log in to see the link]

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

    (@bcworkz)

    If you have your own page, I’m unclear why you would hook into anything. I could be missing something, so don’t worry about that too much. What I usually do is put the form and handler code all on the same page. What the page does is contingent upon the request type. If GET, just output the form. If POST, process the submitted data, calling wp_insert_post() or whatever when appropriate. The wp_insert_post() would fire its own action.

    BTW, calling do_action('wp_insert_post', 'wp_insert_post'); would start an infinite loop, don’t do that 🙂 )

    If needed, put up error messages and re-output the form with the submitted values from $_POST. Either way, the form submits to itself, so to speak, the form action attribute is blank and the method attribute is POST.

    The usual reason for hooking into an action is when we add custom fields to an existing WP form. Then we need to use that action to process our field. When our own page is doing everything, there’s no need for hooks at all, except maybe custom hooks for other devs to expand upon our plugin.

    Thanks so much for this response @bcworkz, very clear and helpful!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘What is the best way to temporarily save draft post data on custom admin page’ is closed to new replies.