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

  1. Ov3rfly
    Posted 3 years ago #

    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.

  2. alanft
    Plugin Author

    Posted 3 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic