Site logo

Archived topic

Error log file when adding code snippets

5 replies · Started by Remez Sasson on January 26, 2023

Viewing posts 1–6 of 6

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() )
		)
	);
}

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.

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

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.

Hi Fernando,

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

Thank you

You're welcome, Remez!

This archived topic is closed to new replies.