Make WordPress Core

Opened 12 years ago

Closed 12 years ago

#19814 closed defect (bug) (duplicate)

add_theme_support() inside init hook function breaks post attachments

Reported by: jamesmehorter's profile jamesmehorter Owned by: james-mehorter's profile James Mehorter
Milestone: Priority: normal
Severity: normal Version: 3.3.1
Component: Media Keywords:
Focuses: Cc:

Description

When adding post-thumbnail support to a theme via add_theme_support(), inside of an init hook function, the post media attachments no longer work. Simply placing the add_theme_support() call outside of the init hook function corrects this. See attached screenshots for display of the issue.

<?php
	//WORKS - The 'Add Media' panel works as expected
	
	//Initiate our theme setup
	add_action( 'init', 'baltimore_theme_setup' );
	
	//Adding thumbnail image support to our 'Form' custom post type
	add_theme_support('post-thumbnails');
	set_post_thumbnail_size( 150, 194, true ); // Normal post thumbnails
	
	function baltimore_theme_setup() {
		// This theme styles the visual editor with editor-style.css to match the theme style.
		add_editor_style();
	
		// This theme uses wp_nav_menu() in one location.
		register_nav_menu( 'primary', __( 'Primary Menu', 'baltimore' ) );
		
		//Create the 'form-page' post type
		register_post_type('form-page', 'supports' => array('thumbnail');
		
		//Create the 'type' taxonomy for the 'form-page' post type
		register_taxonomy('form-page-type', 'form-page');
	}//end baltimore_setup
	
	
	
	//-------------------------------------------------
	
	
	//DOES NOT WORK - Breaks the 'Add Media' panel 
	
	//DETAILS: The 'Add Media' panel for the post type 'form-page' opens, and allows you to upload media, but the uploaded media is not attached to the post. You can upload a media item, but it halts at 'Crunching'. Going into the Media > Library does show the media item, but it's not attached to the post. Similarly, going into the post after uploaded, and going into the Gallery tab of 'Add Media', shows no media items.
	
	//Initiate our theme setup
	add_action( 'init', 'baltimore_theme_setup' );
	
	function baltimore_theme_setup() {
		//Adding thumbnail image support to our 'Form' custom post type
		add_theme_support('post-thumbnails');
		set_post_thumbnail_size( 150, 194, true ); // Normal post thumbnails
	
		// This theme styles the visual editor with editor-style.css to match the theme style.
		add_editor_style();
	
		// This theme uses wp_nav_menu() in one location.
		register_nav_menu( 'primary', __( 'Primary Menu', 'baltimore' ) );
		
		//Create the 'form-page' post type
		register_post_type('form-page', 'supports' => array('thumbnail');
		
		//Create the 'type' taxonomy for the 'form-page' post type
		register_taxonomy('form-page-type', 'form-page');
	}//end baltimore_setup
	
?>

Attachments (1)

add_theme_support_broken_post_attachment.jpg (64.1 KB) - added by jamesmehorter 12 years ago.
This file shows a screenshot of broken post attachments

Download all attachments as: .zip

Change History (3)

@jamesmehorter
12 years ago

This file shows a screenshot of broken post attachments

#1 @SergeyBiryukov
12 years ago

Calling add_theme_support( 'post-thumbnails' ) on init action and trying to upload an image on the Edit Post screen results in the error:

Fatal error: Call to undefined function get_post_thumbnail_id() in wp-admin/includes/media.php on line 1169

The reason is that post-thumbnail-template.php is required before init:
http://core.trac.wordpress.org/browser/tags/3.3.1/wp-settings.php#L291

So init is too late to call add_theme_support( 'post-thumbnails' ). That said, both uploading and attaching images to posts seem to work in current trunk (in spite of the error).

#2 @SergeyBiryukov
12 years ago

  • Keywords needs-patch removed
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.