I am using a plugin called Connections. When a 404 occurs (for good reason, i.e. a page that doesn't exist is accessed via the browser) then the custom post callback filter, specified in the plugin's code breaks, as $wp_query->post
is unset. We get the complaint Trying to get property of non-object when $wp_query->post->ID
is accessed. As you would expect.
I have disabled all other plugins and gone back to Twenty Fifteen theme but the issue still occurs. On a clean install the problem doesn't occur. This doesn't make sense to me. If it's not a plugin or theme issue and it's not in the Wordpress core then where is the issue? A corrupt database? (everything else seems to be working fine).
If I put
public static function filterPostTitle( $title, $id = 0 ) {
global $wp_query, $post, $connections;
// ADDED: If I add this line the problem goes away.
if ( is_404() ) return $title;
// Whether or not to filter the page title with the current directory location.
if ( ! cnSettingsAPI::get( 'connections', 'connections_seo', 'page_title' ) ) return $title;
// If I uncomment the next two lines and comment out the following line of code, then the problem also goes away.
//$post_id = get_queried_object_id();
//if ( ! is_object( $post ) || $post_id != $id || ! self::$filterPermalink ) return $title;
if ( ! is_object( $post ) || $wp_query->post->ID != $id || ! self::$filterPermalink ) return $title;
If I add the following check at the start of the function then the problem also goes away (again as we'd expect):
if ( ! isset( $wp_query->post ) || ! isset( $wp_query->post->ID ) ) return $title;
I have printed out backtraces to give me clues, however the backtraces don't seem to give many clues as the global variable $wp_query is at fault. This is another reminder why globals are bad practise... a very poor show for Wordpress. Globals make debugging a nightmare. Where is the global variable changed? We don't know.
The filter callback is set up as follows:
add_filter( 'the_title', array( __CLASS__, 'filterPostTitle' ), 10, 2 );
The developer of the plugin (maybe rightly so) thinks the problem lies outside of his plugin.
QUESTION: Why is $wp_query->post unset at the time of the callback being triggered? Should $wp_query->post always be set by the time a 'the_title' filter callback is triggered?
As you may guess I am fairly new to Wordpress. It's mostly fantastic... not a fan of the global variables though... a big no-no.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire