WordPress 4.6 Fatal error: Cannot redeclare the_post_thumbnail_caption()

When updating one of my websites to WordPress 4.6, I got a fatal error. The error is as following:

Fatal error: Cannot redeclare the_post_thumbnail_caption() (previously declared in /home/ public_html/wp-includes/post-thumbnail-template.php:244) in /home/ public_html/wp-content/themes/wharton/framework/default.php on line 157

The error message is shown while trying to access any page of the site. The path and the theme Wharton can be unique for me but the error message seems to be the same for many users who have updated to WordPress 4.6 “Pepper”. It says that the function the_post_thumbnail_caption() has been declared in two places and it cannot be so. The two files where the function is declared are: (1) post-thumbnail-template.php on line 244 and (1) default.php on line 157. The first file is a part of the WordPress core and the second one is a part of the theme. The only way is to fix this error is to remove one of the functions and it is best if you remove the one in the theme.

So here is how to fix the error:

Using your file manager or ftp, navigate to the theme file which is causing the error. In my case it is in /wp-content/themes/Wharton/framework/default.php. Open the file using your favorite text editor.

Find the function called the_post_thumbnail_caption(). Just search using CTRL + F within the default.php file. The function starts with the name i.e. function the_post_thumbnail_caption() and includes everything in the code block which is covered by opening curly braces { and closing curly braces }.

Here is how the function looks like for me:

function the_post_thumbnail_caption() {
global $post;
$thumbnail_id    = get_post_thumbnail_id($post->ID);
$thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));
if ($thumbnail_image && isset($thumbnail_image[0])) {
return $thumbnail_image[0]->post_excerpt;
}

You can remove the lines but it is better to enclose it inside comment tags which begin in /* and ends in */.

Here’s how the code should look like after wrapping it around comment tags:

/*
function the_post_thumbnail_caption() {
global $post;
$thumbnail_id    = get_post_thumbnail_id($post->ID);
$thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));
if ($thumbnail_image && isset($thumbnail_image[0])) {
return $thumbnail_image[0]->post_excerpt;
}
*/

Save the file and your website should work.

The only issues with this fix are that some theme functionality may not act like it’s supposed to and the error may come back if your theme is upgraded and the theme developer keeps the code. That is why it is suggested to use Child Themes when editing any files inside the theme directory. But it is highly likely if the developer decides to update the theme, the function will probably be removed or renamed to get rid of the error message.

One Reply to “WordPress 4.6 Fatal error: Cannot redeclare the_post_thumbnail_caption()”

Comments are closed.