无法保存自定义帖子类型(“ car-details”)帖子元字段数据

时间:2019-12-23 05:00:30

标签: wordpress plugins metadata custom-post-type

我尝试了太多解决方法,但无法解决,我认为我的save_car_data_meta($ post_id)无法用于调试是在第一个函数上执行var_dump {car_data_meta_box_output()}然后将函数保存为其他方式工作

admin / meta-boxes / class-car-data-meta-box.php

class Car_Data_Meta_Box_Data {

/**
 * Add job data meta box options.
 * Simple_Job_Board_Meta_box_Job_Data Class
 * 
 * @since   1.0.0
 */
public static function car_data_meta_box_output() {

    // Add a nonce field so we can check for it later.
    wp_nonce_field( 'car_data_meta_box_action', 'car_data_meta_box_nonce' );

    echo '<div class="admin-car-data-metabox">';
    echo '<table cellpadding="0" cellspacing="0" border="0" style="float: left;">';
    echo '<tbody>';
    Car_Data_Meta_box_Data::text('_condition', esc_html__('Condition', 'car-data'), '');
    Car_Data_Meta_box_Data::text('_make', esc_html__('Make', 'car-data'), '');
    Car_Data_Meta_box_Data::text('_model', esc_html__('Model', 'car-data'), '');
    Car_Data_Meta_box_Data::text('_registration_date', esc_html__('First Registration Date', 'car-data'), '');
    Car_Data_Meta_box_Data::text('_price', esc_html__('Price', 'car-data'), '');
    Car_Data_Meta_box_Data::text('_mileage', esc_html__('Mileage', 'car-data'), '');
    Car_Data_Meta_box_Data::text('_fuel_type', esc_html__('Fuel Type', 'car-data'), '');
    echo '</tbody>';
    echo '</table>';
    echo '</div>';

    echo '<div class="admin-car-data-metabox">';
    echo '<table cellpadding="0" cellspacing="0" border="0" >';
    echo '<tbody>';
    Car_Data_Meta_box_Data::text('_color', esc_html__('Color', 'car-data'), '');
    Car_Data_Meta_box_Data::text('_body_style', esc_html__('Body Style', 'car-data'), '');
    Car_Data_Meta_box_Data::text('_transmission', esc_html__('Transmission', 'car-data'), '');
    Car_Data_Meta_box_Data::text('_engine', esc_html__('Engine', 'car-data'), '');
    Car_Data_Meta_box_Data::text('_power_kw', esc_html__('Power KW', 'car-data'), '');
    Car_Data_Meta_box_Data::text('_power_hp', esc_html__('Power HP', 'car-data'), '');
    Car_Data_Meta_box_Data::text('_door', esc_html__('Door', 'car-data'), '');
    // Car_Data_Meta_Box_Data::upload('_car_upload', esc_html__('Upload Image', 'car-data'), '');
    echo '</tbody>';
    echo '</table>';
    echo '</div>';

    var_dump(Car_Data_Meta_Box_Data::save_car_data_meta(158));
}

/**
 * Job data meta box option fields.
 * 
 * @since 2.1.0
 * 
 * @param   string   $id     field id
 * @param   string   $label  field lable
 * @param   string   $desc   field description
 * @return  string   $html   field html
 */
public static function text($id, $label, $desc = '') {

    global $post;

    $html = '<tr class="metabox-field">';
    $html .= '<th><label for="car_data' . $id . '">' . $label . '</label></th>';
    $html .= '<td><input type="text" id="car_data' . $id . '" name="car_data' . $id . '" value="' . esc_attr( get_post_meta($post->ID, 'car_data' . $id, TRUE) ) . '" placeholder="' . esc_attr( get_post_meta($post->ID, 'car_data' . $id, TRUE) ) . '" />';
    if ($desc) {
        $html .= '<span class="tips">' . $desc . '</span>';
    }

    $html .= '</td></tr>';

    echo $html;
}

/**
 * Upload logo field
 * 
 * @since 2.1.0
 * 
 * @param   string   $id     Field id
 * @param   string   $label  Field lable
 * @param   string   $desc   Field description
 * @return  string   $html   Field html
 */
public static function upload($id, $label, $desc = '') {        
    global $post;
    ?>        
    <tr class="metabox-field">
        <th><label for="car_data<?php echo $id; ?>"><?php echo $label; ?></label> </th>
        <td><span class="file_url">
                <input type="text" name="car_data<?php echo $id; ?>" id="car_data<?php echo $id; ?>" class="upload_field" placeholder="URL to the car image" value="<?php echo esc_url( get_post_meta($post->ID, 'car_data' . $id, TRUE) ); ?>" />
                <button type="button" class="button car-data-upload-button"><?php esc_html_e('Upload', 'car-data'); ?></button>
            </span>
            <?php if ( $desc ) : ?>
                <p><?php echo $desc; ?></p>
            <?php endif; ?>
        </td>
    </tr>
    <?php
    print_r($post_id);
}

/**
 * Save job data meta box.
 * 
 * @since   2.2.3
 * 
 * @param   int     $post_id    Post id
 * @return  void
 */
public static function save_car_data_meta($post_id)
{   
    if( !isset( $_POST[ 'car_data_condition' ] ) ) {
        add_post_meta( $post_id, 'car_data_condition', $_POST[ 'car_data_condition' ], true );
    }
    if( !isset( $_POST[ 'car_data_make' ] ) ) {
        add_post_meta( $post_id, 'car_data_make', $_POST[ 'car_data_make' ], true );
    }
    if( !isset( $_POST[ 'car_data_model' ] ) ) {
        add_post_meta( $post_id, 'car_data_model', $_POST[ 'car_data_model' ], true );
    }
    if( !isset( $_POST[ 'car_data_registration_date' ] ) ) {
        add_post_meta( $post_id, 'car_data_registration_date', $_POST[ 'car_data_registration_date' ], true );
    }
    if( !isset( $_POST[ 'car_data_price' ] ) ) {
        add_post_meta( $post_id, 'car_data_price', $_POST[ 'car_data_price' ], true );
    }
    if( !isset( $_POST[ 'car_data_mileage' ] ) ) {
        add_post_meta( $post_id, 'car_data_mileage', $_POST[ 'car_data_mileage' ], true );
    }
    if( !isset( $_POST[ 'car_data_fuel_type' ] ) ) {
        add_post_meta( $post_id, 'car_data_fuel_type', $_POST[ 'car_data_fuel_type' ], true );
    }
    // Variables
    $post = get_post( $post_id ); // The post
    $revision = get_post( $revision_id ); // The revision
    $defaults = _namespace_metabox_defaults(); // The default values
    $details = array(); // An empty array for our new metadata values
    // Update content
    // For each field
    foreach ( $defaults as $key => $value ) {
        // Get the revision history version
        $detail_revision = get_metadata( 'post', $revision->ID, '_namespace_' . $key, true );
        // If a historic version exists, add it to our new data
        if ( isset( $detail_revision ) ) {
            $details[$key] = $detail_revision;
        }
    }
    // Replace our saved data with the old version
    update_post_meta( $post_id, 'car_data_', $details );








    // add_post_meta( $post_id, 'car_data_mileage', $meta_value , true);
    // add_post_meta( $post_id, 'car_data_fuel_type', $meta_value , true); 

    // $bed_room = sanitize_text_field( $_POST['car_data_mileage'] );
    // add_post_meta( $post_id, 'car_data_mileage', $bed_room );

    // $post_meta = get_post_custom( $post_id );
    // var_dump($post_meta);

    // $meta_key = wp_verify_nonce($_REQUEST['car_data_meta_box_nonce']);

    // $meta = get_post_meta( $post_id);
    // $name = filter_var($_POST['car_data_mileage'], FILTER_SANITIZE_STRING);

    // $POST_data = filter_input_array( INPUT_POST );

    // foreach ( $POST_data as $key => $value )
    // {
    //     if (strstr($key, 'car_data')) {
    //         update_post_meta( $post_id, sanitize_key( $key ), $value );
    //     }
    // }
    // // Add new Value
    // foreach ( $meta as $key => $val ):

    //     if ( substr($key, 0, 9 ) == 'car_data_' ) {

    //         $key = preg_replace('/[^\p{L} 0-9]/u', '_', $key);             

    //         // echo $key . ' : ' . $val[0] . '<br/>';

    //         add_post_meta( $post_id, $key, $val[0] );

    //         /* If a new meta value was added and there was no previous value, add it. */
    //         // if ( $meta_value && '' == $val )
    //         //     add_post_meta( $post_id, 'car_data_', $meta_value );
    //         // /* If the new meta value does not match the old value, update it. */
    //         // elseif ( $meta_value && $meta_value != $val )
    //         //     update_post_meta( $post_id, 'car_data_', $meta_value );
    //         // /* If there is no new meta value but an old value exists, delete it. */
    //         // elseif ( '' == $meta_value && $val )
    //         //     delete_post_meta( $post_id, 'car_data_', $meta_value );
    //     }
    // endforeach;
}
}

