我同时是一个构建自定义插件,因此,与此同时,我仍停留在前端表单上,但是目前,我无法将数据发布或保存到数据库中。 我已经搜索过Google诗歌,并且在PHP回调中具有die()函数,并且我相信add_actions是正确的。
我正在本地主机上工作,尽管我怀疑会影响它,而这一切都在管理员而不是前端中。我还检查了js是否已入队和本地化。
我在chrome开发人员区域收到200 OK消息。 也许有人可以帮助我解决我遇到的一个小问题。我正在尝试使用以下代码为Wordpress帖子创建提交表单。
<?php
/**
* The Template for displaying car archives, including the main car-data page which is a post type archive.
*
* Override this template by copying it to yourtheme/car_data/archive-car-data.php
*
* @author Shaikh Nadeem
* @package Car_Data
* @subpackage Car_Data/Templates
* @version 1.0.0
*/
/**
*
*/
class Car_Frontend_Form
{
function __construct()
{
add_shortcode( 'submit_car', array($this, 'submit_car') );
add_action( 'wp_enqueue_scripts', array( $this, 'my_enqueue' ) );
add_action( 'save_post_submit_car_data_post', array( $this, 'submit_car_data_post' ) );
add_action( 'wp_ajax_nopriv_submit_car_data_post', array( $this, 'submit_car_data_post' ) );
add_action( 'wp_ajax_submit_car_data_post', array( $this, 'submit_car_data_post' ) );
}
public function submit_car() {
$html = '<div id="submit_car_form">';
$html .= '<input type="hidden" name="author_id" value="<?php echo get_current_user_id(); ?>" />';
$html .= '<form id="cd_car" name="cd_car" class="cd_car" method="post" action=" " >';
$html .= '<p><label for="title">Title</label><br />';
$html .= '<input type="text" id="title" value="" tabindex="1" size="40" name="title" />';
$html .= '</p>';
$html .= '<p>';
$html .= '<label for="content">Post Content</label><br />';
$html .= '<textarea id="content" tabindex="3" name="content" cols="50" rows="6"></textarea>';
$html .= '</p>';
$html .= '<p>'. wp_dropdown_categories( "show_option_none=Category&tab_index=4&taxonomy=feature" ) .'</p>';
$html .= '<p><label for="post_tags">Tags</label>';
$html .= ' <input type="text" value="" tabindex="5" size="16" name="post_tags" id="post_tags" /></p>';
$html .= wp_nonce_field( "car-frontend-post" );
$html .= '<p align="right"><input type="submit" value="Publish" tabindex="6" id="submit_car" name="submit_car" /></p>';
$html .= '</form>';
$html .= '</div>';
return $html;
}
public function submit_car_data_post() {
// check_ajax_referer( 'ajax_securiy_nonce', 'security' );
if ( isset($_POST['title']) ) {
$title = sanitize_text_field( $_POST['post_title'] );
$content = sanitize_text_field( $_POST['content'] );
$author_id = sanitize_text_field( $_POST['author_id'] );
$tags_input = sanitize_text_field( $_POST['post_tags'] );
$category = sanitize_text_field( $_POST['cat'] );
$args = array(
'post_title' => $title,
'post_content' => $content,
'post_category' => $category,
'tags_input' => $tags_input,
'author' => $author_id,
'post_status' => 'draft',
'post_type' => 'car-data',
);
// Check that the nonce was set and valid
if( !wp_verify_nonce($_POST['_wpnonce'], 'car-frontend-post') ) {
echo '<script>alert("Did not save because your form seemed to be invalid. Sorry");</scipt>';
return;
}
if( is_wp_error( $posts ) ){
echo json_encode( $posts->get_error_messages() );
echo '<script>alert("is_wp_error!");</script>';
} else {
$post_preview = get_preview_post_link($posts);
echo $post_preview;
echo '<script>alert("'. $post_preview .'");</script>';
}
$posts = wp_insert_post( $args );
echo '<script>alert("Saved your post successfully!");</script>';
echo $posts;
wp_die();
}
}
public function my_enqueue() {
wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
}
}
```
Also i have custom post type to save on the same form but till here i not aed that field