WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 2 years ago

Last modified 2 years ago

#25334 closed defect (bug) (fixed)

With custom page templates set per page, surprising behavior after switching themes

Reported by: ericlewis Owned by: wonderboymusic
Milestone: 4.2 Priority: normal
Severity: normal Version: 3.8
Component: Themes Keywords: has-patch
Focuses: administration Cc:

Description

If you have custom page templates set for a page, and you switch to a new theme that has no custom templates, bizarre behavior ensues.

When editing one of these pages, upon saving, everything looks fine. But actually, there was is a silent failure around post.php:2903:

if ( !empty($page_template) && 'page' == $data['post_type'] ) {
		$post->page_template = $page_template;
		$page_templates = wp_get_theme()->get_page_templates();
		if ( 'default' != $page_template && ! isset( $page_templates[ $page_template ] ) ) {
			if ( $wp_error )
				return new WP_Error('invalid_page_template', __('The page template is invalid.'));
			else
				return 0;
		}
		update_post_meta($post_ID, '_wp_page_template',  $page_template);
	}

Since the page template is still set for this page, but does not exist in the theme, the function returns early, which means we don't hit a few crucial hooks - edit_post, save_post, and wp_insert_post.

Attachments (1)

25334.patch (444 bytes) - added by nofearinc 4 years ago.

Download all attachments as: .zip

Change History (8)

#1 @nofearinc
4 years ago

  • Cc mario@… added
  • Keywords 2nd-opinion added

I was able to reproduce that as well. Creating a page and assigning a page template from a theme works OK, when switching to a new theme and updating the page it returns earlier in the code above. The problem exists "forever", i.e. if you save the page with the old template a couple times, it would still fall in that same block (and hooks such as save_post are not fired at all).

Any way we could always ignore that issue and just set the default template (or at least whenever $wp_error is false?)

@nofearinc
4 years ago

#2 @vinod dalvi
4 years ago

  • Cc mozillavvd@… added

#3 @jeremyfelt
3 years ago

  • Component changed from Administration to Themes
  • Focuses admin added

#4 @wonderboymusic
2 years ago

  • Keywords has-patch added; dev-feedback 2nd-opinion removed
  • Milestone changed from Awaiting Review to 4.2

#5 @wonderboymusic
2 years ago

  • Owner set to wonderboymusic
  • Resolution set to fixed
  • Status changed from new to closed

In 31020:

If you set '_wp_page_template' for a post, and then switch to a theme that doesn't support the template, subsequent calls to wp_insert_post() will bail early without running a plethora of hooks. This shouldn't happen. We should set the template to 'default' and continue through the rest of the function. The function will still bail if $wp_error is true via wp_insert_post( $arr, true ).

Props nofearinc.
Fixes #25334.

#6 @wonderboymusic
2 years ago

#29269 was marked as a duplicate.

#7 @ocean90
2 years ago

#21268 was marked as a duplicate.

Note: See TracTickets for help on using tickets.