Disable post thumbnails in Twenty Thirteen child theme

I've made a theme that's a child of the standard Twenty Thirteen theme. Twenty Thirteen displays post_thumbnail images at the top of each of my pages, and I'd like my theme to not do this.

I could create my own version of all the templates that use it (like page.php, content.php) but this seems overkill for making such a small change.

I tried doing remove_theme_suport('post-thumbnail') but that had no effect.

I tried just deleting the add_theme_support('post-thumbnail') line from Twenty Thirteen's functions.php, but that had no effect.

I could hide the thumbnail using CSS, but the image is still loaded, and I'd prefer the HTML to not be included at all.

I don't want to go through loads of existing posts/pages and remove all the thumbnails I set ages ago.

Any thoughts? Is it possible to override has_post_thumbnail() so that it always returns FALSE?

NOTE: This is not the same as the "header" images Twenty Thirteen puts in the site-header block, but appears below the main navigation, in the main content of the page.

Topic theme-twenty-thirteen post-thumbnails Wordpress

Category Web


How can we force has_post_thumbnails() to return false?

Method #1 - disable theme support:

You can remove the post thumbnails support from the backend, with

function twentythirteen_child_setup()
{
    remove_theme_support( 'post-thumbnails' );
}

add_action( 'after_setup_theme', 'twentythirteen_child_setup', 11 );

but notice that this will not remove the featured images.

If the _thumbnail_id post meta value is non-empty, then has_post_thumbnails() will return true. So we need something else, in the that case.

Method #2 - a script to modify the database:

If you want to un-attach the featured images, you could use for example (untested):

/**
 * Un-attach all featured images:
 */
 $pids = get_posts( 
     array( 
        'post_type'       => array( 'post', 'page' ),
        'meta_key'        => '_thumbnail_id', 
         'fields'         => 'ids',
         'posts_per_page' => 10 // modify this to your needs  
     ) 
 );

 foreach( $pids as $pid )
 {
     // Delete:
     // delete_post_meta( $pid, '_thumbnail_id' ); 

     // Debug:
     printf( ' pid: %d <br/>', $pid );
 }

WARNING: Take a database backup before trying this code snippet.

Method #3 - hijack a filter:

If we go hunting for usable filters, then we are taken down the following path:

  • has_post_thumbnail() calls get_post_thumbnail_id()

  • get_post_thumbnail_id() calls get_post_meta()

  • get_post_meta() calls get_metadata('post', ... )

  • get_metadata() contains the get_{$meta_type}_metadata filter

So the filter to use is the get_post_metadata filter.

To hijack the returning value from the filter, $check must fulfill:

! is_null( $check ) && false === (bool) $check

for has_post_thumbnail() to return false.

To better understand this criteria, we can make some tests:

var_dump( ! is_null( 0     ) && false === (bool) 0      );
var_dump( ! is_null( false ) && false === (bool) false  );
var_dump( ! is_null( null  ) && false === (bool) null   );

which give the following output:

bool(true)
bool(true)
bool(false)

This means we can construct the following code snippet:

/**
 * Force has_post_thumbnail() to return false.
 */

! is_admin() && add_filter( 'get_post_metadata', 
    function( $check, $object_id, $meta_key, $single ) 
    {
        // Hijack the meta data for featured images:
        if ( '_thumbnail_id' === $meta_key )
        { 
            $check = 0; // or false
        }

        return $check;
    }
,10, 4 );

to force has_post_thumbnail() to return false.

You can place this code snippet in the functions.php file in your current theme directory or use it in your custom plugin.

ps: it looks like this has been solved few years ago, check for example this thread.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.