我正在使用一个类构建一个自定义的post类型插件。我在页面上有一个表单,它进行AJAX调用,然后返回400个错误请求和0个响应。
我试着用邮递员独立于页面拨打电话。还是一样的回答。
在构造函数中:
add_shortcode(\'websites_form\', [$this, \'renderForm\']);
// handle AJAX calls for both logged in and guest users
add_action(\'wp_action_websites_cpt_create_post\', [$this, \'create_post\']);
add_action(\'wp_action_nopriv_websites_cpt_create_post\', [$this, \'create_post\']);
其他两种方法:public function renderForm() {
wp_register_script(\'websites-cpt-process-form\', plugins_url(\'js/process-form.js\', __FILE__), null, null, true );
// vars being sent to JS
$php_vars = [
// passing the url to call for passing the form data back to WP
\'ajaxurl\' => admin_url( \'admin-ajax.php\', isset( $_SERVER["HTTPS"] ) ? \'https://\' : \'http://\' )
];
wp_localize_script( \'websites-cpt-process-form\', \'phpvars\', $php_vars );
wp_enqueue_script( \'websites-cpt-process-form\' );
return file_get_contents( plugin_dir_path( __FILE__ ) . "partials/websites-form.php" );
}
public function create_post() {
global $oLog;
$oLog->logrow(\'POST\', $_POST);
// verify nonce
// create post with name in title
// add url in metadata
// echo response
echo json_encode($_POST);
// die
die();
}
所以看起来wp_action-XXX
未注册操作。我试着把echo
\'在create_post()
方法,但输出中未显示任何内容。感谢您的任何建议。我不知道下一步该转向哪里。