• Hi,

    I want to add a checkbox to the image editor to give the option for setting the image as featured and then display it with a widget / shortcode. Everything went cool until I tried to uncheck the checkbox and it didn’t update the option. After an hour and half I think I understood the problem and it might be because the options in the image editor are updated by ajax and when I uncheck the checkbox it doesn’t make the call, I say this because the same script with input type built in works perfectly.

    THE CODE I USE:

    add_filter( 'attachment_fields_to_edit', 'edit_function', 10, 2 );
    function edit_function ( $form_fields, $post ) {
        $form_fields[ 'my_option' ] = array(
            'label' => 'Featured',
            'input' => 'html',
            'value' => get_post_meta( $post->ID, '_my_option', true ),
            'helps' => '',
            'html' => "<input type='text' value='1' name='attachments[{$post->ID}][my_option]' id='attachments[{$post->ID}][my_option]'" . checked( 1, $value, true ) . ">"
        );
    
        return $form_fields;
    }
    
    add_filter( 'attachment_fields_to_save', 'save_function', 10, 2 );
    function save_function ( $post, $attachment ) {
        if ( isset( $attachment[ 'my_option' ] ) ) {
            update_post_meta( $post['ID'], '_my_option', $attachment[ 'my_option' ] );
        }
    
        return $post;
    }

    If what I supposed turns out to be true, can anyone help me fix it, I have no idea about ajax in wordpress.

    Thanks in advance.

The topic ‘Custom Field into Image Editor’ is closed to new replies.