另一个文件是admin / class-car-data-admin-meta-boxes-init.php

class Car_Data_Meta_Boxes_Init {

/**
 * Initialize the class and set its properties.
 *
 * @since   1.0.0
 */
public function __construct() {
     /**
     * The class responsible for defining job data meta box options under custom post type in the admin area.
     */
    require_once plugin_dir_path( dirname(__FILE__) ) . 'admin/meta-boxes/class-car-data-meta-box-data.php';

    // Action -> Load WP Media Uploader Scripts.
    // add_action( 'admin_enqueue_scripts', array( $this, 'admin_script_loader' ) );

    // Action -> Post Type -> Car Data -> Add Meta Boxes. 
    add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );


    // Action -> Post Type -> Car Data -> Save Meta Boxes.
    add_action( 'save_car_data_meta', array( $this, 'save_meta_boxes' ), 10, 1 );

    // Action -> Post Type -> Car Data -> Save Job Data Meta Box.
    add_action( 'save_car_data_meta', array( 'Car_Data_Meta_Box_Data', 'save_car_data_meta' ), 30);

} 

/**
 * Load backend scripts
 * 
 * @since   1.0.0
 */
// function admin_script_loader() {

//     global $pagenow;

//     if (is_admin() && ( in_array($pagenow, array('post-new.php', 'post.php'))) ) {
//         wp_enqueue_script('media-upload');
//         wp_enqueue_script('thickbox');
//         wp_enqueue_style('thickbox');            
//     }
// }

