skip to Main Content

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 seem to be going back and forth between the white screen of death, and the backend admin menu just flashing without giving any kind of error or message. The data does not get written to the database either.

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 admin_url('admin-post.php'); ?>" enctype="multipart/form-data">
            <label>Date and Time:</label>
            <input type="datetime-local" name="datetime"><br><br>2
            <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 here is the function and hook in the functions.php file

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 this manages to do is reload the plugins admin page without doing anything else or throwing any errors.. How is this supposed to be done exactly?

2

Answers


  1. Chosen as BEST ANSWER

    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. Posting to admin-post is a really old methodology from what I can see.

    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 the values were actually getting passed through successfully all along, just without traditional indicators.


  2. The action is empty and loads the same page on submit which is the default behaviour for form without or empty action param.

    Add echo admin_url( ‘admin-post.php’)

    <form method="post" action="<?php echo admin_url('admin-post.php'); ?>" 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search