• Resolved Nikita_Sp

    (@nikitasp)


    Hello!

    I’ve noticed that if there is no image in post thumb, than Google structured data throw an error. Please, add the default image field to admin section OR let’s just use the the_custom_logo|site logo instead.

    For example (wp-postratings.php on line 1152):

    
        // Google Rich Snippet
    	$google_structured_data = '';
        $ratings_options['richsnippet'] = isset( $ratings_options['richsnippet'] ) ? $ratings_options['richsnippet'] : 1;
        if( $ratings_options['richsnippet'] && is_singular() && $is_main_loop ) {
            $itemtype = apply_filters( 'wp_postratings_schema_itemtype', 'itemscope itemtype="https://schema.org/Article"' );
    
            if( empty( $post_excerpt ) ) {
                $post_excerpt = ratings_post_excerpt( $post_id, $post->post_excerpt, $post->post_content, $post->post_password );
            }
            $post_meta = '<meta itemprop="headline" content="' . esc_attr( $post_title ) . '" />';
            $post_meta .= '<meta itemprop="description" content="' . wp_kses( $post_excerpt, array() ) . '" />';
            $post_meta .= '<meta itemprop="datePublished" content="' . mysql2date( 'c', $post->post_date, false ) . '" />';
            $post_meta .= '<meta itemprop="dateModified" content="' . mysql2date( 'c', $post->post_modified, false ) . '" />';
            $post_meta .= '<meta itemprop="url" content="' . $post_link . '" />';
            $post_meta .= '<meta itemprop="author" content="' . get_the_author() . '" />';
            $post_meta .= '<meta itemprop="mainEntityOfPage" content="' . get_permalink() . '" />';
            $site_logo = '';
            if ( function_exists( 'the_custom_logo' ) ) {
                $custom_logo_id = get_theme_mod( 'custom_logo' );
                if ( $custom_logo_id ) {
                    $custom_logo = wp_get_attachment_image_src( $custom_logo_id, 'full' );
                    $site_logo = $custom_logo[0];
                }
            }
            if( empty( $site_logo ) ) {
                if( has_header_image() ) {
                    $header_image = get_header_image();
                    if( ! empty( $header_image ) ) {
                        $site_logo = $header_image;
                    }
                }
            }
            $site_logo = apply_filters( 'wp_postratings_site_logo', $site_logo );
            // Image
            if( has_post_thumbnail() ) {
                $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( null ) );
                if( ! empty( $thumbnail ) ) {
                    $post_meta .= '<div style="display: none;" itemprop="image" itemscope itemtype="https://schema.org/ImageObject">';
                    $post_meta .= '<meta itemprop="url" content="' . $thumbnail[0] . '" />';
                    $post_meta .= '<meta itemprop="width" content="' . $thumbnail[1] . '" />';
                    $post_meta .= '<meta itemprop="height" content="' . $thumbnail[2] . '" />';
                    $post_meta .= '</div>';
                }
            }else{
            	$site_logo_info = getimagesize($site_logo);
                $post_meta .= '<div style="display: none;" itemprop="image" itemscope itemtype="https://schema.org/ImageObject">';
                $post_meta .= '<meta itemprop="url" content="' . $site_logo . '" />';
                $post_meta .= '<meta itemprop="width" content="' . $site_logo_info[0] . '" />';
                $post_meta .= '<meta itemprop="height" content="' . $site_logo_info[1] . '" />';
                $post_meta .= '</div>';
            }
        	// Publisher
            $post_meta .= '<div style="display: none;" itemprop="publisher" itemscope itemtype="https://schema.org/Organization">';
            $post_meta .= '<meta itemprop="name" content="' . get_bloginfo( 'name' ) . '" />';
            $post_meta .= '<div itemprop="logo" itemscope itemtype="https://schema.org/ImageObject">';
            $post_meta .= '<meta itemprop="url" content="' . $site_logo . '" />';
            $post_meta .= '</div>';
            $post_meta .= '</div>';
    
            $ratings_meta = '';
            if( $post_ratings_average > 0 ) {
                $ratings_meta .= '<div style="display: none;" itemprop="aggregateRating" itemscope itemtype="https://schema.org/AggregateRating">';
                $ratings_meta .= '<meta itemprop="bestRating" content="' . $ratings_max . '" />';
                $ratings_meta .= '<meta itemprop="worstRating" content="1" />';
                $ratings_meta .= '<meta itemprop="ratingValue" content="' . $post_ratings_average . '" />';
                $ratings_meta .= '<meta itemprop="ratingCount" content="' . $post_ratings_users . '" />';
                $ratings_meta .= '</div>';
            }
    

    Thanks.

    • This topic was modified 7 years, 4 months ago by Nikita_Sp. Reason: code added
Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Lester Chan

    (@gamerz)

    Thanks for the feedback.

    I have added it https://github.com/lesterchan/wp-postratings/commit/711ad185581938ccfb973bf49240b64f071a0c67
    $thumbnail = apply_filters( 'wp_postratings_post_thumbnail', $thumbnail, $post_id );

    You could try it on the dev branch

    https://github.com/lesterchan/wp-postratings/archive/master.zip

    Thread Starter Nikita_Sp

    (@nikitasp)

    Hello!

    I’ve solved the issue with your filter using:

    function ratings_structed_data($string) {
    	if(strpos($string, '<div style="display: none;" itemprop="image" itemscope itemtype="https://schema.org/ImageObject">') === false){
    		$site_logo = '';
    		if ( function_exists( 'the_custom_logo' ) ) {
    			$custom_logo_id = get_theme_mod( 'custom_logo' );
    			if ( $custom_logo_id ) {
    				$custom_logo = wp_get_attachment_image_src( $custom_logo_id, 'full' );
    				$site_logo = $custom_logo[0];
    			}
    		}
    		if( empty( $site_logo ) ) {
    			if( has_header_image() ) {
    				$header_image = get_header_image();
    				if( ! empty( $header_image ) ) {
    					$site_logo = $header_image;
    				}
    			}
    		}
    
    		$site_logo = apply_filters( 'wp_postratings_site_logo', $site_logo );
    
    		$site_logo_info = getimagesize($site_logo);
    		$post_meta = '<div style="display: none;" itemprop="image" itemscope itemtype="https://schema.org/ImageObject">';
    		$post_meta .= '<meta itemprop="url" content="' . $site_logo . '" />';
    		$post_meta .= '<meta itemprop="width" content="' . $site_logo_info[0] . '" />';
    		$post_meta .= '<meta itemprop="height" content="' . $site_logo_info[1] . '" />';
    		$post_meta .= '</div>';
    
    		$position = strpos($string, '<div style="display: none;" itemprop="publisher"');
    		$new_string = substr($string, 0, $position);
    		$new_string .= $post_meta;
    		$new_string .= substr($string, $position);
    		$string = $new_string;
    	}
    
        return $string;
    }
    add_filter( 'wp_postratings_google_structured_data', 'ratings_structed_data', 10, 3 );
    Thread Starter Nikita_Sp

    (@nikitasp)

    So the LOGO image is using when no image is attached to the post.

    BUT!
    Logo in the Organization data must be there.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Default image in schema’ is closed to new replies.