Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. PHP Warning: Attempt to read property "post_content" on null

PHP Warning: Attempt to read property "post_content" on null

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
helpphpsysadminannouncement
2 Posts 2 Posters 22 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Aruna KN
    wrote on last edited by
    #1

    I’m using a wordpress theme which has not received updates for 2 years, and they’re not giving any support for that theme but I love this theme. After upgrading my server’s PHP version from 7.4 to 8.1 I’m getting an error_log.

    PHP Warning: Attempt to read property "post_content" on null in /some-path/wp-content/themes/boldr-lite/functions.php on line 267

    PHP Warning: Attempt to read property "post_content" on null in /some-path/wp-content/themes/boldr-lite/functions.php on line 268

    I checked the functions.php and line number 267 and 268 as given below:

    preg_match( '//', $post->post_content )

    || preg_match( '//', $post->post_content )

    And the full function including the above two lines as given below:

    /*
    * Rewrite and replace wp_trim_excerpt() so it adds a relevant read more link
    * when the or quicktags are used
    * This new function preserves every features and filters from the original wp_trim_excerpt
    */
    function boldr_trim_excerpt( $text = '' ) {
    global $post;
    $raw_excerpt = $text;
    if ( '' === $text ) {
    $text = get_the_content( '' );
    $text = strip_shortcodes( $text );
    $text = apply_filters( 'the_content', $text );
    $text = str_replace( ']]>', ']]>', $text );
    $excerpt_length = apply_filters( 'excerpt_length', 55 );
    $excerpt_more = apply_filters( 'excerpt_more', ' [...]' );
    $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );

    	/\* If the post\_content contains a  OR a  quicktag
    	 \* AND the more link has not been added already
    	 \* then we add it now
    	 \*/
    	if (
    		(
    			preg\_match( '//', $post->post\_content )
    			|| preg\_match( '//', $post->post\_content )
    		)
    		&& strpos( $text, $excerpt\_more ) === false
    	) :
    		$text .= $excerpt\_more;
    	endif;
    
    }
    return apply\_filters( 'boldr\_trim\_excerpt', $text, $raw\_excerpt );
    

    }
    remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
    add_filter( 'get_the_excerpt', 'boldr_trim_excerpt' );

    Please suggest a solution to fix the issue

    L 1 Reply Last reply
    0
    • A Aruna KN

      I’m using a wordpress theme which has not received updates for 2 years, and they’re not giving any support for that theme but I love this theme. After upgrading my server’s PHP version from 7.4 to 8.1 I’m getting an error_log.

      PHP Warning: Attempt to read property "post_content" on null in /some-path/wp-content/themes/boldr-lite/functions.php on line 267

      PHP Warning: Attempt to read property "post_content" on null in /some-path/wp-content/themes/boldr-lite/functions.php on line 268

      I checked the functions.php and line number 267 and 268 as given below:

      preg_match( '//', $post->post_content )

      || preg_match( '//', $post->post_content )

      And the full function including the above two lines as given below:

      /*
      * Rewrite and replace wp_trim_excerpt() so it adds a relevant read more link
      * when the or quicktags are used
      * This new function preserves every features and filters from the original wp_trim_excerpt
      */
      function boldr_trim_excerpt( $text = '' ) {
      global $post;
      $raw_excerpt = $text;
      if ( '' === $text ) {
      $text = get_the_content( '' );
      $text = strip_shortcodes( $text );
      $text = apply_filters( 'the_content', $text );
      $text = str_replace( ']]>', ']]>', $text );
      $excerpt_length = apply_filters( 'excerpt_length', 55 );
      $excerpt_more = apply_filters( 'excerpt_more', ' [...]' );
      $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );

      	/\* If the post\_content contains a  OR a  quicktag
      	 \* AND the more link has not been added already
      	 \* then we add it now
      	 \*/
      	if (
      		(
      			preg\_match( '//', $post->post\_content )
      			|| preg\_match( '//', $post->post\_content )
      		)
      		&& strpos( $text, $excerpt\_more ) === false
      	) :
      		$text .= $excerpt\_more;
      	endif;
      
      }
      return apply\_filters( 'boldr\_trim\_excerpt', $text, $raw\_excerpt );
      

      }
      remove_filter( 'get_the_excerpt', 'wp_trim_excerpt' );
      add_filter( 'get_the_excerpt', 'boldr_trim_excerpt' );

      Please suggest a solution to fix the issue

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      As with all such questions, the problem is that $post has not been initialised. Whenever a system complains about some action being attempted on null, or a NullPointerException is thrown, you need to look at the object that the message refers back to.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups