1

I'm having an issue processing a form. The project was standalone PHP and I'm integrating it into Wordpress using this:

require_once("../../../wp-load.php");

The form element is defined as:

<form id="new_post" name="new_post" class="form-horizontal" method="post" enctype="multipart/form-data">

Now, I'm satisfied with the testing, I'm trying to move this in to a wordpress plugin and embed the form on the front end into a particular page using is_page()

Now, the form appears ok but when I submit the form, I get the theme's 404 page showing up instead of the desired output that I was getting in the test version.

I've tried changing the form element to:

<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?> id="new_post" name="new_post" class="form-horizontal" method="post" enctype="multipart/form-data">

but then I just get a white screen. No errors are showing up in my logs.

Any suggestions on what I'm doing wrong?

  • I would avoid loading wp-load.php directly, it's better to embed the form into WP than to embed WP into the form. You'll also never be able to use functions such as is_page etc, as there won't be a main query to check if that's true or not if you did this correctly, and no current post to test if it's a page or not. – Tom J Nowell Apr 14 at 18:52
  • Thanks Tom. I was only doing that while starting to implement new features. Now I’m trying to fully migrate it as the next step and that is where the problem has started. – TomC Apr 14 at 18:54
  • Is your form PHP also processing the POST data? – jdm2112 Apr 14 at 19:17
  • Yes and then it creates a post and sets a load of terms from the form inputs. All that was working in the test script. – TomC Apr 14 at 19:21
  • Try updating your action attribute. This is what I use when form and processing are in the same PHP. action="<?php echo esc_url( the_permalink() ); ?>" – jdm2112 Apr 14 at 19:21
1

I'm not sure what the root cause of your issue is. But I do see a syntax error in your last code block, which could be what's causing the white screen. Your "action" attribute is missing a close quote. Try this instead:

<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" id="new_post" name="new_post" class="form-horizontal" method="post" enctype="multipart/form-data">
| improve this answer | |
  • Well spotted, but unfortunately it wasn't that. – TomC Apr 15 at 1:34
  • Darn...sorry I couldn't be of more help. – Kevin Meboe Apr 15 at 1:43
1

I managed to get this working by adding a hidden field:

<input type="hidden" name="action" value="process_form">

And updating the form action to:

<form action="<?php echo admin_url( 'admin-post.php' ); ?>" id="new_post" name="new_post" class="form-horizontal" method="post" enctype="multipart/form-data">

Then I wrapped the form processing in a function:

function process_form() 

And then hooked it using:

add_action( 'admin_post_process_form', 'process_form' );

This works because sending to admin-post.php then looks for a function by the name within the form hidden field which can be hooked using

admin_post_YOUR_HIDDEN_VALUE_HERE

| improve this answer | |
1

I don't have enough reputation to add a comment to your answer, which is where this belongs. It's still useful information, so I'm adding another answer.

This post that I found today gives all the details for what you were trying to do: https://www.sitepoint.com/handling-post-requests-the-wordpress-way/

You've already figured this all out, but it might be helpful for other people who have the same question and want to see more details on how it all works.

If an admin can convert this into a comment under TomC's answer, that would probably be more useful.

| improve this answer | |

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.