/**
 * Add car_data meta boxes.
 *
 * @since 1.0.0
 */
public function add_meta_boxes() {
    global $wp_post_types;
    add_meta_box('car-data-post_options', esc_html__('Car Detail', 'car-data'), array('Car_Data_Meta_box_Data', 'car_data_meta_box_output'), 'car-details', 'normal');  
}

/**
 * Save Meta Boxes.
 *
 * @since 1.0.0
 */
public function save_meta_boxes( $post_id  ) {

    /**
     * We need to verify this came from our screen and with proper authorization,
     * because the save_post action can be triggered at other times.
     */

    // Check if nonce is set.
    if ( NULL == filter_input( INPUT_POST, 'car_data_meta_box_nonce' ) ) { 
        return;
    }

    // Verify that the nonce is valid.
    check_admin_referer( 'car_data_meta_box_action', 'car_data_meta_box_nonce' );

    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }

    // Check the user's permissions.
    if ( NULL != filter_input( INPUT_POST, 'car-details' ) && 'page' == filter_input( INPUT_POST, 'car-details') ) {
        if (!current_user_can('edit_page', $post_id)) {
            return;
        }
    } else {
        if (!current_user_can('edit_post', $post_id)) {
            return;
        }
    }

    /**
     * @hooked save_car_data_meta - 10
     * @hooked save_car_data_meta - 20
     * @hooked save_car_data_meta - 30
     * 
     * Save Car Data Meta Box:
     * 
     * - Save job features meta box.
     * - Save job application meta box.
     * - Save job data meta box. 
     * 
     * @since   1.0.0
     * 
     * @param   int    $post_id    Post Id
     */ 
    do_action( 'save_car_data_meta', $post_id );

}

}

new Car_Data_Meta_Boxes_Init();

0 个答案:

没有答案