最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Form Processing

programmeradmin3浏览0评论

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
  • 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 Commented Apr 14, 2020 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 Commented Apr 14, 2020 at 18:54
  • Is your form PHP also processing the POST data? – jdm2112 Commented Apr 14, 2020 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 Commented Apr 14, 2020 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 Commented Apr 14, 2020 at 19:21
 |  Show 1 more comment

3 Answers 3

Reset to default 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">

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论