Rank Math SEO Support

By default, if you enable Rank Math SEO it will override the built-in WC Vendors Pro vendor SEO options. Luckily Rank Math SEO is well written so you can easily hook into the plugin and get both plugins working together. 

The following snippet provided by fdeross gets you up and going. 

// Change OG title for Rank Math on Vendor Pages
function wcv_rankmath_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( 'rank_math/frontend/title', 'wcv_rankmath_change_og_title' );

// Change Meta description for Rank Math on Vendor Pages
function wcv_rankmath_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( 'rank_math/frontend/description', 'wcv_rankmath_change_meta_description' );

// Change OG description for Rank Math on Vendor Pages
function wcv_rankmath_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( 'rank_math/frontend/description', 'wcv_rankmath_change_og_description' );

// Change OG image for Rank Math on Vendor Pages
function wcv_rankmath_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( 'rank_math/opengraph/facebook/image', 'wcv_rankmath_change_og_image' );
add_filter( 'rank_math/opengraph/twitter/image', 'wcv_rankmath_change_og_image' );


// Change OG URL for Rank Math on Vendor Pages.
function wcv_rankmath_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( 'rank_math/opengraph/url', 'wcv_rankmath_change_og_url' );

filters

Updated on March 8, 2021

Was this article helpful?