- This topic has 5 replies, 3 voices, and was last updated 3 years, 3 months ago by
Fernando.
-
AuthorPosts
-
January 26, 2023 at 2:27 am #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() ) ) ); }January 26, 2023 at 4:15 am #2509605David
StaffCustomer SupportHi there,
i assume it is the first code, as the
generate_post_author_outputwon’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.
January 31, 2023 at 11:38 pm #2516616Remez 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
February 1, 2023 at 1:22 am #2516683Fernando 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.
February 7, 2023 at 1:17 am #2524129Remez Sasson
Hi Fernando,
Thank you for your help. I replaced the line you suggested, and now the error message stopped showing.
Thank you
February 7, 2023 at 1:26 am #2524136Fernando Customer Support
You’re welcome, Remez!
-
AuthorPosts
- You must be logged in to reply to this topic.