How to let a single post have its own domain name

I am not sure which solution here would work best as a solution but what I am looking for is just a simple way to enter an optional domain through a metabox on the page edit screen and then just select a template from the default page templates wordpress uses.

I am assuming there must be a simple way to accomplish this by manually adding an A name record for the new domain and pointing it to the same IP the main website is using and then through some code allow requests to this new domain to load a specific post ID while utilizing the specific page template you selected.

I would like for the page to be accessible for both domains and just the new domain would utilize the defined template.

How can this be done?

updated
I guess the other way would be to first setup a single page so it can have its own subdomain by using some built in wordpress code used for multisite? In other words, if a custom subdomain could be assigned to a specific post id then a cname record to this subdomain could be set for the new domain… Not sure the best way to accomplish this.

2 Answers
2

This code will allow you to set a custom meta value, and if the domain name (or subdomain if you edit the code) matches it, the query will be changed to match that post. The page template will only be used for that request, not for requests via the “normal” URL.

This does not change links on that page: should they go to the “normal” site or stay in the subdomain?

How you solve this on the DNS side is probably a Server Fault question.

define( 'WPSE4558_STANDARD_SERVER', 'www.example.com' );
define( 'WPSE4558_META_KEY', 'domainname' );

add_filter( 'request', 'wpse4558_request' );
function wpse4558_request( $query_vars )
{
    $query_vars['is_subdomain_request'] = false;
    if ( WPSE4558_STANDARD_SERVER != $_SERVER['SERVER_NAME'] ) {
        $query_vars['meta_key'] = WPSE4558_META_KEY;
        // This can also be just the subdomain, if you edit it
        $query_vars['meta_value'] = $_SERVER['SERVER_NAME'];
        $query_vars['is_subdomain_request'] = true;

    }
    return $query_vars;
}

add_action( 'parse_query', 'wpse4558_parse_query' );
function wpse4558_parse_query( &$wp_query )
{
    if ( $wp_query->get( 'is_subdomain_request' ) ) {
        $wp_query->is_home = false;
        $wp_query->is_page = true;
        $wp_query->is_singular = true;
    }
}

add_filter( 'page_template', 'wpse4558_page_template' );
function wpse4558_page_template( $template )
{
    global $wp_query;
    $id = $wp_query->get_queried_object_id();
    if( ! $wp_query->get( 'is_subdomain_request' ) && get_post_meta( $id, WPSE4558_META_KEY ) ) {
        // This is a page that has a subdomain attached, but the current request is not via that subdomain
        // So use the normal template hierarchy, ignore the page template
        $templates = array();
        $pagename = $wp_query->get_queried_object()->post_name;
        if ( $pagename ) {
            $templates[] = "page-$pagename.php";
        }
        if ( $id ) {
            $templates[] = "page-$id.php";
        }
        $templates[] = "page.php";
        $template = locate_template( $templates );
    }
    return $template;
}

Leave a Comment