Yoast & WC Vendors Pro

When Yoast is enabled it will treat all vendor store pages as the main archive page. This results in loading the stores Open Graph data instead of the vendors SEO data that they input. You can use the following code to replace the marketplace SEO data with that of the vendor store details that they enter on their store settings page.

// Change OG title for Yoast on Vendor Pages
function wcv_wpseo_change_og_title( $title ) {
	WC_Vendors::log( $title ); 
	if ( WCV_Vendors::is_vendor_page() ) {
		$vendor_shop = urldecode( get_query_var( 'vendor_shop' ) );
		$vendor_id = WCV_Vendors::get_vendor_id( $vendor_shop );
		$shop_title = get_user_meta( $vendor_id, 'pv_shop_name', true );
		$og_title = get_user_meta( $vendor_id, 'wcv_seo_fb_title', true );

		if ( ! empty( $og_title ) ) {
			$title = $og_title;
		} else {
			$title = $shop_title;
		}
	}
	return $title;
}
add_filter( 'wpseo_opengraph_title', 'wcv_wpseo_change_og_title' );

// Change Meta description for Yoast on Vendor Pages
function wcv_wpseo_change_meta_description( $desc ) {
	if ( WCV_Vendors::is_vendor_page() ) {
		$vendor_shop = urldecode( get_query_var( 'vendor_shop' ) );
		$vendor_id   = WCV_Vendors::get_vendor_id( $vendor_shop );
		$shopdesc    = get_user_meta( $vendor_id, 'pv_shop_description', true );
		$meta_desc   = get_user_meta( $vendor_id, 'wcv_seo_meta_description', true );

		if ( ! empty( $meta_desc ) ) {
			$desc = $meta_desc;
		} else {
			$desc = $shopdesc;
		}
	}
	return $desc;
}
add_filter( 'wpseo_metadesc', 'wcv_wpseo_change_meta_description' );

// Change OG description for Yoast on Vendor Pages
function wcv_wpseo_change_og_description( $desc ) {
	if ( WCV_Vendors::is_vendor_page() ) {
		$vendor_shop = urldecode( get_query_var( 'vendor_shop' ) );
		$vendor_id   = WCV_Vendors::get_vendor_id( $vendor_shop );
		$shopdesc    = get_user_meta( $vendor_id, 'pv_shop_description', true );
		$meta_desc   = get_user_meta( $vendor_id, 'wcv_seo_meta_description', true );
		$og_desc     = get_user_meta( $vendor_id, 'wcv_seo_fb_description', true );

		if ( ! empty( $og_desc ) ) {
			$desc = $og_desc;
		} elseif (! empty( $meta_desc)) {
			$desc = $meta_desc;
		} else {
			$desc = $shopdesc;
		}
	}

	return $desc;
}
add_filter( 'wpseo_opengraph_desc', 'wcv_wpseo_change_og_description' );

// Change OG image for Yoast on Vendor Pages
function wcv_wpseo_change_og_image ( $image ) {
	if ( WCV_Vendors::is_vendor_page() ) {
		$vendor_shop = urldecode( get_query_var( 'vendor_shop' ) );
		$vendor_id   = WCV_Vendors::get_vendor_id( $vendor_shop );
		$og_image    = get_user_meta( $vendor_id, 'wcv_seo_fb_image_id', true );

		if (!empty($og_image)) {
			$image = wp_get_attachment_url( $og_image );
		}
	}
	return $image;
}
add_filter( 'wpseo_opengraph_image', 'wcv_wpseo_change_og_image' );

// Change OG URL for Yoast on Vendor Pages.
function wcv_wpseo_change_og_url ( $url ) {
	if ( WCV_Vendors::is_vendor_page() ) {
		$vendor_shop = urldecode( get_query_var( 'vendor_shop' ) );
		$vendor_id   = WCV_Vendors::get_vendor_id( $vendor_shop );
		$url         = WCV_Vendors::get_vendor_shop_page( $vendor_id );
	}

	return $url;
}
add_filter( 'wpseo_opengraph_url', 'wcv_wpseo_change_og_url' );

filters

Updated on May 6, 2020

Was this article helpful?