kode – Apa yang salah di WordPress Meta Box Generator menyerah?

Saya sangat baru dalam pengembangan WordPress. Ketika saya mengirimkan plugin saya untuk ditinjau, itu ditolak karena “## Nama fungsi/kelas/define/namespace generik”. Saya tidak dapat memahami cara memperbaikinya. Tinjau tanda tim pada garis sede itu update_post_meta( $post_id, $meta_field['id'], $_POST[ $meta_field['id'] ] );

Saya telah menghasilkan kode menggunakan https://wp-skills.com/tools/meta-box-generator/Og7AvMWAZGqFjcrQy3Q6

Kode lengkap adalah

class MetaBoxesForVideoTestimonialSliderMetaBox{

private $screen = array(
    'post',
                    
);

private $meta_fields = array(
            array(
                'label' => 'Designation',
                'id' => 'designation',
                'default' => 'Adi mom, Grade 8, USA',
                'type' => 'text',
            ),

            array(
                'label' => 'Country',
                'id' => 'country',
                'default' => 'India',
                'type' => 'select',
                'options' => array(
                    'India',
                    'USA',
                    'United Kingdom'
                )
            ),

            array(
                'label' => 'Star Rating',
                'id' => 'star_rating',
                'type' => 'select',
                'options' => array(
                    '1',
                    '2',
                    '3',
                    '4',
                    '5'
                )
            ),

            array(
                'label' => 'Video URL',
                'id' => 'video_url',
                'type' => 'url',
            )

);

public function __construct() {
    add_action( 'add_meta_boxes', array( $this, 'add_meta_boxes' ) );
    add_action( 'save_post', array( $this, 'save_fields' ) );
}

public function add_meta_boxes() {
    foreach ( $this->screen as $single_screen ) {
        add_meta_box(
            'MetaBoxesForVideoTestimonialSlider',
            __( 'MetaBoxesForVideoTestimonialSlider', 'textdomain' ),
            array( $this, 'meta_box_callback' ),
            $single_screen,
            'normal',
            'default'
        );
    }
}

public function meta_box_callback( $post ) {
    wp_nonce_field( 'MetaBoxesForVideoTestimonialSlider_data', 'MetaBoxesForVideoTestimonialSlider_nonce' );
    $this->field_generator( $post );
}
public function field_generator( $post ) {
    $output="";
    foreach ( $this->meta_fields as $meta_field ) {
        $label="<label for="" . $meta_field['id'] . '">' . $meta_field['label'] . '</label>';
        $meta_value = get_post_meta( $post->ID, $meta_field['id'], true );
        if ( empty( $meta_value ) ) {
            if ( isset( $meta_field['default'] ) ) {
                $meta_value = $meta_field['default'];
            }
        }
        switch ( $meta_field['type'] ) {
                            case 'select':
                                $input = sprintf(
                                    '<select id="%s" name="%s">',
                                    $meta_field['id'],
                                    $meta_field['id']
                                );
                                foreach ( $meta_field['options'] as $key => $value ) {
                                    $meta_field_value = !is_numeric( $key ) ? $key : $value;
                                    $input .= sprintf(
                                        '<option %s value="%s">%s</option>',
                                        $meta_value === $meta_field_value ? 'selected' : '',
                                        $meta_field_value,
                                        $value
                                    );
                                }
                                $input .= '</select>';
                                break;

            default:
                                $input = sprintf(
                                    '<input %s id="%s" name="%s" type="%s" value="%s">',
                                    $meta_field['type'] !== 'color' ? 'style="width: 100%"' : '',
                                    $meta_field['id'],
                                    $meta_field['id'],
                                    $meta_field['type'],
                                    $meta_value
                                );
        }
        $output .= $this->format_rows( $label, $input );
    }
    echo '<table class="form-table"><tbody>' . $output . '</tbody></table>';
}

public function format_rows( $label, $input ) {
    return '<tr><th>'.$label.'</th><td>'.$input.'</td></tr>';
}

public function save_fields( $post_id ) {
    if ( ! isset( $_POST['MetaBoxesForVideoTestimonialSlider_nonce'] ) )
        return $post_id;
    $nonce = $_POST['MetaBoxesForVideoTestimonialSlider_nonce'];
    if ( !wp_verify_nonce( $nonce, 'MetaBoxesForVideoTestimonialSlider_data' ) )
        return $post_id;
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return $post_id;
    foreach ( $this->meta_fields as $meta_field ) {
        if ( isset( $_POST[ $meta_field['id'] ] ) ) {
            switch ( $meta_field['type'] ) {
                case 'email':
                    $_POST[ $meta_field['id'] ] = sanitize_email( $_POST[ $meta_field['id'] ] );
                    break;
                case 'text':
                    $_POST[ $meta_field['id'] ] = sanitize_text_field( $_POST[ $meta_field['id'] ] );
                    break;
            }
            update_post_meta( $post_id, $meta_field['id'], $_POST[ $meta_field['id'] ] );
        } else if ( $meta_field['type'] === 'checkbox' ) {
            update_post_meta( $post_id, $meta_field['id'], '0' );
        }
    }
}

}

if (class_exists(‘MetaBoxesForVideoTestimonialSliderMetabox’)) { new MetaBoxesForVideoTestimonialSliderMetabox; };

Leave a Reply

Your email address will not be published. Required fields are marked *