Support » Plugin: Widget Logic » Solution for "Notice: Trying to get property of non-object .. eval()'d code .."

  • About the annoying php debug notice when WP_DEBUG is true and global $post; is used:

    Notice: Trying to get property of non-object … eval()’d code in line …

    As explained on Other Notes page of Widget Logic plugin, it is possible to access $post by using e.g.

    global $post; return (is_page('home') || ($post->post_parent=="13"));

    This example causes the above explained error under certain conditions.

    Another shorter example which also can trigger the error:

    global $post; return ( $post->post_name == 'test' );

    To avoid the error, you simply have to check if $post is available before accessing its properties:

    Fixed example, with an extra check, no more php debug notice:

    global $post; return ( !empty( $post ) && $post->post_name == 'test' );

    Obviously the eval() is called two times by WordPress, first time when a $post is not yet there and a second time when the widget is finally displayed in sidebar etc.

    Suggested fix: Include this information in Widget Logic documentation.

    Thanks for this great plugin.

Viewing 1 replies (of 1 total)
  • Plugin Contributor alanft

    (@alanft)

    I’ll mark this to add in to the FAQ (best place for it? though it’s not been ‘frequent’… yet!). And… my thanks.

Viewing 1 replies (of 1 total)
  • The topic ‘Solution for "Notice: Trying to get property of non-object .. eval()'d code .."’ is closed to new replies.