Wordpress - 前端上的自定义帖子图片无法上传

时间:2016-06-22 04:01:14

标签: php wordpress

我有一个表格来上传附件

<form id="post-job-form" class="frontend-form" action="" method="post" enctype="multipart/form-data" role="form">
    <?php
        $status_message = '';

        if( isset( $_GET['message'] ) ){
            $status_message = $_GET['message'];
        }

        // check if user have post a company
        $check = get_posts( array( 'post_type' => 'company', 'author' => get_current_user_id() ) );
        if( $check == null ){
            $status_message = '6';
        }//endif;

        jobboard_set_post_message( $status_message );
    ?>

    <div class="form-group">
        <label for="sallary_periode"><?php _e( 'Salaire Periode', 'jobboard' ); ?></label>
        <select name="sallary_periode" id="sallary_periode" class="form-control">
            <?php
                $sallary_periode = array(
                    'hourly' => 'Hourly',
                    'daily' => 'Daily',
                    'weekly' => 'Weekly',
                    'monthly' => 'Monthly'
                );
                foreach( $sallary_periode as $periode_key => $periode_value ) {
                    $selected = '';
                    if( $default['sallary_periode'] == $periode_key ){
                        $selected = 'selected';
                    }
                    echo '<option value="'.$periode_key.'" '.$selected.'>'.esc_attr($periode_value).'</option>';
                }
            ?>
        </select>
    </div><!-- /.form-group -->


    <div class="form-group">
        <label for="job_img"><?php _e( 'L`image (en option)', 'jobboard' ); ?></label>
        <?php
            if( isset($_GET['action']) && $_GET['action'] == 'edit' ){
                $edit = get_post( $_GET['jid'] );
                $jobimag = get_post_meta( $edit->ID, 'jbchild_meta_job_image', true );
                if(!empty($jobimag)){

                    echo '<img src="' . $jobimag . '">';

                }//!empty
            }//endif;
        ?>
        <input class="" type="file" name="job_photo" id="job_photo" accept="image/*" />
        <span class="help-block"><?php _e( 'Télécharger éventuellement votre image pour les candidats pour voir', 'jobboard' ); ?></span>
    </div><!-- /.form-group -->

    <?php
        if( isset( $_GET['action'] ) && $_GET['action'] == 'edit' ){
            $button_text = __( 'Update Job', 'jobboard' );
    ?>
        <input type="hidden" name="form_type" id="form_type" value="edit_post_job" />
        <input type="hidden" name="post_id" id="post_id" value="<?php echo esc_attr( $default['post_id'] ); ?>" />
    <?php
        }else{
            $button_text = __( 'Post A Job', 'jobboard' );
    ?>
        <input type="hidden" name="form_type" id="form_type" value="post_job" />
    <?php
        }
    ?>
    <input type="hidden" name="user_id" id="user_id" value="<?php echo esc_attr( get_current_user_id() ); ?>" />
    <button type="submit" name="submit" class="btn btn-post-resume"><?php echo esc_attr( $button_text ); ?></button>

</form>

这是我的职责:

function jobboard_post_job_123( $data = array(), $files = array(), $update = false ){
    $post_status = 'publish';

    if(jobboard_option('enable_highlight_package_job') == '1'){
        $post_status ='publish';
    }

    $job_args = array(
        'post_content'      => $data['job_description'],
        'post_title'        => $data['job_title'],
        'post_status'       => $post_status,
        'post_type'         => 'job',
        'post_author'       => $data['user_id'],
    );

    $message = '1';

    if( $update ){
        $job_args['ID'] = $data['post_id'];
        $job_args['post_status'] = get_post_status( $data['post_id'] );
        $message = '2';
    }

    $job_id = wp_insert_post( $job_args );

    if($job_id){
        if( isset( $data['job_region'] ) ){
            wp_set_object_terms( $job_id, $data['job_region'], 'job_region' );
        }
        if( isset( $data['job_type'] ) ){
            wp_set_object_terms( $job_id, $data['job_type'], 'job_type' );
        }
        if( isset( $data['job_category'] ) ){
            wp_set_object_terms( $job_id, $data['job_category'], 'job_category' );
        }


        // Job Company Metabox
        update_post_meta( $job_id, '_jboard_job_company', $data['job_company'] );

        // Job Experience Metabox
        update_post_meta( $job_id, '_jboard_job_experiences', $data['job_experience'] );

        // Job Salary Metabox
        update_post_meta( $job_id, '_jboard_job_sallary', $data['job_sallary'] );

        // Job Salary Periode
        update_post_meta( $job_id, '_jboard_sal_periode', $data['sallary_periode'] );

        // Job Summary Metabox
        update_post_meta( $job_id, '_jboard_job_summary', $data['job_summary'] );

        // Job Overview Metabox
        update_post_meta( $job_id, '_jboard_job_overview', $data['job_overview'] );

        if( !empty( $files['job_photo']['name'] ) ){
            $job_img = jobboard_file_upload( $files['job_photo'], 'file' );
            $old_job_img = get_post_meta( $job_id, 'jbchild_meta_job_image', true );
            if($job_img){
                //update_post_meta( $job_id, 'jbchild_meta_job_image', $job_img['url'], $old_job_img  ); 
                update_post_meta( $job_id, 'jbchild_meta_job_image', 'http://www.soslivreur.fr/wp-content/uploads/2016/04/20160316_174725.jpg' );
            }
        }           

        // Job metabox data set
        $job_meta = array(
            '_jboard_sal_periode'
        );
        update_post_meta( $job_id, 'jobboard_job_mb_fields', $job_meta );

        wp_redirect( esc_url(add_query_arg( array( 'action' => 'edit', 'jid' => $job_id, 'message' => $message )  ) ) );
        exit;
    }
}

我有问题。当我提交此表单时,其他字段可以插入数据库但图像无法插入数据库。

如何修复我的代码?谢谢

1 个答案:

答案 0 :(得分:0)

请在函数文件函数名jobboard_post_job_123

中替换此行代码
if( !empty( $files['job_photo']['name'] ) ){
            $job_img = jobboard_file_upload( $files['job_photo'], 'file' );
            //$old_job_img = get_post_meta( $job_id, 'jbchild_meta_job_image', true );
            if($job_img != ''){
                update_post_meta( $job_id, 'jbchild_meta_job_image', 'http://www.soslivreur.fr/wp-content/uploads/2016/04/20160316_174725.jpg' );
            }
        }

然后检查它的工作与否,因为你已经预定了旧的工作形象。