注册带或不带钩子的侧边栏
Registering sidebar with or without hook
我对此有点困惑。在 Wordpress 的函数文件中注册侧边栏时,我们使用 register_sidebar() 函数并在其中传递参数。
在不使用任何动作钩子的情况下,侧边栏工作得很好。但是,在手抄本中,我看到一个名为 'widget_init' 的动作挂钩用于注册侧边栏。所以,我的困惑是注册侧边栏的标准方法是什么,有或没有动作挂钩?
我发现 this interesting piece 在注册侧边栏上写着
Bad sidebar code
There are some common things I would like to see changed within
themes. Not all of these things are technically incorrect, but they
can present some unintended consequences or are just needless bits of
code.
Problem #1: Randomly dropping code into functions.php
If you’re a theme developer, you should be familiar with WordPress’
built-in hooks. Not only should you be familiar with them, you should
actually be using them.
The biggest issue I see is sidebar code just being dropped into
functions.php. You should create a sidebar registration function and
hook it to widgets_init. You can see an example of this in the
“Registering a dynamic sidebar” section above.
The reason this is important is so that child themes (and even
plugins) can know exactly when a sidebar was registered. This gives
child themes the opportunity unregister a sidebar if needed. Plus, not
doing it this way is just plain sloppy.
As a sidenote to this: You should never just drop code into
functions.php. Always use the hooks WordPress provides to execute your
functions when they should be executed in the WordPress flow.
我对此有点困惑。在 Wordpress 的函数文件中注册侧边栏时,我们使用 register_sidebar() 函数并在其中传递参数。
在不使用任何动作钩子的情况下,侧边栏工作得很好。但是,在手抄本中,我看到一个名为 'widget_init' 的动作挂钩用于注册侧边栏。所以,我的困惑是注册侧边栏的标准方法是什么,有或没有动作挂钩?
我发现 this interesting piece 在注册侧边栏上写着
Bad sidebar code
There are some common things I would like to see changed within themes. Not all of these things are technically incorrect, but they can present some unintended consequences or are just needless bits of code.
Problem #1: Randomly dropping code into functions.php
If you’re a theme developer, you should be familiar with WordPress’ built-in hooks. Not only should you be familiar with them, you should actually be using them.
The biggest issue I see is sidebar code just being dropped into functions.php. You should create a sidebar registration function and hook it to widgets_init. You can see an example of this in the “Registering a dynamic sidebar” section above.
The reason this is important is so that child themes (and even plugins) can know exactly when a sidebar was registered. This gives child themes the opportunity unregister a sidebar if needed. Plus, not doing it this way is just plain sloppy.
As a sidenote to this: You should never just drop code into functions.php. Always use the hooks WordPress provides to execute your functions when they should be executed in the WordPress flow.