Javascript Wordpress无法保存特色图像

Javascript Wordpress无法保存特色图像,javascript,php,wordpress,plugins,themes,Javascript,Php,Wordpress,Plugins,Themes,这是Nadeem我是PHP和WordPress的初学者,我在自定义插件开发过程中遇到了一个问题,我的问题是,无法从前端保存特色图像和自定义分类标签和类别。与此同时,我已经在谷歌、StackOverflow等网站上搜索了太多关于这个错误的解决方案,但不幸的是,这不起作用,我在过去两周一直停留在这一点上 这是我的插件PHP代码 /** * The Template for displaying car archives, including the main car-data page which

这是Nadeem我是PHP和WordPress的初学者,我在自定义插件开发过程中遇到了一个问题,我的问题是,无法从前端保存特色图像和自定义分类标签和类别。与此同时,我已经在谷歌、StackOverflow等网站上搜索了太多关于这个错误的解决方案,但不幸的是,这不起作用,我在过去两周一直停留在这一点上

这是我的插件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' ) ); 

        add_action( 'init', array( $this, 'submit_car_data_post' ) ); 
    }

    public function submit_car() {

        echo '<div id="submit_car_form">';
        echo '<form id="cd_car" name="cd_car" class="cd_car" method="post" action="'.$_SERVER['PHP_SELF'].'" enctype="multipart/form-data" >';

        wp_nonce_field( "car-frontend-post" );

        echo '<p><label for="title">Title</label><br />';
        echo '<input type="text" id="title" value="" size="60" name="title" />';
        echo '</p>';

        echo '<p>';
        echo '<label for="content">Post Content</label><br />';
        echo '<textarea id="content" name="content" cols="40" rows="4"></textarea>';
        echo '</p>';

        echo '<p><label for="feature">Feature:</label><br />';
         wp_dropdown_categories( "show_option_none=Feature&tab_index=3&taxonomy=feature&name=feature&id=feature&class=feature" ) .'</p>';

        echo '<p><label for="make">Make:</label><br />';
        wp_dropdown_categories( "show_option_none=Make&tab_index=4&taxonomy=make&name=make&id=make&class=make" ) .'</p>';

        echo '<p><label for="post_tags">Tags</label><br />';
        echo ' <input type="text" value="" size="60" name="post_tags" id="post_tags" /></p>';

        echo '<p><label for="post_tags">Car Image</label><br />';
        echo '<input type="file" name="file" id="file"></p>';

        echo '<p align="left"><input type="submit" tabindex="6" id="submit_car" name="submit_car" /></p>';
        echo '</form>';
        echo '</div>';

        return;
    }

    public function submit_car_data_post() {

        if ( isset($_POST['title']) ) {

            echo '<script>console.log("file error: '.$fileerror.'<br>");</script>';

            $title      = sanitize_text_field( $_POST['title'] );
            $content    = sanitize_text_field( $_POST['content'] );
            $author_id  = sanitize_text_field( $_POST['author_id'] );
            $tags_input = sanitize_text_field( array($_POST['post_tags']) );
            $feature    = sanitize_text_field( array($_POST['feature']) );
            $make       = sanitize_text_field( array($_POST['make']) );
            // $image      = $_POST['file'];

            $args = array(
                'post_title'    => $title,
                'post_content'  => $content,
                'tags_input'    => $_POST['post_tags'],
                'author'        => $author_id,
                'post_status'   => 'publish',
                'post_type'     => 'car-data',
                'tax_input'     => array(
                    $feature,
                    $make
                ),
                // 'post_category'     => array(
                //     'feature' => array($_POST['feature']),
                //     'make' => array($_POST['make'])
                // )
            );

            // Check that the nonce was set and valid
            if( !wp_verify_nonce($_POST['_wpnonce'], 'car-frontend-post') ) {
                return;
            }

            if( is_wp_error( $posts ) ){
                echo json_encode( $posts->get_error_messages() );
            }

            // save frontend Post
            $posts = wp_insert_post( $args );

            // save frontend Texonomy
            wp_set_object_terms( $posts, $feature, 'feature' );
            wp_set_object_terms( $posts, $make, 'make' );

            // save Atachment on featured image for the current custom post
            $uploaddir = wp_upload_dir();
            $file = $_FILES[$image];
            $uploadfile = $uploaddir['path'] . '/' . basename( $file );

            move_uploaded_file( $_FILES['file']['tmp_name'], $uploaddir['path']  . '/' . $filename );
            $filename = basename( $uploadfile );

            $wp_filetype = wp_check_filetype(basename($filename), null );

            $attachment = array(
                'guid'           =>  $wp_upload_dir['url'] . '/' . basename( $filename ), 
                'post_mime_type' =>  $wp_filetype['type'],
                'post_title'     =>  preg_replace('/\.[^.]+$/', '', $filename),
                'post_content'   =>  preg_replace('/\.[^.]+$/', '', $filename),
                'post_excerpt'   =>  preg_replace('/\.[^.]+$/', '', $filename),
                'post_status'    => 'inherit', 
                'post_type'      => 'attachment',
                'menu_order'    => $_i + 1000,
            );
            $attach_id = wp_insert_attachment( $attachment, $uploadfile );
            require_once( ABSPATH . 'wp-admin/includes/image.php' );
            $attach_data = wp_generate_attachment_metadata($attach_id, $filename );
            wp_update_attachment_metadata( $attach_id, $attach_data );  

            wp_die();            
        }
    } 

    public function my_enqueue() {

        wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
    }
}

此外,如果(isset($\u POST['title'])无法使用isset POST上的
submit
键,则代码
if(isset($\u POST['title'))
上有一个错误,只有
title
在这里工作

我相信您的问题是由于
清理文本字段(数组(..)
,所以我假设通过发送一个数组,它实际上总是看到字符串“Array”-而不是您的POST值

要清理阵列,可以执行以下操作:

$tags_input = array();
if ( isset($_POST['post_tags']) && !empty($_POST['post_tags']) ) {
    foreach ($_POST['post_tags'] as $val)
       $tags_input[] = sanitize_text_field( $val );
}
// [ .. repeat for feature & make .. ]
或者,如果它们只是单个值(不是数组),只需删除
数组
包装器即可

$tags_input = array();
if ( isset($_POST['post_tags']) && !empty($_POST['post_tags']) ) {
    foreach ($_POST['post_tags'] as $val)
       $tags_input[] = sanitize_text_field( $val );
}
// [ .. repeat for feature & make .. ]