WordPress.org

Make WordPress Core

Opened 4 years ago

Closed 2 months ago

#40309 closed defect (bug) (fixed)

Notice: in canonical.php line 122

Reported by: bobbingwide Owned by: SergeyBiryukov
Milestone: 5.7 Priority: normal
Severity: normal Version: 3.0
Component: Canonical Keywords: has-patch
Focuses: Cc:

Description

Scenario

  • Attempted to access a post using ?p=22781
  • Post 22781 exists.
  • But it has a post type associated with a plugin that's been deactivated.

Expected result

404 page without a notice

Actual result

404 page with the following Notice
Notice: Trying to get property of non-object in wp-includes\canonical.php on line 122

Explanation

The post_type is not registered so this returns null

$post_type_obj = get_post_type_object($redirect_post->post_type);

Proposed solution

Add test on $post_type_obj.

if ( $post_type_obj && $post_type_obj->public && 'auto-draft' != $redirect_post->post_status ) {

Workaround =

Either re-activate the de-activated plugin, or use the correct post ID... I meant to type ?p=22871

Attachments (3)

40309.diff (855 bytes) - added by bobbingwide 4 years ago.
Test $post_type_obj before accessing public property
40309.1.diff (624 bytes) - added by Hareesh Pillai 2 months ago.
Confirm that the reported issue still remains and the suggested patch resolves it. Refreshing against trunk.
40309.1.2.diff (625 bytes) - added by Hareesh Pillai 2 months ago.

Download all attachments as: .zip

Change History (9)

@bobbingwide
4 years ago

Test $post_type_obj before accessing public property

#1 @bobbingwide
4 years ago

  • Keywords has-patch added

#2 @swissspidy
4 years ago

  • Milestone changed from Awaiting Review to 4.8
  • Version changed from trunk to 3.0

This ticket was mentioned in Slack in #core by obenland. View the logs.


4 years ago

#4 @obenland
4 years ago

  • Milestone changed from 4.8 to Future Release

@Hareesh Pillai
2 months ago

Confirm that the reported issue still remains and the suggested patch resolves it. Refreshing against trunk.

#5 @SergeyBiryukov
2 months ago

  • Milestone changed from Future Release to 5.7
  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

#6 @SergeyBiryukov
2 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 49924:

Canonical: Check if the post type exists when attempting a canonical redirect.

This avoids a PHP notice if the post type is no longer registered.

Follow-up to [14595], [35480].

Props bobbingwide, hareesh-pillai.
Fixes #40309.

Note: See TracTickets for help on using tickets.