WordPress query_var by domain

I would like to add a request variable to all requests from a specific domain.

For example, mydomain.com and proxydomain.com show the same WordPress site, but for users visiting through proxydomain.com, I would like to be able to handle their requests in different ways.

In addition, I would like to apply several different CSS styles for visitors passing through proxydomain.com.

I thought I could check query_var and apply classes based on the presence of this variable.

+5
source share
2 answers

This is the code to add to your functions.php file:

 add_filter( 'body_class', 'domain_as_body_class' ); function domain_as_body_class( $classes ) { $classes[] = sanitize_title( $_SERVER['SERVER_NAME'] ); return $classes; } 

It adds the sanitized domain of your site (i.e. mydomain-com or proxydomain-com ) as the class class of the body tag of your pages, so you can configure the relative class for custom styles.

Update

For queries, you can add the function again in functions.php , for example:

 function is_proxydomain() { return 'proxydomain.com' == $_SERVER['SERVER_NAME']; } 

And then use it when necessary for the request:

 if( is_proxydomain() ) { $args = array( // arguments for proxydomain.com ); } else { $args = array( // arguments for mydomain.com ); } $query = new WP_Query( $args ); 
+10
source

I like the answer d79 for the first part.

For queries, I think it would be better to extend the WP_Query class (i.e. WP_Query_Custom) and have one copy for each domain. Then you can download the file you need based on the domain in the functions.php file, and therefore you do not need to change your calls in the future wherever you use WP_Query_Custom, even if you need to add additional domains and different versions of WP_Query_Custom.

 //in functions.php $mydomain = str_replace('.', '_', $_SERVER['SERVER_NAME']); require_once("path/to/my/classes/$mydomain/WP_Query_Custom.php"); //In each path/to/my/classes/$mydomain/WP_Query_Custom.php class WP_Query_Custom extends WP_Query { function __construct( $args = array() ) { // Force these args $args = array_merge( $args, array( 'post_type' => 'my_custom_post_type', 'posts_per_page' => -1, // Turn off paging 'no_found_rows' => true // Optimize query for no paging ) ); add_filter( 'posts_where', array( $this, 'posts_where' ) ); parent::__construct( $args ); // Make sure these filters don't affect any other queries remove_filter( 'posts_where', array( $this, 'posts_where' ) ); } function posts_where( $sql ) { global $wpdb; return $sql . " AND $wpdb->term_taxonomy.taxonomy = 'my_taxonomy'"; } } 

Sample class is copied from WP_Query extension

+1
source