douti9253 2017-11-03 13:46
浏览 12
已采纳

切换不起作用的WordPress功能

I have a WordPress-theme that has to be used on multiple sites. So I wrote this function, to determine which site the theme is on:

/*
 * Check which site
*/
function determine_site() {

  $the_site = '';
  $URL = $_SERVER['HTTP_HOST'];

  switch( $URL ){
    case 'domain1.com':
    case 'develop1.ment.com':
      $the_site = 'domain1_com';
      break;
    case 'domain2.com':
    case 'develop2.ment.com':
      $the_site = 'domain2_com';
      break;
    default;
      $the_site = 'domain_undetermined';
      break;
  }
  return $the_site;
}

And this function is then called from another function, which adds it to the body-class on the site, as such:

/*
 * Add body class
 */
function add_custom_body_class( $classes ) {
  $which_language = determine_site();
  $classes[] = $which_language;
  return $classes;
}
add_filter( 'body_class', 'add_custom_body_class' );

But it reaches the default-case and sets domain_undetermined as the body-class (which it should match on one of the first cases). If I print out the $URL-var in the top of the determine_site-function, as such:

function determine_site() {

  $the_site = '';
  $URL = $_SERVER['HTTP_HOST'];
  echo '<pre>;
  print_r($URL);
  echo '</pre>;
  ...
  ...
  ...

Then it outputs this (!?):

develop1.ment.comclass="home page-template-default page page-id-11 logged-in admin-bar no-customize-support wp-custom-logo domain_undetermined">

Why does it print the end of the body-classes, as well?

Another thing that might help figure out where the hatchet is burried... If I run sanitize_title (a WordPress-function) on $URL, as such:

function determine_site() {

  $the_site = '';
  $URL = $_SERVER['HTTP_HOST'];
  $URL = sanitize_title( $URL );
  echo '<pre>';
  print_r($URL);
  echo '</pre>';
  ...
  ...
  ...

... Then it returns this (even though I am on the develop1.ment.com-domain:

www-domain1-comclass="home page-template-default page page-id-11 logged-in admin-bar no-customize-support wp-custom-logo domain_undetermined">

The development-server does some proxy-stuff, which explains this last bit, - but it doesn't explain all of these mysteries.

  • 写回答

1条回答 默认 最新

  • doufei5537 2017-11-06 17:40
    关注

    Would something like this work in place of your own "add body class" function?

    /**
     * Add domain to body class for each site.
     */
    function my_multisite_body_classes( $classes ) {
      $current_blog = get_blog_details( get_current_blog_id() );
      $classes[] = $current_blog->domain;
      return $classes;
    }
    add_filter('body_class', 'my_multisite_body_classes');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?