Làm thế nào để thêm một yếu tố hộp kiểm vào trình soạn thảo tệp đính kèm


7

Mã dưới đây thêm trường nhập tùy chỉnh vào trình chỉnh sửa tệp đính kèm. Làm cách nào tôi có thể chuyển đổi văn bản nhập thành hộp kiểm và nhận / đặt giá trị của hộp kiểm khi tải và lưu?

Lưu ý: "input" => "checkbox"không không làm việc :(

function image_attachment_fields_to_edit($form_fields, $post) {  
        $form_fields["imageLinksTo"] = array(  
            "label" => __("Image Links To"),  
            "input" => "text",
            "value" => get_post_meta($post->ID, "_imageLinksTo", true)  
        );    
        return $form_fields;  
    }  

function image_attachment_fields_to_save($post, $attachment) {  
        if( isset($attachment['imageLinksTo']) ){  
            update_post_meta($post['ID'], '_imageLinksTo', $attachment['imageLinksTo']);  
        }  
        return $post;  
    } 

add_filter("attachment_fields_to_edit", "image_attachment_fields_to_edit", null, 2); 
add_filter("attachment_fields_to_save", "image_attachment_fields_to_save", null, 2); 

Câu trả lời:


9

Đặt 'đầu vào' thành 'html' và viết ra html cho đầu vào:

function filter_attachment_fields_to_edit( $form_fields, $post ) {
    $foo = (bool) get_post_meta($post->ID, 'foo', true);

    $form_fields['foo'] = array(
    'label' => 'Is Foo',
    'input' => 'html',
    'html' => '<label for="attachments-'.$post->ID.'-foo"> '.
        '<input type="checkbox" id="attachments-'.$post->ID.'-foo" name="attachments['.$post->ID.'][foo]" value="1"'.($foo ? ' checked="checked"' : '').' /> Yes</label>  ',
    'value' => $foo,
    'helps' => 'Check for yes'
    );
    return $form_fields;
}

Việc lưu lại hoạt động giống như bạn đã làm ở trên, nhưng thay vào đó, bạn đang kiểm tra giá trị hộp kiểm, vì vậy bạn sẽ cần cập nhật thành true nếu isset () và cập nhật thành false nếu không.


Bạn có thể vui lòng thêm một ví dụ hoạt động của mã lưu thông tin này không? Sẽ thực sự tuyệt vời! :)
Ole Henrik Skogstrøm

5

Dưới đây là một khối hoàn chỉnh để thêm hộp kiểm IsLogo, bao gồm cả lưu:

function attachment_fields_to_edit_islogoimage( $form_fields, $post ) {
    $islogo = (bool) get_post_meta($post->ID, '_islogo', true);
    $checked = ($islogo) ? 'checked' : '';

    $form_fields['islogo'] = array(
        'label' => 'Logo Image ?',
        'input' => 'html',
        'html' => "<input type='checkbox' {$checked} name='attachments[{$post->ID}][islogo]' id='attachments[{$post->ID}][islogo]' />",
        'value' => $islogo,
        'helps' => 'Should this image appear as a proposal Logo ?'
    );
    return $form_fields;

}
add_filter( 'attachment_fields_to_edit', 'attachment_fields_to_edit_islogoimage', null, 2 );

function attachment_fields_to_save_islogoimage($post, $attachment) {
    $islogo = ($attachment['islogo'] == 'on') ? '1' : '0';
    update_post_meta($post['ID'], '_islogo', $islogo);  
    return $post;  
}
add_filter( 'attachment_fields_to_save', 'attachment_fields_to_save_islogoimage', null, 2 );

2 xu của tôi.

Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.