• Resolved manpreetparisjewellers

    (@manpreetparisjewellers)


    Hi I am using snippet to hide the category on shop page but this warning I saw in my error log in every second which increase the size of file. please help!

    PHP Warning: Attempt to read property “slug” on string in /home/wp-content/plugins/code-snippets/php/snippet-ops.php(505) : eval()’d code on line 12

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    Hi?@manpreetparisjewellers,

    This is an error coming from one of your snippets. You have a variable holding a string, and are trying to use it as an object.

    Unfortunately, it’s not something I can fix in the plugin, but if you’re able to find the snippet causing the error and post the code here, I’m happy to help debug. Searching for?slug @line:12?might help.

    Thread Starter manpreetparisjewellers

    (@manpreetparisjewellers)

    Hi Bunge, Thank you so much for your help!

    Here is the code

    add_filter( 'get_terms', 'ts_get_subcategory_terms', 10, 3 );
    
        function ts_get_subcategory_terms( $terms, $taxonomies, $args ) {
    
              $new_terms = array();
    
              // if it is a product category and on the shop page
              if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {
    
                 foreach ( $terms as $key => $term ) {
                       $termobj = get_term($term);
                     if ( ! in_array( $termobj->slug, array( 'uncategorised') ) ) {        //pass the slug name here
                        $new_terms[] = $term;
                     }
              }
              $terms = $new_terms;
        }
    
        return $terms;
    }
    Plugin Author Shea Bunge

    (@bungeshea)

    Hi @manpreetparisjewellers,

    I’ve rewritten the code to add a check confirming that get_term does actually return a WP_Term object before attempting to use it:

    add_filter( 'get_terms', function ( $terms, $taxonomies ) {
    
    	// if it is a product category and on the shop page
    	if ( ! in_array( 'product_cat', $taxonomies ) || is_admin() || ! is_shop() ) {
    		return $terms;
    	}
    
    	$new_terms = array();
    
    	foreach ( $terms as $term ) {
    		$term_object = get_term( $term );
    
    		if ( $term_object && ! is_wp_error( $term_object ) && $term_object->slug !== 'uncategorised' ) {        //pass the slug name here
    			$new_terms[] = $term;
    		}
    	}
    
    	return $new_terms;
    }, 10, 2 );

    Hopefully this fixes up your issues!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Printing Warning in Every second in my error log and increase the file size’ is closed to new replies.