Site logo

[Support request] Error log file when adding code snippets

Home Forums Support [Support request] Error log file when adding code snippets

Home Forums Support Error log file when adding code snippets

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #2509528
    Remez Sasson

    Hello,

    I am using 2 code snippets in my code snippets plugin, one for the author and guest author name, and one for removing author link.

    On the front-end everything is okay. However, when I log into the server I see an error log file with many lines showing the following error:

    PHP Warning: Attempt to read property “ID” on null in …/public_html/blog/wp-content/plugins/code-snippets/php/snippet-ops.php(505) : eval()’d code on line 7

    Is it possible that there is an error in the snippets code. I copied them from the Generatepress forum and have been using them for a long time.

    Here is the first snippet – author and guest author name:

    
    add_filter( 'the_author', 'guest_author_name' );
    add_filter( 'get_the_author_display_name', 'guest_author_name' );
    
    function guest_author_name( $name ) {
    global $post;
    
    $author = get_post_meta( $post->ID, 'guest-author', true );
    
    if ( $author )
    $name = $author;
    
    return $name;
    }

    Here is the second snippet – removing author link:

    add_filter( 'generate_post_author_output','tu_no_author_link' );
    function tu_no_author_link() {
    	printf( ' <span class="byline">%1$s</span>',
    		sprintf( '<span class="author vcard" itemtype="http://schema.org/Person" itemscope="itemscope" itemprop="author">%1$s <span class="fn n author-name" itemprop="name">%4$s</span></span>',
    			__( 'By','generatepress'),
    			esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
    			esc_attr( sprintf( __( 'View all posts by %s', 'generatepress' ), get_the_author() ) ),
    			esc_html( get_the_author() )
    		)
    	);
    }
    #2509605
    David
    Staff
    Customer Support

    Hi there,

    i assume it is the first code, as the generate_post_author_output won’t be found in the admin area.

    So try changing the first snippet to:

    
    add_filter( 'the_author', 'guest_author_name' );
    add_filter( 'get_the_author_display_name', 'guest_author_name' );
    
    function guest_author_name( $name ) {
        if ( !is_admin() ) {
            global $post;
            $author = get_post_meta( $post->ID, 'guest-author', true );
            if ( $author )
            $name = $author;
        }
        return $name;
    }

    This will make it apply to the front end only. And should stop the error.

    #2516616
    Remez Sasson

    Thank you for your help.

    I changed the snippet as you suggested, but I am still getting this error in my log files:

    PHP Warning: Attempt to read property “ID” on null in /…/…/…/blog/wp-content/plugins/code-snippets/php/snippet-ops.php(505) : eval()’d code on line 7

    #2516683
    Fernando
    Customer Support

    Hi Remez,

    To test, can you try replacing this line:

    $author = get_post_meta( $post->ID, 'guest-author', true );

    with this:

    $author = get_post_meta( get_the_ID(), 'guest-author', true );

    Let us know how it goes.

    #2524129
    Remez Sasson

    Hi Fernando,

    Thank you for your help. I replaced the line you suggested, and now the error message stopped showing.

    Thank you

    #2524136
    Fernando
    Customer Support

    You’re welcome, Remez!

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.