在精选的照片元框中添加多个单选按钮

时间:2019-01-14 06:17:48

标签: wordpress forms custom-fields post-meta

我想向编辑器提供选项,以选择如何裁剪特色图像。我在特征图像元框中添加了三个单选按钮(左,中,右裁剪)。它显示正确,但更新后无法正常工作。它不保存数据...

// adding thumbnail cropping choices as a meta key into feature image box
    function add_featured_image_display_settings( $content, $post_id ) {
        $field_id_left = 'left_cropped_featured_image';
        $field_value_left = esc_attr( get_post_meta( $post_id, $field_id_left, true ) );
        $field_text_left  = esc_html__( 'Left', 'generatewp' );
        $field_state_left = checked( $field_value_left, 1, false);

        $field_id_center = 'center_cropped_featured_image';
        $field_value_center = esc_attr( get_post_meta( $post_id, $field_id_center, true ) );
        $field_text_center  = esc_html__( 'Center', 'generatewp' );
        $field_state_center = checked( $field_value_center, 1, false);

        $field_id_right = 'right_cropped_featured_image';
        $field_value_right = esc_attr( get_post_meta( $post_id, $field_id_right, true ) );
        $field_text_right  = esc_html__( 'Right', 'generatewp' );
        $field_state_right = checked( $field_value_right, 1, false);

        $field_label = sprintf(
            '<p><b>Cropping</b></p><p><input type="radio" name="featured_image_cropping" id="%1$s" value="%2$s" %4$s><label for="%1$s"> %3$s </label></p><p><input type="radio" name="featured_image_cropping" id="%5$s" value="%6$s" %8$s><label for="%5$s"> %7$s </label></p><p><input type="radio" name="featured_image_cropping" id="%9$s" value="%10$s" %12$s><label for="%9$s"> %11$s </label></p>', $field_id_left, $field_value_left, $field_text_left, $field_state_left, $field_id_center, $field_value_center, $field_text_center, $field_state_center, $field_id_right, $field_value_right, $field_text_right, $field_state_right
        );

        return $content .= $field_label;
    }
    add_filter( 'admin_post_thumbnail_html', 'add_featured_image_display_settings', 10, 2 );

    function save_featured_image_display_settings( $post_ID, $post, $update ) {
        $field_id_left    = 'left_cropped_featured_image';
        $field_value_left = isset( $_REQUEST[ $field_id_left ] ) ? 1 : 0;
        update_post_meta( $post_ID, $field_id_left, $field_value_left );

        $field_id_center    = 'center_cropped_featured_image';
        $field_value_center = isset( $_REQUEST[ $field_id_center ] ) ? 1 : 0;
        update_post_meta( $post_ID, $field_id_center, $field_value_center );

        $field_id_right    = 'right_cropped_featured_image';
        $field_value_right = isset( $_REQUEST[ $field_id_right ] ) ? 1 : 0;
        update_post_meta( $post_ID, $field_id_right, $field_value_right );

    }
    add_action( 'save_post', 'save_featured_image_display_settings', 10, 3 );

1 个答案:

答案 0 :(得分:0)

    function add_featured_image_display_settings( $content, $post_id ) {
        $field_id    = 'featured_image_cropping';
        $field_value = esc_attr( get_post_meta( $post_id, $field_id, true ) );

        $field_attribute_left  = esc_html__( 'left', 'generatewp' );
        $field_text_left  = esc_html__( 'Left', 'generatewp' );
        $field_state_left = checked( $field_value, 'left', true);

        $field_attribute_center  = esc_html__( 'center', 'generatewp' );
        $field_text_center  = esc_html__( 'Center', 'generatewp' );
        $field_state_center = checked( $field_value, 'center', true);

        $field_attribute_right  = esc_html__( 'right', 'generatewp' );
        $field_text_right  = esc_html__( 'Right', 'generatewp' );
        $field_state_right = checked( $field_value, 'right', true);

        $field_label = sprintf(
            '<p><b>Cropping</b></p><p><input type="radio" name="featured_image_cropping" id="%1$s" value="%1$s" %3$s><label for="%1$s"> %2$s </label></p><p><input type="radio" name="featured_image_cropping" id="%4$s" value="%4$s" %6$s><label for="%4$s"> %5$s </label></p><p><input type="radio" name="featured_image_cropping" id="%7$s" value="%7$s" %9$s><label for="%7$s"> %8$s </label></p>', $field_attribute_left, $field_text_left, $field_state_left, $field_attribute_center, $field_text_center, $field_state_center, $field_attribute_right, $field_text_right, $field_state_right
        );

        return $content .= $field_label;
    }
    add_filter( 'admin_post_thumbnail_html', 'add_featured_image_display_settings', 10, 2 );

    function save_featured_image_display_settings( $post_ID, $post, $update ) {
        $field_id    = 'featured_image_cropping';
        switch($_REQUEST[ $field_id ]){
                case 'left':
                $field_value = 'left';
                break;
                case 'center':
                $field_value = 'center';
                break;
                case 'right':
                $field_value = 'right';
                break;
        }
        update_post_meta( $post_ID, $field_id, $field_value );

    }

我修改了代码,它可以正常工作。我只是害怕这种做法不够好。如果有人对此发表意见,请发表评论。非常感谢你!