最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

What mechanism does WordPress use to keep constants from being redefined?

programmeradmin2浏览0评论

I recently posted a comment on a general tech group about this line of code:

define('WP_POST_REVISIONS', true);

Someone replied, "If the original coders don't know that you can't redefine a constant than why would you even trust any of their other code?"

That made me wonder, what mechanism in WordPress keeps themes/plugins/etc from redefining constants? I'm guessing that it has to do with the order in which code loads.

I recently posted a comment on a general tech group about this line of code:

define('WP_POST_REVISIONS', true);

Someone replied, "If the original coders don't know that you can't redefine a constant than why would you even trust any of their other code?"

That made me wonder, what mechanism in WordPress keeps themes/plugins/etc from redefining constants? I'm guessing that it has to do with the order in which code loads.

Share Improve this question asked May 8, 2020 at 16:38 Nora McDougall-CollinsNora McDougall-Collins 3952 silver badges15 bronze badges 4
  • 5 Constants are controlled at the PHP level, not the WP level. If a plugin tries to redefine a constant, it will throw a PHP error. There's no WordPress-specific magic here. – vancoder Commented May 8, 2020 at 16:50
  • So, the mechanism is PHP. Does that mean that there is no place in the WordPress core that defines WP_POST_REVISIONS? I recently ran across 2 sites that neither showed the Revisions list, nor had the option in Screen Options. I had to add the code in wp-config – Nora McDougall-Collins Commented May 8, 2020 at 16:53
  • 3 WP defines the constant only if it is not already defined: github/WordPress/WordPress/blob/master/wp-includes/… – vancoder Commented May 8, 2020 at 16:57
  • 1 Exactly the information I needed! If you would make this answer, I will check it. – Nora McDougall-Collins Commented May 8, 2020 at 17:00
Add a comment  | 

1 Answer 1

Reset to default 2

What mechanism does WordPress use to keep constants from being redefined?

None. Constants are not a WordPress feature, they're a part of the PHP programming language.

Someone replied, "If the original coders don't know that you can't redefine a constant than why would you even trust any of their other code?"

I think this is them suggesting that if the authors don't know that you cannot redefine a constant, then they might not be very good at PHP

That made me wonder, what mechanism in WordPress keeps themes/plugins/etc from redefining constants? I'm guessing that it has to do with the order in which code loads.

Once a constant is defined, it can't be changed. PHP enforces this, otherwise constants would just be fancy variables that can change on a whim.

Generally, it's considered poor practice to define those constants in plugins and themes. They're intended for wp-config.php and should be set by the user. I would treat any plugin or theme that defines WP constants with great suspicion and skepticism.

发布评论

评论列表(0)

  1. 暂无评论