• Resolved Josh Leuze

    (@jleuze)


    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?

Viewing 15 replies - 1 through 15 (of 20 total)
  • Thread Starter Josh Leuze

    (@jleuze)

    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? 😉

    Thread Starter Josh Leuze

    (@jleuze)

    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!

    Thread Starter Josh Leuze

    (@jleuze)

    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!

    Nagmay

    (@gabrielmcgovern)

    @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' );
    	}
    }
    Thread Starter Josh Leuze

    (@jleuze)

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

    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?

    Thread Starter Josh Leuze

    (@jleuze)

    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.

    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.

    Thread Starter Josh Leuze

    (@jleuze)

    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.

    @jleuze
    Thanks!!!
    It saved my life..
    Is there a way to change “set featured image” text, which shows inside the box… 😉

    Thread Starter Josh Leuze

    (@jleuze)

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

    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!

    Thread Starter Josh Leuze

    (@jleuze)

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

    Thank you, exactly what I was looking for.

    Thread Starter Josh Leuze

    (@jleuze)

    No problem Doug!

Viewing 15 replies - 1 through 15 (of 20 total)
  • The topic ‘How to move Featured Image box from side to main column’ is closed to new replies.