为动态存档禁用 Yoast 14+

Disable Yoast 14+ for a dynamic archive

将 Yoast 与 WC Vendors Pro 一起使用时,它将所有供应商商店页面视为主要商店存档页面。这导致加载主要站点 Open Graph 数据而不是他们输入的供应商 SEO 数据。

在 Yoast 14.0 之前,下面的代码运行良好,但它不再适用于 Yoast 14。关于如何修改它的任何想法,因为他们已经弃用了功能?

remove_action( 'template_redirect', 'wpseo_frontend_head_init', 999 );
add_action( 'template_redirect', 'enable_wcv_store_seo' );
function enable_wcv_store_seo(){
    if ( !WCV_Vendors::is_vendor_page() ) {
        add_action( 'template_redirect', 'wpseo_frontend_head_init', 999 );
    }
}

根据在 Yoast 开发人员门户网站上找到的一段代码,我能够定位供应商页面并成功更改区域设置(作为测试,不需要更改)。

function fleece_wpseo_change_og_locale( $locale ) {
if ( WCV_Vendors::is_vendor_page() ) {
return 'nl_NL';}
}

add_filter( 'wpseo_locale', 'fleece_wpseo_change_og_locale' );

我找不到足够的文档来弄清楚如何定位正确的项目并知道足够多的危险。

我设法编写了一个答案并将其交给了 WC Vendors。这是更通用的选项,以防有人无意中发现这一点。我对最终实现的代码进行了更深入的定制:

// 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' );