I am trying to do this on a standalone WP installation (not multisite). What I am trying to achieve is:
- User saves
domain.com
on usermeta. (done) - User creates a new CPT, say
company
. Which is accessible via by defaultoriginal.com/company/example-company
(done – by default) - I need all the posts created by the user to make available also via
domain.com/company/example-company
when the usermetadomain
is set.
I understand that the DNS and domain should be pointed to current WP installation (irrelevant), but not sure how to map the domain to a permalink.
Algorithm Should Be Something Like This
- Check if
company
CPT single page is shown. - Check if the author set a domain.
- If
domain
is set, modify the permalink.
If you set
domain.com
as an alias oforiginal.com
, in WordPress you have to do nothing to make it works.The problem is the countrary: once in DNS the 2 domains are aliases, every url of your WordPress will be accessible via user defined domains:
domain.com/any/wp/url
, but alsodomain2.com/any/wp/url
,domain3.com/any/wp/url
and so on…So, what you have to do, is
is the one that saved the domain
Let’s assume you save yout original domain in a constant, maybe in
wp-config.php
now you can easily implement the workflow described above:
Now let’s write a function to redirect request using current url, but with original domain
The last thing to do is filter the permalink creation to use the user-defined domain for singular company CPT urls:
At this point, you have only set DNS for your server, where all the user defined domains are aliases of the original.
Please note code is untested.
A simple constant
WP_SITEURL
could do the trick. I did worked on something similar to it.The procedure i tried –
Checked the host using
$_SERVER['HTTP_HOST']
and Validated if it exist on the database.Comparing your needs, you could check this like –
Next, defined
WP_SITEURL
andWP_HOME
So, all of the links dynamically changed to current host address, and all of them were accessible as like a general wordpress site.