Hide Featured Image in WordPress Without Plugin

Loading featured image makes more HTTP request and also makes the site load slightly slower. And sometime we want to turn off it in some posts too.

By default, there might not be option to hide featured image in post, but we need to place an option ourselves.

We can place checkbox meta box like we did in “Add Subtitle to WordPress Post“.

Hide Featured Images Without Using Plugin

Why to use any plugin, if we can do it by using few lines of snippets ? Check the snippets below

Step 1 : Create Custom Checkbox Metabox

<?php

/** 
 * Retrieving the values:
 * Enable Featured Image ? = get_post_meta( get_the_ID(), 'enable_featured_imageenable-featured-image', true )
 */
class Blogietech
{
    private $config = '{"title":"Enable Featured Image","prefix":"enable_featured_image","domain":"blogietech","class_name":"blogietech","post-type":["post"],"context":"advanced","priority":"core","fields":[{"type":"checkbox","label":"Enable Featured Image ?","id":"enable_featured_imageenable-featured-image"}]}';

    public function __construct()
    {
        $this->config = json_decode($this->config, true);
        add_action('add_meta_boxes', [$this, 'add_meta_boxes']);
        add_action('admin_head', [$this, 'admin_head']);
        add_action('save_post', [$this, 'save_post']);
    }

    public function add_meta_boxes()
    {
        foreach ($this->config['post-type'] as $screen) {
            add_meta_box(
                sanitize_title($this->config['title']),
                $this->config['title'],
                [$this, 'add_meta_box_callback'],
                $screen,
                $this->config['context'],
                $this->config['priority']
            );
        }
    }

    public function admin_head()
    {
        global $typenow;
        if (in_array($typenow, $this->config['post-type'])) {
?><?php
            }
        }

        public function save_post($post_id)
        {
            foreach ($this->config['fields'] as $field) {
                switch ($field['type']) {
                    case 'checkbox':
                        update_post_meta($post_id, $field['id'], isset($_POST[$field['id']]) ? $_POST[$field['id']] : '');
                        break;
                    default:
                        if (isset($_POST[$field['id']])) {
                            $sanitized = sanitize_text_field($_POST[$field['id']]);
                            update_post_meta($post_id, $field['id'], $sanitized);
                        }
                }
            }
        }

        public function add_meta_box_callback()
        {
            $this->fields_table();
        }

        private function fields_table()
        {
                ?><table class="form-table" role="presentation">
    <tbody><?php
            foreach ($this->config['fields'] as $field) {
            ?><tr>
                <th scope="row"><?php $this->label($field); ?></th>
                <td><?php $this->field($field); ?></td>
            </tr><?php
                }
                    ?></tbody>
</table><?php
        }

        private function label($field)
        {
            switch ($field['type']) {
                default:
                    printf(
                        '<label class="" for="%s">%s</label>',
                        $field['id'],
                        $field['label']
                    );
            }
        }

        private function field($field)
        {
            switch ($field['type']) {
                case 'checkbox':
                    $this->checkbox($field);
                    break;
                default:
                    $this->input($field);
            }
        }

        private function checkbox($field)
        {
            printf(
                '<label class="rwp-checkbox-label"><input %s id="%s" name="%s" type="checkbox"> %s</label>',
                $this->checked($field),
                $field['id'],
                $field['id'],
                isset($field['description']) ? $field['description'] : ''
            );
        }

        private function input($field)
        {
            printf(
                '<input class="regular-text %s" id="%s" name="%s" %s type="%s" value="%s">',
                isset($field['class']) ? $field['class'] : '',
                $field['id'],
                $field['id'],
                isset($field['pattern']) ? "pattern='{$field['pattern']}'" : '',
                $field['type'],
                $this->value($field)
            );
        }

        private function value($field)
        {
            global $post;
            if (metadata_exists('post', $post->ID, $field['id'])) {
                $value = get_post_meta($post->ID, $field['id'], true);
            } else if (isset($field['default'])) {
                $value = $field['default'];
            } else {
                return '';
            }
            return str_replace('\u0027', "'", $value);
        }

        private function checked($field)
        {
            global $post;
            if (metadata_exists('post', $post->ID, $field['id'])) {
                $value = get_post_meta($post->ID, $field['id'], true);
                if ($value === 'on') {
                    return 'checked';
                }
                return '';
            } else if (isset($field['checked'])) {
                return 'checked';
            }
            return '';
        }
    }
    new Blogietech;

Save the above code with the name hide-featured-image.php or any name, but must end with .php extension. You can place this file anywhere inside your theme’s folder. But I prefer placing it inside inc/ folder. And as you can see Blogietech in between the code, you have to use yours own text domain.

Step 2 : Import Your File to functions.php

Now, you need to import the file to functions.php. Here’s how my code looks like in functions.php

require get_template_directory() . '/inc/plugins/hide-featured-image.php';
Hide Featured Images

After doing this, now you can see the option to hide Featured Image on the right side meta boxes area. Check how the box looks like.

Step 3 : Edit content.php

It’s not over yet. Now we need to edit content.php of our theme file. This file is responsible to display the post. You can file content.php inside template-parts folder or varies, it depends on theme itself, how the developer developed it.

Now, you have to search for “the_post_thumbnail“. Out main logic will be, if the box is checked, then only featured image will get displayed. So we need to use php, if statement and wrap the featured image container inside it.

Let’s check how the code looks like.

<?php
// Checking condition for featured image
$featured_image_status = get_post_meta(get_the_ID() , 'enable_featured_imageenable-featured-image', true);
if ($featured_image_status): ?>
            <div class="entry-thumb">
                <?php
    if (has_post_thumbnail())
    {
        the_post_thumbnail('post-thumbnails');
    }
?>
            </div>
        <?php
endif;
// Featured Image Condition Ends

?>

That’s it, you are done. Now onward, your every post will have no featured image, you have to check the box in order to display it.

The Sum Up

I hope, I able to help you to hide featured images without using plugin. But make sure to backup the file I suggest you to edit, just to be sure, everything works fine for future. If you find this tutorial helpful, please leave comment down below and share this post to your friends as well.

You can also find the source code in my Github Repository

Sakar Aryal is a Computer Application student from beautiful town Tilottama, Nepal. He loves coding and blogging. He provides problem solving technique specially related to WordPress, HTML, CSS, php and more other web and tech related things through on his blog , blogietech.com

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.