問題描述

我試圖在獨立的 WP 安裝 (而不是多站點) 上執行此操作。我想要實現的是:

  1. 用户在 usermeta 上保存 domain.com 。 (完成)

  2. 用户創建一個新的 CPT,説 company 。哪個可以通過默認訪問 original.com/company/example-company(完成 – 默認情況下)

  3. 當設置 usermeta domain 時,我需要用户創建的所有帖子,也可以通過 domain.com/company/example-company 使用。

我明白 DNS 和域應該指向當前的 WP 安裝 (不相關),但不知道如何將域映射到固定鏈接。

算法應該是這樣的

  1. 檢查是否顯示 company CPT 單頁。

  2. 檢查作者是否設置了一個域。

  3. 如果設置了 domain,請修改固定鏈接。

最佳解決方案

如果將 domain.com 設置為 original.com 的別名,則在 WordPress 中,您不得不做任何事情使其工作。

問題是數量:一旦在 DNS 中,2 個域是別名,您的 WordPress 的每個 url 都可以通過用户定義的域訪問:domain.com/any/wp/url,還有 domain2.com/any/wp/urldomain3.com/any/wp/url 等等…

那麼,你要做的就是

  1. 檢查 url 是否是用户定義的域之一

  2. 如果是這樣,請檢查請求的頁面是否是單個 CPT,其作者是保存域的作者

  3. 如果沒有,請將請求重定向到原來的 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_SITEURLWP_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 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。