WordPress.org

Support

Support » Plugins and Hacks » Hacks » [Resolved] What to put in the action parameter of the form tag in the plugin?

[Resolved] What to put in the action parameter of the form tag in the plugin?

  • Hi

    I don’t understand how wp plugin handle html form. Say, I develop a wp plugin that has a setting page which uses HTML form to handle input.

    <FORM action=”” method=”post”>

    What to put in the action parameter? Should it be blank or the filename of the plugin php file?

    I saw some sample code. They put in “options.php”. It made me even more puzzled because I have no idea where is that options.php file.

    Please help me understand.

    Thanks a lot!
    Narin

Viewing 3 replies - 1 through 3 (of 3 total)
  • options.php handles form submits from the Settings API. Another option is to leave it empty, in which case the submit request goes to the same page as the current one. Code pages often present forms on GET requests and process the form on POST requests, all from a single page. Or you can reference a second code page which handles the form submit, but such a page cannot access WP resources.

    To access WP resources, the request must go to a page post_type that uses a custom template or to either admin-ajax.php or admin-post.php or similar WP admin pages that load the WP environment for you, such as admin.php?page=your-settings-page-slug.

    As @bcworkz writes,

    typically a php page will check for $http_post like:

    $http_post = ('POST' == $_SERVER['REQUEST_METHOD']);

    and then issue code like:

    $user_login = '';
    $user_email = '';
    if ( $http_post ) {
        $user_login = $_POST['user_login'];
        $user_email = $_POST['user_email'];
        $errors = register_new_user($user_login, $user_email);
        if ( !is_wp_error($errors) ) {
            $redirect_to = !empty( $_POST['redirect_to'] ) ? $_POST['redirect_to'] : 'wp-login.php?checkemail=registered';
            wp_safe_redirect( $redirect_to );
            exit();
        }
    }

    That, is the part that handles when a form has already been posted. The code then proceeds to handle the ‘normal’ case: it outputs the html form that will be used for posting.

    You can also check out this plugin:

    http://www.xen.dds.nl/files/ft_signature_manager.php

    These are great answers, thanks!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] What to put in the action parameter of the form tag in the plugin?’ is closed to new replies.