問題描述
我試圖在獨立的 WP 安裝 (而不是多站點) 上執行此操作。我想要實現的是:
-
使用者在 usermeta 上儲存
domain.com。 (完成) -
使用者建立一個新的 CPT,說
company。哪個可以透過預設訪問original.com/company/example-company(完成 – 預設情況下) -
當設定 usermeta
domain時,我需要使用者建立的所有帖子,也可以透過domain.com/company/example-company使用。
我明白 DNS 和域應該指向當前的 WP 安裝 (不相關),但不知道如何將域對映到固定連結。
演演算法應該是這樣的
-
檢查是否顯示
companyCPT 單頁。 -
檢查作者是否設定了一個域。
-
如果設定了
domain,請修改固定連結。
最佳解決方案
如果將 domain.com 設定為 original.com 的別名,則在 WordPress 中,您不得不做任何事情使其工作。
問題是數量:一旦在 DNS 中,2 個域是別名,您的 WordPress 的每個 url 都可以透過使用者定義的域訪問:domain.com/any/wp/url,還有 domain2.com/any/wp/url,domain3.com/any/wp/url 等等…
那麼,你要做的就是
-
檢查 url 是否是使用者定義的域之一
-
如果是這樣,請檢查請求的頁面是否是單個 CPT,其作者是儲存域的作者
-
如果沒有,請將請求重定向到原來的 domainù
我們假設你把一個原始域儲存在一個常量中,也許在 wp-config.php 中
define('ORIGINAL_DOMAIN', 'original.com');
現在,您可以輕鬆實現上述工作流程:
add_action('template_redirect', 'check_request_domain', 1);
function check_request_domain() {
$domain = filter_input(INPUT_SERVER, 'HTTP_HOST', FILTER_SANITIZE_URL);
// strip out the 'www.' part if present
$domain = str_replace( 'www.', '', $domain);
// if the request is from original domain do nothing
if ( $domain === ORIGINAL_DOMAIN ) return;
// if it is not a singular company CPT request redirect to same request
// but on original domain
if ( ! is_singular('company') ) {
redirect_to_original(); // function defined below
}
// if we are here the request is from an user domain and for a singular company request
// let's check if the author of the post has user meta, assuming meta key is `'domain'`
// and the meta value is the same of domain in current url
$meta = get_user_meta( get_queried_object()->post_author, 'domain', TRUE );
if ( $meta !== $domain ) { // meta doesn't match, redirect
redirect_to_original(); // function defined below
} else {
// meta match, only assuring that WordPress will not redirect canonical url
remove_filter('template_redirect', 'redirect_canonical');
}
}
現在我們來寫一個函式來重定向請求使用當前的 url,但與原始域
/**
* Redirect the request to same url, but using original domain
*/
function redirect_to_original() {
$original = untrailingslashit( home_url() ) . add_query_arg( array() );
wp_safe_redirect( $original, 301 );
exit();
}
最後一件事是過濾固定連結建立,以將 user-defined 域用於單一公司 CPT 網址:
add_filter( 'post_type_link', 'custom_user_domain_plink', 999, 2 );
function custom_user_domain_plink( $post_link, $post ) {
// we want change permalink only for company cpt posts
if ( $post->post_type !== 'company' ) return $post_link;
// has the user setted a custom domain? If not, do nothing
$custom = get_user_meta( $post->post_author, 'domain', TRUE );
if ( empty($custom) ) return $post_link;
// let's replace the original domain, with the custom one, and return new value
return str_replace( ORIGINAL_DOMAIN, $custom, $post_link);
}
此時,您只為伺服器設定 DNS,其中所有使用者定義的域都是原始的別名。
請注意程式碼未經測試。
次佳解決方案
一個簡單的常數 WP_SITEURL 可以做到這一點。我做過類似的事情。
The difference is all of the domains were hosted on the same server and pointed to the root directory.
我試過的程式 –
使用 $_SERVER['HTTP_HOST']檢查主機,並在資料庫中存在驗證。比較你的需求,你可以檢查一下 –
global $wpdb;
$domain_user = $wpdb->get_var(
"SELECT user_id FROM $wpdb->usermeta".
" WHERE meta_key = 'domain'".
" AND meta_value='". $_SERVER['HTTP_HOST'] ."'"
);
// if an user found, do further processing.
// Exclude posts by other user using pre_get_posts may be.
接下來,定義 WP_SITEURL 和 WP_HOME
define( 'MY_SITE_DOMAIN', $_SERVER['HTTP_HOST'] );
if( !defined( 'WP_SITEURL' )):
if( is_ssl())
define( 'WP_SITEURL', 'https://'. MY_SITE_DOMAIN );
else
define( 'WP_SITEURL', 'http://'. MY_SITE_DOMAIN );
endif;
if( !defined( 'WP_HOME' ) ):
define( 'WP_HOME', WP_SITEURL );
endif;
所以,所有的連結動態地改變到當前的主機地址,並且它們都可以像一般的 wordpress 站點一樣可訪問。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。