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'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?
Share Improve this question asked Apr 14, 2020 at 17:43 TomCTomC 1,3168 silver badges18 bronze badges 6 | Show 1 more comment3 Answers
Reset to default 1I'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">
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
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/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.
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 asis_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 ♦ Commented Apr 14, 2020 at 18:52action
attribute. This is what I use when form and processing are in the same PHP.action="<?php echo esc_url( the_permalink() ); ?>"
– jdm2112 Commented Apr 14, 2020 at 19:21