0

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?

9
  • 1
    did you look at your PHP error log? There is always a PHP error message with the real problem, a white screen of death just means you have to go and find the error log to discover what really happened. WP_DEBUG on its own won't create an error log
    – Tom J Nowell
    Mar 20, 2023 at 18:28
  • @TomJNowell yes, and strangely enough there's nothing in there either.
    – Yintii.eth
    Mar 20, 2023 at 19:02
  • at a minimum, remove the ?> 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
    Mar 20, 2023 at 19:54
  • @TomJNowell thanks for getting back on this post. I removed the closing php tag now. I am testing this on a local env with MAMP. I am not familiar with this process and have been trying to piece together the solution from various other answers. The final input field <input type="hidden" name="action" value="wp_meetings_submit" /> Should be what triggers the function in the functions.php file from what I have read and looked into. If admin-post is the old way of doing this, what would be considered the up-to-date method?
    – Yintii.eth
    Mar 20, 2023 at 20:07
  • it's very likely that it's working as intended but once it's processed your request there is no code to forward the user on to the next step, there are no redirects in your code. Also the answer to your follow up question is in the comment it is replying to
    – Tom J Nowell
    Mar 20, 2023 at 20:15

1 Answer 1

0

First 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 !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.