WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] How to move Featured Image box from side to main column (21 posts)

  1. Josh Leuze
    Member
    Posted 4 years ago #

    I am working on a plugin for WordPress 3.0 that creates a custom post type. The custom post type includes the Featured Image functionality.

    I would like to move the Featured Image box from the "side" position to the "normal" or "advanced" position.

    Pretty simple to pull off with the add meta box function using the "context" parameter, but I can't seem to find a similar trick for existing boxes.

    Any ideas?

  2. Josh Leuze
    Member
    Posted 4 years ago #

    Been digging deeper into WordPress to see how this Featured Image box is generated.

    In the wp-admin/includes/meta-boxes.php file I found this code:

    function post_thumbnail_meta_box() {
    		global $post;
    		$thumbnail_id = get_post_meta( $post->ID, '_thumbnail_id', true );
    		echo _wp_post_thumbnail_html( $thumbnail_id );
    	}

    Can't think of anything clever to do with that.

    But in the wp-admin/edit-form-advanced.php file I found this code:

    if ( current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports($post_type, 'thumbnail') )
    		add_meta_box('postimagediv', __('Featured Image'), 'post_thumbnail_meta_box', $post_type, 'side', 'low');

    Seems I could easily edit that bit like this to get it to work like I want:

    if ( current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports($post_type, 'thumbnail') )
    		add_meta_box('postimagediv', __('My Own Fancy Title'), 'post_thumbnail_meta_box', $post_type, 'normal', 'high');

    But of course I'm not about to edit a core file, and besides, I only want that version to load on my custom post type admin page.

    I've got a hunch I want to conditionally filter that bit of original code only on the custom post type admin page and replace it with my own version. But I can't figure out which filter would allow me to accomplish this.

    Any of you smart cookies wanna toss me a crumb? ;)

  3. Josh Leuze
    Member
    Posted 4 years ago #

    Just as a test I tried to add the featured image meta box where I wanted without filtering the other one:

    add_action('admin_init', 'customposttype_image_box');
    
    function customposttype_image_box() {
    
    	add_meta_box('custompostimagediv', __('Custom Image'), 'post_thumbnail_meta_box', 'customposttype', 'normal', 'high');
    
    }

    That worked great, added exactly the meta box I wanted, right where I wanted it. However, I didn't touch the default featured image box, so that one is still sitting there on the page.

    I figured, if add_meta_box added the box I want, remove_meta_box should get rid of the other one right?

    Wrong. I tried this and got nothing:

    add_action('admin_init', 'customposttype_image_box');
    
    function customposttype_image_box() {
    
    	remove_meta_box( 'postimagediv', 'customposttype', 'side' );
    
    }

    Now, if you look at the documentation for the remove_meta_box function, it appears that it only works for 'post', 'page', or 'link' pages, and only in the 'normal', or 'advanced' context.

    I tried it in those circumstances, and it does work fine. But I also tried it on a custom post type and the 'side' context with no problem.

    But I couldn't get it to remove a featured image meta box on any page, post, or custom post type admin page.

    It's not just a matter of the context, I could remove other meta boxes from the side, like the Publish box:

    add_action('admin_init', 'customposttype_image_box');
    
    function customposttype_image_box() {
    
    	remove_meta_box( 'submitdiv', 'customposttype', 'side' );
    
    }

    But for some reason, I cannot remove the Featured Image meta box, it almost seems like a bug. It's too bad, if I could remove that meta box it would totally solve my problem!

  4. Josh Leuze
    Member
    Posted 4 years ago #

    Ha, I finally figured it out! So I suppose I should answer my own question to help the next poor bastard who has this problem.

    I was on the right track with removing the Featured Image meta box and adding a new version, but there was one small problem. I was running these functions on the admin_init action, which seemed to work fine with most meta boxes, but to remove the post thumbnail meta box I had to switch to the do_meta_boxes action.

    Here is the code that worked for me to move the Featured Image box just on my custom post type:

    add_action('do_meta_boxes', 'customposttype_image_box');
    
    function customposttype_image_box() {
    
    	remove_meta_box( 'postimagediv', 'customposttype', 'side' );
    
    	add_meta_box('postimagediv', __('Custom Image'), 'post_thumbnail_meta_box', 'customposttype', 'normal', 'high');
    
    }

    Just a few more bugs to work out and my first plugin should be ready to submit to the plugin directory!

  5. Nagmay
    Member
    Posted 4 years ago #

    @jleuze,
    Thank you so much for posting your solution. I had been trying everything to get rid of the "Featured Image" meta box.

    /* === Remove Featured Image Meta === */
    add_action('do_meta_boxes', 'remove_image_box');
    
    function remove_image_box() {
    	if ($current_user->user_level < 10){
    		remove_meta_box( 'postimagediv','post','side' );
    	}
    }
  6. Josh Leuze
    Member
    Posted 4 years ago #

    No problem Gabriel, glad it was of use to you.

  7. Richard-B
    Member
    Posted 4 years ago #

    This is fantastic, thank you for documenting your progress with this, its a huge help especially during theme development.

    The only thing I need to solve is how to control the size of the image in the featured image in the add / edit post, at the moment mine is coming out at full size which looks really messy, is there a way of defining the image size for the admin area?

  8. Josh Leuze
    Member
    Posted 4 years ago #

    You're welcome Richard, glad it helped.

    Did you set the size of your featured images for the frontend? So they show up fine on your site, but are not resized as thumbnails in the admin, is that correct?

    I don't think there is a way to set the size of those thumbnails, in my experience they seem to be the same height but the width varies depending on the ratio of your images. Never seen them get really large though.

    My best guess is that you've got a typo or something that is throwing it off. Here is a really good tutorial on featured images, you could compare Mark's code to your own.

  9. spacechampion
    Member
    Posted 3 years ago #

    Nice! Exactly what I was looking for.

    On the off chance you know how to do this.... I have a custom post type of 'Video', which needs posters/thumbnails attached, as well as videos. Worse, I want to allow alternate video files, so I need a metabox to attach those from the media uploader.

    Thanks regardless.

  10. Josh Leuze
    Member
    Posted 3 years ago #

    Glad it helped. I have a custom meta box in my plugin, and this tutorial was a big help in getting it to work.

    You'll have to do some extra research to add an uploader to a metabox though.

  11. amuralikumar
    Member
    Posted 3 years ago #

    @JLeuze
    Thanks!!!
    It saved my life..
    Is there a way to change "set featured image" text, which shows inside the box... ;)

  12. Josh Leuze
    Member
    Posted 3 years ago #

    No problem, you should be able to change that text with a filter but I have not looked into actually changing it myself.

  13. Andy Bailey
    Member
    Posted 3 years ago #

    beautiful!

    thanks, I was just scratching my head over removing the featured image box for the past 10 minutes.

    thanks for posting all the geeky details!

  14. Josh Leuze
    Member
    Posted 3 years ago #

    You're welcome Andy, glad those geeky details helped you out!

  15. Doug
    Member
    Posted 3 years ago #

    Thank you, exactly what I was looking for.

  16. Josh Leuze
    Member
    Posted 3 years ago #

    No problem Doug!

  17. michael
    Member
    Posted 3 years ago #

    thank you JLeuze. removing that postimagediv meta box almost drove me nuts

  18. mirko.menegazzo
    Member
    Posted 3 years ago #

    Thanks, very very useful!

  19. Josh Leuze
    Member
    Posted 3 years ago #

    You're welcome mirko!

  20. mike62
    Member
    Posted 3 years ago #

    I'm trying to remove this box in my template code. I'm making a child theme of twentyten, and I'm using the much more flexible Multiple Template Images plugin for post images rather than the Featured Image box. Is the code you're using able to be put into my child theme's functions.php file somehow? I tried putting it in as-is and it didn't remove the box so I'm assuming I'm doing something wrong.

    I want to modify my child theme in some way to remove the Featured Image functionality altogether. Do you know if that's possible?

    Thanks!

  21. Josh Leuze
    Member
    Posted 3 years ago #

    Hi Mike, when I have needed more than one post thumbnail, the Multiple Post Thumbnails plugin has come in handy.

    Twenty Ten is setup so that you can override settings in the functions.php file. Take a look at this comment:

    To override twentyten_setup() in a child theme, add your own twentyten_setup to your child theme's
    * functions.php file.

    So you could copy the "twentyten_setup" function into your child theme's functions.php file and comment this out:

    add_theme_support( 'post-thumbnails' );

Topic Closed

This topic has been closed to new replies.

About this Topic