Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#24250 closed defect (bug) (fixed)

Notice thrown in get_page_template_slug in certain conditions

Reported by: tollmanz Owned by: SergeyBiryukov
Milestone: 3.6 Priority: normal
Severity: minor Version: 3.4
Component: Template Keywords:
Focuses: Cc:


When get_page_template_slug() is called outside of the loop and get_post returns null, the check of $post->post_type will trigger the notice:

Notice: Trying to get property of non-object in /path/to/root/wp-includes/post-template.php on line 1284

Steps to reproduce:

  1. Remove all posts from site (or just trash them)
  2. Add the test code below to Twenty Thirteen's functions.php (making sure that Twenty Thirteen is activated)
  3. Load the front page
  4. Notice will appear

function test_get_page_template_slug() {
add_action( 'init', 'test_get_page_template_slug' );

I'm marking as 3.4 since get_page_template_slug() was introduced then.

Attachments (1)

24250.diff (944 bytes) - added by tollmanz 2 years ago.

Download all attachments as: .zip

Change History (6)

@tollmanz2 years ago

comment:1 @tollmanz2 years ago

The diff file also improves the $post_id PHPDoc as it can be null as well as an int.

comment:2 @SergeyBiryukov2 years ago

  • Milestone changed from Awaiting Review to 3.6

comment:3 @SergeyBiryukov2 years ago

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

In 24191:

Avoid a PHP notice in get_page_template_slug() if the page does not exist. props tollmanz. fixes #24250.

comment:4 @lkraav2 years ago

  • Cc leho@… added

comment:5 @SergeyBiryukov2 years ago

#23340 was marked as a duplicate.

Note: See TracTickets for help on using tickets.