Cannot remove comments from schema after update

#15479
Viewing 3 replies - 1 through 3 (of 3 total)
  • Hello,

    Thank you for contacting Rank Math today.

    Could you please try the following code:

    /**
     * Filter: Remove/modify schema data.
     *
     * @param array $return Array of json-ld data.
     */
    add_filter( 'rank_math/json_ld', function( $data, $json ) {
    	if ( is_home() && isset( $data['Comment'] ) ) { 
    		unset( $data['Comment'] );
    	}
    	return $data;
    }

    Looking forward to helping you. Thank you.

    ​​​​​​

    Hi,

    Your code is not valid, it makes a PHP crash, it should have been:

    add_filter( 'rank_math/json_ld', function( $data, $json ) {
    	if ( is_home() && isset( $data['Comment'] ) ) { 
    		unset( $data['Comment'] );
    	}
    	return $data;
    }, 100, 2);
    

    But this does not work either, in fact, it should be:

    add_filter( 'rank_math/json_ld', function( $data, $json ) {
    	if ( isset( $data['Blog'] ) ) { 
    		foreach ($data['Blog']['blogPost'] as $index => $blogpost) {
    			unset ( $data['Blog']['blogPost'][$index]['comment'] );
    		}
    		
    	}
    	return $data;
    }, 100, 2);

    This effectively removes all the comments.

    But finally, I settled on:

    add_filter( 'rank_math/json_ld', function( $data, $json ) {
    	if ( isset( $data['Blog'] ) ) { 
    		unset ($data['Blog']['blogPost']);
    	}
    	return $data;
    }, 100, 2);

    I simply do not like the verbosity of the blogPost part of the schema.

    Thanks for trying to help me!

    Hello,

    Thanks for getting back to us and for sharing the working snippet.

    Feel free to contact us for any other questions, comments or suggestions.

    Thank you.

Viewing 3 replies - 1 through 3 (of 3 total)

The ticket ‘Cannot remove comments from schema after update’ is closed to new replies.