【发布时间】: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