Title: PHP error / eval()’d code
Last modified: April 8, 2024

---

# PHP error / eval()’d code

 *  Resolved [SilverSunrise](https://wordpress.org/support/users/silversunrise/)
 * (@silversunrise)
 * [2 years, 2 months ago](https://wordpress.org/support/topic/php-error-evald-code/)
 * Hi,
 * since I changed to PHP 8.0 I get this error-message in the server logs:
 * _126359#0: *37455 FastCGI sent in stderr: “PHP message: PHP Warning: Attempt 
   to read property “ID” on null in […/wp-content/plugins/code-snippets/php/snippet-ops.php](https://95.237.205.92.host.secureserver.net/smb/file-manager/code-editor?currentDir=%2Fhttpdocs%2Fwp-content%2Fplugins%2Fcode-snippets%2Fphp&subscriptionId=38&file=snippet-ops.php&redirect=%2Fsmb%2Flog-file%2Fbrowser%2Fid%2F38)(
   582) : eval()’d code on line 5″ while reading response header from upstream_
 * I only have one snippet activated with the following code:
 *     ```wp-block-code
       add_action( 'template_redirect', 'redirect_to_primary_category' );function redirect_to_primary_category() {global $post;// If the post is single.if ( is_single( $post->ID ) ) {// If the post has multiple categories assigned.$cats = wp_get_post_categories( $post->ID );if ( count( $cats ) > 1 ) {// Get the primary category.$primary_category = get_post_meta( $post->ID, 'rank_math_primary_category', true );// If the primary category isn't in the URL, redirect.global $wp;$primary_object = get_term( $primary_category );if ( 0 !== strpos( $wp->request, $primary_object->slug ) ) {$primary_url = home_url( '/' . $primary_object->slug . '/' . $post->post_name . '/' );wp_safe_redirect( $primary_url, 301 );exit;}}}}
       ```
   
 * I cannot find any error in this snippet. Could you please take a look?
 * Thank you so much
 * Joachim
    -  This topic was modified 2 years, 2 months ago by [SilverSunrise](https://wordpress.org/support/users/silversunrise/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fphp-error-evald-code%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 1 replies (of 1 total)

 *  Plugin Author [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * (@bungeshea)
 * [2 years ago](https://wordpress.org/support/topic/php-error-evald-code/#post-17777473)
 * Hi Joachim,
 * You can’t be assured that the `global $post` variable will be set on each instance
   that the function is called. You can just include it in your `if` statement to
   avoid this error:
 *     ```wp-block-code
       add_action( 'template_redirect', 'redirect_to_primary_category' );
       function redirect_to_primary_category() {
       	global $post;
       	// If the post is single.
       	if ( $post && is_single( $post->ID ) ) {
       		// If the post has multiple categories assigned.
       		$cats = wp_get_post_categories( $post->ID );
       		if ( count( $cats ) > 1 ) {
       			// Get the primary category.
       			$primary_category = get_post_meta( $post->ID, 'rank_math_primary_category', true );
       			// If the primary category isn't in the URL, redirect.
       			global $wp;
       			$primary_object = get_term( $primary_category );
       			if ( 0 !== strpos( $wp->request, $primary_object->slug ) ) {
       				$primary_url = home_url( '/' . $primary_object->slug . '/' . $post->post_name . '/' );
       				wp_safe_redirect( $primary_url, 301 );
       				exit;
       			}
       		}
       	}
       }
       ```
   

Viewing 1 replies (of 1 total)

The topic ‘PHP error / eval()’d code’ is closed to new replies.

 * ![](https://ps.w.org/code-snippets/assets/icon.svg?rev=2148878)
 * [Code Snippets](https://wordpress.org/plugins/code-snippets/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/code-snippets/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/code-snippets/)
 * [Active Topics](https://wordpress.org/support/plugin/code-snippets/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/code-snippets/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/code-snippets/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Shea Bunge](https://wordpress.org/support/users/bungeshea/)
 * Last activity: [2 years ago](https://wordpress.org/support/topic/php-error-evald-code/#post-17777473)
 * Status: resolved