Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
if ( ! function_exists( 'cliff_primary_site_always_the_canonical' ) ) {
/**
* Always have www.primary-site.com as the canonical URL.
*
* If site is loaded from www.example.com, for example.
*
* @link https://developer.wordpress.org/reference/functions/wp_get_canonical_url/
* @link https://kb.yoast.com/kb/canonical-urls-in-wordpress-seo/#code
*
* @param string $canonical_url
*
* @return string
*/
function cliff_primary_site_always_the_canonical( $canonical_url ) {
// HTTP_HOST does not contain primary-site domain
if ( false === strpos( $_SERVER['HTTP_HOST'], 'primary-site.com') ) {
$canonical_url = str_replace( $_SERVER['HTTP_HOST'], 'www.primary-site.com', $canonical_url );
}
return $canonical_url;
}
}
add_filter( 'get_canonical_url', 'cliff_primary_site_always_the_canonical' );
add_filter( 'wpseo_canonical', 'cliff_primary_site_always_the_canonical' );
if ( ! function_exists( 'cliff_example_add_to_cart_url' ) ) {
/**
* If site is loaded from www.example.com, change the Add To Cart button link to something we confirmed works, like
* the Cart URL with the add-to-cart query param.
*
* @see \is_example()
* @see \WC_Product_Simple::add_to_cart_url()
* @see \vc_gitem_template_attribute_woocommerce_product_link()
* @see \wc_get_cart_url()
*
* @param string $url A URL string that already has 'add-to-cart' query param, if it should.
* @param WC_Product $the_class A class that extends this class, such as WC_Product_Simple.
*
* @return string
*/
function cliff_example_add_to_cart_url( $url, $the_class ) {
if (
is_example()
&& $the_class->is_purchasable()
&& $the_class->is_in_stock()
) {
$url = remove_query_arg( 'added-to-cart', add_query_arg( 'add-to-cart', $the_class->get_id(), wc_get_cart_url() ) );
}
return $url;
}
}
// @TODO: The following (commented out) is likely not needed so try without it first!
// add_filter( 'woocommerce_product_add_to_cart_url', 'cliff_example_add_to_cart_url', 10, 2 );
if ( ! function_exists( 'cliff_example_home_page_to_landing_page' ) ) {
/**
* Redirect example.com home page to its landing page.
*
* @see \is_example()
*/
function cliff_example_home_page_to_landing_page() {
if (
! is_front_page()
|| ! is_example()
) {
return;
}
wp_redirect( 'https://www.example.com/some/deep-link/' );
exit;
}
}
add_action( 'template_redirect', 'cliff_example_home_page_to_landing_page' );
<?php
if( ! function_exists( 'is_example' ) ) {
/**
* Whether or not this is example.com (with or without WWW) request.
*
* @return bool
*/
function is_example() {
if (
! empty( $_SERVER['HTTP_HOST'] )
&& (
'example.com' === $_SERVER['HTTP_HOST']
|| 'www.example.com' === $_SERVER['HTTP_HOST']
)
) {
return true;
} else {
return false;
}
}
}
if ( is_example() ) {
$example = 'https://www.example.com';
define( 'WP_SITEURL', $example );
define( 'WP_HOME', $example );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.