Where is Featured Image code stored in WP?

Well, I’m looking for source code of these two small containers (PHP backend, HTML frontend & JS used to attach the image):

Featured Image

Read More

I want to build a theme option based on Featured Image code, but I’m not able to find it. I’m pretty sure it is somewhere in wp-admin directory, but I’ve spent loong time there and found nothing.

Any clues?

Related posts

Leave a Reply

3 comments

  1. Here is a complete example to have the Featured image meta box display and work outside the wp-admin/post.php page context (since WordPress 4.6, see wp-admin/includes/post.php), which is one of the possible reason why you might want to know where is the code stored:

    /**
     * Featured image metabox
     *
     * @uses _wp_post_thumbnail_html
     *
     * @param int    $post_id   Post ID.
     * @param string $post_type Post type.
     */
    function featured_image_metabox( $post_id, $post_type ) {
    
        // @see edit-form-advanced.php
        $thumbnail_support = current_theme_supports( 'post-thumbnails', $post_type ) && post_type_supports( $post_type, 'thumbnail' );
    
        if ( ! $thumbnail_support ||
            ! current_user_can( 'upload_files' ) ) {
    
            return;
        }
    
        // All meta boxes should be defined and added before the first do_meta_boxes() call (or potentially during the do_meta_boxes action).
        require_once ABSPATH . 'wp-admin/includes/meta-boxes.php';
    
        if ( ! $post_id ) {
            // New post.
            $post = get_default_post_to_edit( $post_type, true );
    
            // Fix PHP error. Add filter so get_post( $post ) returns our post!
            $post->filter = 'raw';
        } else {
            $post = get_post( $post_id );
        }
    
        $thumbnail_id = get_post_meta( $post->ID, '_thumbnail_id', true );
    
        if ( ! $thumbnail_id ) {
    
            $thumbnail_id = -1;
        }
    
        $post_type_object = get_post_type_object( $post_type );
    
        $title = $post_type_object->labels->featured_image;
        ?>
        <script>
            jQuery( document ).ready(function( e ) {
                wp.media.view.settings.post.id = <?php echo json_encode( $post->ID ); ?>;
                wp.media.view.settings.post.featuredImageId = <?php echo json_encode( $thumbnail_id ); ?>;
                wp.media.view.settings.post.nonce = <?php echo json_encode( wp_create_nonce( 'update-post_' . $post->ID ) ); ?>;
            });
        </script>
        <div id="poststuff" style="min-width: 320px;">
            <div id="postimagediv" class="postbox">
                <h2 class='hndle'><span><?php echo esc_html( $title ); ?></span></h2>
                <div class="inside">
                <?php
                    echo _wp_post_thumbnail_html( $thumbnail_id, $post );
                ?>
                </div>
            </div>
        </div>
        <?php
    }
    

    Working example (Sensei LMS course wizard), in the wp-admin, but outside the post context