Support » Plugins » Hacks » Insert Media struggle with ‘attachment_fields_to_edit’ filter

  • Resolved ernestortiz

    (@ernestortiz)


    To remove some fields in media upload area when using thickbox (something like tb_show('', 'media-upload.php?post_id=0&type=image&TB_iframe=true&referer=alter', false);

    this filter:

    add_filter('attachment_fields_to_edit', 'profilextra_style_umedia', 10000, 2);
    function profilextra_style_umedia($form_fields,$post) {
        unset( $form_fields['align'] );
        unset( $form_fields['post_title'] );
        unset( $form_fields['image_alt'] );
        unset( $form_fields['post_excerpt'] );
        unset( $form_fields['post_content'] );
        unset( $form_fields['url'] );
        return $form_fields;
    }

    do it well. The problem is that when this code is present Media Library stops (do not show images at upload.php) and the same for Insert Media on posts and pages editor (when clicked on “Add Media” button).

    What could be wrong here?
    Thanks in advance for your answers.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    I couldn’t say exactly why, but it’s because you are unsetting data the media library needs to work properly. PHP fails when code tries to use data that does not exist. Your question would be better phrased with how do you stop this from happening while maintaining the thickbox functionality you want?

    Could you simply hide undesired fields with CSS? Sometimes empty fields are better than no fields as far as PHP is concerned.

    Thread Starter ernestortiz

    (@ernestortiz)

    Hello bcworks. Thanks for your answer.

    Your assumption is completely logic, and hidden fields with CSS is a solution. The problem is that I only want to hide those fields on my custom thickbox (which open when click on a link, inside a post metabox), not in Media Library nor in the regular insertion of image into post.

    Finally I found a solution that works as expected (and my plugin is ready); although I do not completely realise yet why it works fine and the other one doesn’t. I’ll think on that later…

    Well, I just append the filter to the admin_init action, this way:

    function profilextra_thickbox() {
        global $pagenow;
        if ( 'media-upload.php'==$pagenow || 'async-upload.php'==$pagenow )
            add_filter('attachment_fields_to_edit', 'profilextra_style_umedia', 10000, 2);
    }
    add_action( 'admin_init', 'profilextra_thickbox' );

    (Wrote it here in case anyone else confront a similar problem)

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Insert Media struggle with ‘attachment_fields_to_edit’ filter’ is closed to new replies.