WordPress 的 plugins_url() 函数不适用于共享主机 [英] WordPress's plugins_url() function not working on shared hosting

查看:16
本文介绍了WordPress 的 plugins_url() 函数不适用于共享主机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立的一个站点使用了 Advanced Custom Fields 插件,并且在我自己的网络主机上的 localhost 上一切正常.不幸的是,当我将站点移至客户购买的主机(GoDaddy 共享主机)时,高级自定义字段插件的 JavaScript 和 CSS 文件无法正确加载.检查来源,问题很明显 - 他们指向以下路径:

http://www.clientsamazingwebsite.com/wp-content/plugins/home/content/06/10145906/html/wp-content/plugins/advanced-custom-fields/js/input-actions.js?ver=3.5.7.2

(如果你仔细看,你会发现这里引用的是服务器上文件的实际路径,而不是 URL)

我已将问题追溯到插件中的以下行:

$this->dir = plugins_url('',__FILE__);

应该返回/wp-content/plugins/advanced-custom-fields

相反,它返回 /wp-content/plugins/home/content/06/10145906/html/wp-content/plugins/advanced-custom-fields

我已经编辑了插件文件,使其指向正确的路径,但是每次更新插件时这些更改都会恢复,因此这不是一个长期的解决方案.

我看到有些人抱怨 __FILE__ 魔术常量在使用符号链接时不能按预期工作,但我当然没有创建任何符号链接.这是使用 GoDaddy 的限制吗?

更新

我注意到 __FILE__ 在 GoDaddy 上返回的内容与在我的本地计算机或其他 Web 服务器上不同.它从文件系统的根目录(即 /srv/www/sitename/public_html/file.php)返回完整路径的两台工作机器,而在 GoDaddy 上,它返回的路径开始在主目录 (/home/content/06/10145906/html/file.php).

这可能是问题吗?

解决方案

根据 Wordpress Codex

<块引用>

您可以将 $path 参数指定为相对于plugins 目录,或者方便地将 __FILE__ 作为第二个参数传递给 make$path 相对于当前 PHP 脚本文件的父目录.

所以使用

plugins_url( basename( __DIR__ ) . '/path/to/plugin/asset.js' );

代替

plugins_url( '/path/to/plugin/asset.js', __FILE__ );

对我来说是可以接受的解决方法.可能不如指定 __FILE__ 那样灵活.

A site I've built uses the Advanced Custom Fields plugin, and everything works well on localhost on my own web host. Unfortunately, when I moved the site to the hosting that the client purchased (GoDaddy shared hosting), the JavaScript and CSS files for the Advanced Custom Fields plugin are not loading properly. Checking the source, the problem is clear - they are pointing to the following path:

http://www.clientsamazingwebsite.com/wp-content/plugins/home/content/06/10145906/html/wp-content/plugins/advanced-custom-fields/js/input-actions.js?ver=3.5.7.2

(if you look carefully you can see that there's a reference to the actual path of the file on the server, not the URL)

I've traced the problem to the following line in the plugin:

$this->dir = plugins_url('',__FILE__);

It should be returning /wp-content/plugins/advanced-custom-fields

Instead it's returning /wp-content/plugins/home/content/06/10145906/html/wp-content/plugins/advanced-custom-fields

I've edited the plugin file so that it points to the proper path, but those changes will revert back every time the plugin is updated, so it's not a long term solution.

I've seen some people complain that the __FILE__ magic constant doesn't work as expected with symlinks, but I certainly didn't create any symlinks. Is this a limitation of using GoDaddy?

Update

I've noticed that __FILE__ returns something different on GoDaddy than on my local machine or on my other web server. One the two working machines it return the full path, from the root of the file system (ie, /srv/www/sitename/public_html/file.php), while on GoDaddy the path it returns begins at the home directory (/home/content/06/10145906/html/file.php).

Could that be the problem?

解决方案

According to Wordpress Codex

You can either specify the $path argument as a hardcoded path relative to the
plugins directory, or conveniently pass __FILE__ as the second argument to make
the $path relative to the parent directory of the current PHP script file.

So using

plugins_url( basename( __DIR__ ) . '/path/to/plugin/asset.js' );

instead of

plugins_url( '/path/to/plugin/asset.js', __FILE__ );

is acceptable workaround for me. Maybe not so agile as specifying __FILE__ though.

这篇关于WordPress 的 plugins_url() 函数不适用于共享主机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