【问题标题】:Can't get Wordpress form submission to work无法让 Wordpress 表单提交工作
【发布时间】:2018-09-12 20:30:54
【问题描述】:

我知道有人反复问过这个问题,但我找到的解决方案都不适合我。我有一个简单的 wordpress 插件,它通过页面上的简码输出表单 html。当我填写表单时,它没有点击表单操作回调,我不知道为什么。这是我的代码:

<?php
/*
 Plugin Name: Form Submit Test
 Plugin URI: http://10.0.2.15/wp-content/plugins/form-submit-test
 Description: Testing to get these forms to submit
 Version: 1.0.0
 Author: Randy Young
 Author URI: http://example.com
 License: GPLv2 or later
*/


// security check
defined('ABSPATH') or  die('Illegal Access');
define( 'FormSubmitTest_VERSION', '1.0.0' );

class FormSubmitTest
{


    function fst_shortcode() {
        return $this->html_form_code();
    }

    private function html_form_code() {
        error_log('html_form_code()');
        $html = '';
        $html .= '<form action="' . esc_url( admin_url('admin-post.php')) . '" method="post">'; //
        $html .= '<input type="hidden" name="action" value="fst_action_hook"';
        $html .= '<p>';
        $html .= 'Your Name (required) <br />';
        $html .= '<input type="text" name="cf-name" pattern="[a-zA-Z0-9 ]+" value="' . ( isset( $_POST["cf-name"] ) ? esc_attr( $_POST["cf-name"] ) : '' ) . '" size="40" />';
        $html .= '</p>';
        $html .= '<p>';
        $html .= 'Your Email (required) <br />';
        $html .= '<input type="email" name="cf-email" value="' . ( isset( $_POST["cf-email"] ) ? esc_attr( $_POST["cf-email"] ) : '' ) . '" size="40" />';
        $html .= '</p>';
        $html .= '<p>';
        $html .= 'Subject (required) <br />';
        $html .= '<input type="text" name="cf-subject" pattern="[a-zA-Z ]+" value="' . ( isset( $_POST["cf-subject"] ) ? esc_attr( $_POST["cf-subject"] ) : '' ) . '" size="40" />';
        $html .= '</p>';
        $html .= '<p>';
        $html .= 'Your Message (required) <br />';
        $html .= '<textarea rows="10" cols="35" name="cf-message">' . ( isset( $_POST["cf-message"] ) ? esc_attr( $_POST["cf-message"] ) : '' ) . '</textarea>';
        $html .= '</p>';
        $html .= '<p><input type="submit" name="cf-submitted" value="Send"/></p>';
        $html .= '</form>';

        return $html;
    }
}


// instantiate the FormSubmitTest class
if( class_exists('FormSubmitTest')) {
    $formSubmitTest = new FormSubmitTest();
}

add_action( 'wp_post_nopriv_fst_action_hook', 'fst_action_function' ); // need this to serve non logged in users
add_action( 'wp_post_fst_action_hook', 'fst_action_function' );
// THE FUNCTION
function fst_action_function() {

    error_log('fst_action_function()');
    
    wp_redirect(admin_url('admin.php?page=10.0.2.15/index.php/ringwood2/'));
    exit;
}

// create a shortcode to add to the page
add_shortcode('form_submit_test', array( $formSubmitTest, 'fst_shortcode'));

?>

我看到了 error_log('html_form_code()');导致 debug.log,但我没有看到 error_log('fst_action_function()');回调的结果。我也进入了 wp-admin/admin-post.php 页面。

任何帮助将不胜感激。

【问题讨论】:

    标签: wordpress forms plugins submission


    【解决方案1】:

    您似乎正在尝试通过 AJAX 提交表单,但操作有误。对于 AJAX,它应该是 wp_ajax,而不是 wp_post。

    add_action( 'wp_ajax__nopriv_fst_action_hook', 'fst_action_function' ); // need this to serve non logged in users
    add_action( 'wp_ajax_fst_action_hook', 'fst_action_function' );
    

    您可以在此处找到更多信息:wp_ajax_{action}_。这基本上允许您通过 AJAX 提交表单,但是,您不能以您尝试的方式重定向。使用 AJAX,您通常会返回一个响应,然后在 Javascript 中对该响应执行一些操作,例如显示一条消息(或重定向)。

    如果您希望简单地发布没有 AJAX 的表单,您应该考虑使用 init 操作。然后就可以监听表单提交了。比如,

    add_action( 'init', 'listen_for_form_submissions' );
    
    function listen_for_form_submissions() {
        if( isset( $_POST['action'] ) && $_POST['action'] === 'fst_action_hook' ) {
            error_log('fst_action_function()');
    
            wp_redirect(admin_url('admin.php?page=10.0.2.15/index.php/ringwood2/'));
            exit;
        }
    }
    

    【讨论】:

    • 感谢克里斯的回答。我很困惑。是什么让你相信我的意图是使用 ajax?我正在尝试使用 wp_post_{action} 方法。我究竟做错了什么?使用 ajax 会更好吗?
    • 我不相信 wp_post_{action} 是一个有效的钩子。我在 WP 文档中找不到任何内容
    • 并且 nopriv_ 标志是 wp_ajax 的文档化元素
    • 谢谢克里斯。根据我看到的示例,您示例中的挂钩将是 wp_post_nopriv_listen_for_form_submissions (用于未登录用户)和 wp_post_listen_for_form_submissions 用于登录用户。无论如何,我接受了你的建议来使用“init”动作。它工作得很好。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    • 2011-07-05
    • 2019-09-24
    • 1970-01-01
    • 2015-08-31
    相关资源
    最近更新 更多