Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
1  
Your question is pretty complex. From creating a metabox, to managing DNS server records while overriding the fixed-domain wordpress has by design. I suggest you split the question apart to the concrete problems one for it's own. This question is just too specific. –  hakre Dec 5 '10 at 13:52
add comment

2 Answers

up vote 1 down vote accepted

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;
}
share|improve this answer
    
thanks for the answer. This also lets you pick a page template which is independent from the page template right? –  NetConstructor.com Dec 6 '10 at 17:20
    
@NetConstructor: The wpse4558_page_template() filter uses the regular selected page template (in the "Page Attributes" metabox), unless it is a page with a subdomain and you're currently not viewing through the subdomain, then it skips this user-selected template. But it does not use an extra field (so you can't select two templates: one for the subdomain and one for the regular URL). –  Jan Fabry Dec 6 '10 at 17:23
add comment

Updated Answer

Okay, it sounds like you want an option in the write panel that will allow you to compose a post on Site A and, optionally, also publish that post to Site B. It sounds like an interesting project and it should be possible using WordPress and XMLRPC.

Here is some example code. By combining the code in this article with a custom meta box, you should be able to accomplish your goal. Good luck!

Here are some more good resources:

N.B. To ensure updates you make to posts are syndicated, you may also want to store the post_ID of the post on the remote site in a custom field. You should probably also implement a delay of a few minutes before the initial remote post is sent (I know that I inevitably make changes immediately after publishing almost anything, but that may just be me).


Original Answer is Below


Either (i) setup .htaccess with mod_rewrite to mask the post URL, (ii) syndicate your posts across multiple sites using RSS with wp_insert_post or (iii) create a folder within your theme called post-redirect and add the following files to it:

  • post-redirect-meta.php - Create a meta box called "Post Redirect URL" with a text input box that will save to your postmeta table.

  • post-redirect.php - Create a theme template called "Post Redirect" which conditionally (based on the HTTP referrer) executes a 301 redirect using the target URL saved in the "Post Redirect URL" meta box on your post edit page. This will allow you to display the original post in a frame on the new domain.

Based on your stated requirements, these seem like the simplest solutions, but it's far from clear that this is your intent (e.g. what does "I would like for the page to be accessible for both domains" mean? Are both sites running WP? What's the business relationship between the two sites?). It would help if you more clearly explained the final result you are trying to achieve and a list of your most important concerns. Whatever your answer is to this, if possible, please also explain "why".

The code to achieve this isn't complicated, but before going through the trouble, please clarify what you're trying to accomplish.

share|improve this answer
    
@joelhous -- thanks for your response. Regarding a clarification... what I was talking about is that a visitor would still be able to view the applicable page/post from the default domain name (with the applicable theme/template set for that domain) just like would be the default AND if a custom domain name is set and an applicable custom template is selected for the additional single domain page then it would use that template. So this means the page would be accessible through both domains meaning both would show the post differently. Does that clarify things? –  NetConstructor.com Nov 27 '10 at 0:23
    
@joelhous -- Also to clarify the usage a bit better so you understand. What I am trying to do here is have one website with individual posts for "properties". On the main domain name the visitor can search for properties (posts) and clicking on a property detail page would show the default post using the applicable template... THEN... if a custom domain is also set then for example www.property-123.com could be mapped directly to a specific post which means that if you visit that domain you would only see that property and it would use the template you selected. –  NetConstructor.com Nov 27 '10 at 0:26
    
Thanks for the comments, that makes it more clear and I think I understand. If you prefer framing the post on the other domain, the design/layout would be mostly identical to the primary site; alternatively, you can syndicate the post, effectively duplicating the content and displaying it with any design/layout you choose. The second option is a bit more complex, but it would perform better SEO wise. If you want it automated, then you can write a custom script using wp_insert_post(); but if you're happy with publishing manually, then all you need is WP's export/import function. Thoughts? –  joelhaus Nov 27 '10 at 19:32
    
@joelhous -- Well, at least for my situation (and I would assume for others in this situation) I need some way of doing this through the admin area. I know there must be a simple way to customize things so that a url check is automatically done and if matched it would pull up a specific template and the assigned post ID for the query. In respect to integrating it I would think some of the same code used for the multi-site stuff should work as it already deals with this. Do you have a solution which would accomplish this? –  NetConstructor.com Nov 29 '10 at 23:35
    
@NetConstructor.com -- Let me suggest that you change the title of your question. Perhaps something along the lines of "How to Remote Post to WordPress". You may get some better, more specific answers and others will have an easier time discovering this information. –  joelhaus Dec 5 '10 at 19:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.