admin管理员组文章数量:1122846
I've been trying to get a basic plugin to work and I have followed what seems to be the flow to get the form to post to admin-post.php.
However, I only seem to get the white screen of death upon form submission.
This is my main plugin file
<?php
/**
* Plugin Name: Test form
*/
function wp_meetings_menu(){
add_menu_page('Meetings', 'Meetings', 'manage_options', 'wp_meetings', 'wp_meetings_form');
}
add_action('admin_menu', 'wp_meetings_menu');
function wp_meetings_form() {
?>
<div class="wrap">
<h2>Meetings</h2>
<form method="post" action="<?php echo admin_url('admin-post.php'); ?>" enctype="multipart/form-data">
<label>Date and Time:</label>
<input type="datetime-local" name="datetime"><br><br>
<label>Drop Down 1:</label>
<select name="dropdown1">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select><br><br>
<label>Drop Down 2:</label>
<select name="dropdown2">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select><br><br>
<label>Drop Down 3:</label>
<select name="dropdown3">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select><br><br>
<label>PDF File:</label>
<input type="file" name="pdf_file"><br><br>
<input type="submit" name="submit" value="Submit">
<input type="hidden" name="action" value="wp_meetings_submit" />
</form>
</div>
<?php
}
?>
and this is the code in the functions.php file that is supposed to handle that submission to admin-post.php
// Handle form submission and database insertion
add_action( 'admin_post_wp_meetings_submit', 'wp_meetings_submit' );
add_action( 'admin_post_nopriv_wp_meetings_submit', 'wp_meetings_submit' );
function wp_meetings_submit(){
global $wpdb;
if(isset($_POST['submit'])){
$datetime = $_POST['datetime'];
$dropdown1 = $_POST['dropdown1'];
$dropdown2 = $_POST['dropdown2'];
$dropdown3 = $_POST['dropdown3'];
$pdf_file = $_FILES['pdf_file']['name'];
$upload_dir = wp_upload_dir();
$pdf_path = $upload_dir['path'] . '/' . $pdf_file;
move_uploaded_file($_FILES['pdf_file']['tmp_name'], $pdf_path);
$wpdb->insert(
'wp_meetings',
array(
'datetime' => $datetime,
'dropdown1' => $dropdown1,
'dropdown2' => $dropdown2,
'dropdown3' => $dropdown3,
'pdf_file' => $pdf_file
),
array(
'%s',
'%s',
'%s',
'%s',
'%s'
)
);
}
}
All I get when posting the form is a white screen of death with a blank die message error box.
I configured the WP_DEBUG to be true, but when recreating the error again, no debug log is generated.
I was wondering what is the correct and full way of accomplishing this?
I've been trying to get a basic plugin to work and I have followed what seems to be the flow to get the form to post to admin-post.php.
However, I only seem to get the white screen of death upon form submission.
This is my main plugin file
<?php
/**
* Plugin Name: Test form
*/
function wp_meetings_menu(){
add_menu_page('Meetings', 'Meetings', 'manage_options', 'wp_meetings', 'wp_meetings_form');
}
add_action('admin_menu', 'wp_meetings_menu');
function wp_meetings_form() {
?>
<div class="wrap">
<h2>Meetings</h2>
<form method="post" action="<?php echo admin_url('admin-post.php'); ?>" enctype="multipart/form-data">
<label>Date and Time:</label>
<input type="datetime-local" name="datetime"><br><br>
<label>Drop Down 1:</label>
<select name="dropdown1">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select><br><br>
<label>Drop Down 2:</label>
<select name="dropdown2">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select><br><br>
<label>Drop Down 3:</label>
<select name="dropdown3">
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
<option value="option3">Option 3</option>
</select><br><br>
<label>PDF File:</label>
<input type="file" name="pdf_file"><br><br>
<input type="submit" name="submit" value="Submit">
<input type="hidden" name="action" value="wp_meetings_submit" />
</form>
</div>
<?php
}
?>
and this is the code in the functions.php file that is supposed to handle that submission to admin-post.php
// Handle form submission and database insertion
add_action( 'admin_post_wp_meetings_submit', 'wp_meetings_submit' );
add_action( 'admin_post_nopriv_wp_meetings_submit', 'wp_meetings_submit' );
function wp_meetings_submit(){
global $wpdb;
if(isset($_POST['submit'])){
$datetime = $_POST['datetime'];
$dropdown1 = $_POST['dropdown1'];
$dropdown2 = $_POST['dropdown2'];
$dropdown3 = $_POST['dropdown3'];
$pdf_file = $_FILES['pdf_file']['name'];
$upload_dir = wp_upload_dir();
$pdf_path = $upload_dir['path'] . '/' . $pdf_file;
move_uploaded_file($_FILES['pdf_file']['tmp_name'], $pdf_path);
$wpdb->insert(
'wp_meetings',
array(
'datetime' => $datetime,
'dropdown1' => $dropdown1,
'dropdown2' => $dropdown2,
'dropdown3' => $dropdown3,
'pdf_file' => $pdf_file
),
array(
'%s',
'%s',
'%s',
'%s',
'%s'
)
);
}
}
All I get when posting the form is a white screen of death with a blank die message error box.
I configured the WP_DEBUG to be true, but when recreating the error again, no debug log is generated.
I was wondering what is the correct and full way of accomplishing this?
Share Improve this question asked Mar 20, 2023 at 17:48 Yintii.ethYintii.eth 11 bronze badge 9 | Show 4 more comments1 Answer
Reset to default 0First thank you to @TomJNowell for helping me to figure this out. There was a few misleading assumptions I made that had me making this harder than it needed to be.
There's no need to post to admin-post, keeping the action blank to submit to the current page is perfectly fine.
I don't seem to need to add the url parameter that wordpress automatically adds either.
The url parameters do not get placed in the url bar, leading me to believe it was not getting posted. (Assumption) Upon adding this to the file:
<?php
if(isset($_POST['submit'])){
echo "Submit detected!";
print_r($_POST);
}
?>
I was able to see that the input data was very much getting posted and I just need to take it from this point.
Thanks again !
本文标签: pluginsSubmitting form to adminpostphp Wordpress
版权声明:本文标题:plugins - Submitting form to admin-post.php Wordpress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736289895a1928400.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
WP_DEBUG
on its own won't create an error log – Tom J Nowell ♦ Commented Mar 20, 2023 at 18:28?>
from the end of your plugins PHP file. You can also comment out parts of the code to determine which part is causing the problem. Additionally, how are you testing this? Nothing in your admin post handler redirects the user or displays any output, so of course the user is not redirected and sees nothing.admin-post.php
is also a very very old API, you can also let the page that serves the form also be the form handler – Tom J Nowell ♦ Commented Mar 20, 2023 at 19:54