• Resolved Alkorr

    (@alkorr)


    Hi, I searched for a long time how to add a second meta box for another featured image and I found a snippet to do it a while ago. It worked fine for a long time but since the last WP update, it doesn’t work anymore…

    I didn’t modify anything, the code I use in my functions.php is still the same but the box isn’t there anymore, I can’t add a new image (but the default meta box is still there). I can’t switch to a plugin so I hope someone in this forum will be nice enough to help me. That would be really great because I don’t know much of PHP (or JS) and I can’t find what the problem is…

    Here’s the code:

    add_action( 'add_meta_boxes', 'custom_featured_mb_add' );
    function custom_featured_mb_add(){
      add_meta_box( 'custom-featured-image', 'Post Image', 'custom_featured_image_mb', 'post', 'side', 'low' );
    }
    
    function custom_featured_image_mb($post){
      $image_meta = 'custom_featured_image';
      $image_id = get_post_meta( $post->ID, $image_meta, true );
      $image_attributes = wp_get_attachment_image_src( $image_id, array(596,170));
     
      echo '<div class="custom_uploader">';
        echo '<img class="custom_media_image" src="'. $image_attributes[0] .'" width="254" style="'. ( ! $image_id ? 'display:none;' : '' ) .' margin: 10px 0 15px 0;" />';
        echo '<a href="#" class="custom_media_add" style="'. (! $image_id ? '' : 'display:none;') .'">Set New Featured Image</a>';
        echo '<br><a href="#" class="custom_media_remove" style="'. ( ! $image_id ? 'display:none;' : '' ) .'">Remove post image</a>';
        echo '<input class="custom_media_id" type="hidden" name="'. $image_meta .'" value="'. $image_id .'">';
      echo '</div>';
    }
    
    add_action( 'save_post', 'custom_featured_image_mb_save' );
    
    function custom_featured_image_mb_save( $post_id ){ 
      //verify the metadata is set
      if ( isset ($_POST['custom_featured_image'])) {
        //save the metadata
        update_post_meta( $post_id, 'custom_featured_image', $_POST['custom_featured_image']);
      }
    }
    
    function featured_image_handler_enqueue() {
      global $typenow;
      if( $typenow == 'post' ) {
        wp_enqueue_script( 'featured-image-handler-js', get_template_directory_uri() . '/js/featured-image-handler.js', array(), '', false );
      }
    }
    add_action( 'admin_enqueue_scripts', 'featured_image_handler_enqueue' );

    Thanks a lot for your help! ??

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

    (@bcworkz)

    On the post edit screen, pull down the screen options tab at the upper right and be sure “Post Image” is checked.

    Your meta box code displays a meta box on my site running 4.9.5. I only made minor data changes so it works with my data. The box didn’t actually work because I don’t have the JavaScript, but the box at least displays without any issue.

    Thread Starter Alkorr

    (@alkorr)

    Hi bcworkz, thanks for your reply. I checked the screen options and the Featured Image (default box) is checked. There is no Post Image on my screen options excepted the Featured Image… I’m puzzled! Is there a reason why you see it on your screen options and I don’t? <:)

    Moderator bcworkz

    (@bcworkz)

    I imagine something is conflicting with your add meta box code, as it should result in “Post Image” being one of the screen options. In fact, normally it should be checked by default, but sometimes it fails to be checked even with 100% correct code. In any case, the option should always be there.

    Try using the troubleshooting mode of the health-check plugin. Also ensure there are no problems flagged under the health check tab. To use the troubleshooting mode, temporarily copy your meta box code to the functions.php of twentyseventeen theme. Health-check will deactivate all plugins and switch to twentyseventeen. In this mode, you should see your meta box on the post edit screen. Use the troubleshooting item in the admin bar to first switch back to your normal theme, the reactivate plugins one by one, testing after each change. When the meta box disappears again, the last activated module is causing a conflict.

    Thread Starter Alkorr

    (@alkorr)

    Hi bcworkz, I followed your advice (at least part of it) and I deactivated all the plugins. Unfortunately, the second image box still doesn’t appear in the Post Admin. This is weird because you say it does on your side…

    The images I set in older posts using this second image box when it used to show are still showing on the site but when I edit the post, I can’t even change the image since the second image box doesn’t appear anymore.

    So the code is indeed working, but there is something preventing the second image box to show in the Post Admin and I have no idea what could do that. Not a plugin (I deactivated them all) and no ‘Post Image’ option to check on the Screen Options.

    The additional image box stopped showing after a WP update, maybe there is a conflict somehow with the JS Handler or something frot he original code. I’m puzzled O_o

    Thread Starter Alkorr

    (@alkorr)

    Alright. I started a brand new install of WP and now everything is working fine. I stll don’t know why it stopped working, I may never know but thank you for your help, your detailed answer pushed me to reinstall everything and you were right, it works fine.

    Thanks again for your help, and sorry for the bother ??

    Moderator bcworkz

    (@bcworkz)

    No bother at all. My next advice would have been to do a manual update (to same version, just refreshing core files), which is essentially the same as a new install. Great minds think alike ?? I think what happened is there was a communications glitch during the update that corrupted a file somewhere related to custom meta boxes.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Second Meta Box for Featured Image not showing anymore since last WP update’ is closed to new replies.