4

I am trying to add a checkbox control in the image details popup (when editing image in a post). I have added the following code in my template function.php but I can't manage to display the checkbox in the popup. What am I missing?

function bluesolutions_customize_register($wp_customize) {
  $wp_customize->add_section('bluesolutions_use_lighbox_section', array(
      'title' => __('Lightbox', 'mytheme'),
      'description' => '',
      'priority' => 120,
  ));

  $wp_customize->add_setting('mytheme_use_lighbox_setting', array(
      'default' => 0,
      'capability' => '',
      'type' => 'checkbox',
  ));

  $wp_customize->add_control(new WP_Customize_Image_Control(
          $wp_customize, 'use_lighbox', array(
      'label' => __('Usen lightbox', 'mytheme'),
      'section' => 'advanced',
      'settings' => 'mytheme_use_lighbox_setting',
      'priority' => 1,
      'type' => 'checkbox',
          )
  ));
}

add_action( 'customize_register', 'bluesolutions_customize_register' );

enter image description here

  • The hook you are using (and related code) customize_register is meant for the theme customization page... not the editor "edit image" window. I'm not sure which hook to use for the "Image Details" window. Perhaps someone else can chime in here? – josh Feb 11 '15 at 15:24
  • OK thanks, I will try to find te right hook to use. – Mika A. Feb 12 '15 at 8:15
  • Did you ever find a solution? I'm looking for the same answer. – Terri Ann Apr 8 '15 at 18:35
  • I'm curious what the hook is for this as well – Max Hartshorn Dec 2 '16 at 22:24
0

You can use this code

function your_slug_edit_media_custom_checkbox( $form_fields, $post ) {
    $form_fields['custom_field'] = array(
        'label' => 'Custom Field',
        'input' => 'text',
        'value' => get_post_meta( $post->ID, '_custom_field', true )
    );
    return $form_fields;
}
function your_slug_save_media_custom_field( $post, $attachment ) {
    update_post_meta( $post['ID'], '_custom_field', $attachment['custom_field'] );
    return $post;
}
add_filter( 'attachment_fields_to_edit', 'edit_media_custom_checkbox', 11, 2 );
add_filter( 'attachment_fields_to_save', 'save_media_custom_checkbox', 11, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.