'Is there any way to have a wordpress multi domain setup with shared post and templates and so on?
I wonder if there is any way to have only one wordpress instant which has 2 different domains (one main domain and one subdomain). The content of the domains should be different but also share pages and templates and posts. E.g. a page "employees" should be the same for both domains.
I have already set up a WordPress network and so the multi-domain setup works but unfortunately pages and posts and templates are separated. There are plugins which mirror e.g. posts also on the other domain but here is then again the problem that I have saved the post 2 times.
Solution 1:[1]
Maybe you already have a plugin which can do this, without any coding, but i'm not familiar with them.
An idea would be to hook onto the save_post action and push the newly created post to the different blogs.
if ( ! function_exists( 'post_to_multiple_sites' ) ) {
add_action( 'save_post', 'post_to_multiple_sites', 20, 2 );
function post_to_multiple_sites($original_id, $original_post) {
// To prevent publishing revisions
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $original_id;
}
// We only want to mess around with a post that has been published
if( 'publish' !== get_post_status( $original_post ) ) {
return $original_id;
}
// prevent "Fatal error: Maximum function nesting level reached"
remove_action( 'save_post', __FUNCTION__ );
/**
* If i'm correct, when creating a network, each site/blog receive an ID.
* You can set these hardcoded, or create a function that returns an array of the blog id's.
*
* If you only have a couple of sites, I would maintain this manually. Otherwise create a function to
* gather all the ID's and return it as an array (up to you).
*/
$blog_ids = [2, 3];
$post_data = [
'post_author' => $original_post->post_author,
'post_date' => $original_post->post_date,
'post_modified' => $original_post->post_modified,
'post_content' => $original_post->post_content,
'post_title' => $original_post->post_title,
'post_excerpt' => $original_post->post_excerpt,
'post_status' => 'publish', // new post will be set to published
'post_name' => $original_post->post_name,
'post_type' => $original_post->post_type,
];
// Gather the post meta & terms
$post_terms = wp_get_object_terms( $original_id, 'category', array( 'fields' => 'slugs' ) );
$post_meta = get_post_custom( $original_id );
foreach ($blog_ids as $blog_id) {
switch_to_blog($blog_id); // https://developer.wordpress.org/reference/functions/switch_to_blog/
// IN case a post with the same slug exists, don't do anything.
// Or maybe create a new post with slug-name-2...up to you.
if ( get_posts( [ 'name' => $post_data[ 'post_name' ], 'post_type' => $post_data[ 'post_type' ], 'post_status' => 'publish' ] ) ) {
restore_current_blog();
continue;
}
$inserted_post_id = wp_insert_post( $post_data );
wp_set_object_terms( $inserted_post_id, $post_terms, 'category', false );
foreach ( $post_meta as $meta_key => $meta_values) {
// we do not need these redirects
if( '_wp_old_slug' === $meta_key ) {
continue;
}
foreach ( $meta_values as $meta_value ) {
add_post_meta( $inserted_post_id, $meta_key, $meta_value );
}
}
restore_current_blog();
}
}
}
It became a bigger function than I expected and honestly don't know if you should go this route. Your safest bet is to try and find a plugin that handles this for you. But you can use this piece of code and extend it obviously to your needs
Make sure to read the comments I added. These are important!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Humpff |
