admin管理员组文章数量:1130349
I am creating a plugin in which my custom sidebar widget changes content depending on the page it is loading on. One way to do this is
// Register and load the widget
function custom_register_widget() {
register_widget( 'custom_widget' );
}
//trigger on every sidebar load
add_action('dynamic_sidebar', 'custom_register_widget' );
However, this calls the register_widget() on every page load ( thereby making changes to WordPress DB ), thus slowing the page speed.
Is there an efficient way to this?
I am creating a plugin in which my custom sidebar widget changes content depending on the page it is loading on. One way to do this is
// Register and load the widget
function custom_register_widget() {
register_widget( 'custom_widget' );
}
//trigger on every sidebar load
add_action('dynamic_sidebar', 'custom_register_widget' );
However, this calls the register_widget() on every page load ( thereby making changes to WordPress DB ), thus slowing the page speed.
Is there an efficient way to this?
Share Improve this question asked Nov 27, 2018 at 3:19 mallik1055mallik1055 32 bronze badges 1 |1 Answer
Reset to default 1register_widget() doesn't make any changes to the database. All it does is make a particular widget available to be used. It shouldn't be used on the dynamic_sidebar hook. It's supposed to be used on the widgets_init hook.
If you want the contents of a widget to change depending on the current page, then that logic needs to be in the widget itself, in the widget() method:
class My_Widget extends WP_Widget {
public function __construct() {}
public function widget( $args, $instance ) {
if ( is_page() ) {
$page_id = get_queried_object_id();
echo get_the_title( $page_id );
}
}
public function form( $instance ) {}
public function update( $new_instance, $old_instance ) {}
}
That example will output the current page title, if you're viewing a page.
I am creating a plugin in which my custom sidebar widget changes content depending on the page it is loading on. One way to do this is
// Register and load the widget
function custom_register_widget() {
register_widget( 'custom_widget' );
}
//trigger on every sidebar load
add_action('dynamic_sidebar', 'custom_register_widget' );
However, this calls the register_widget() on every page load ( thereby making changes to WordPress DB ), thus slowing the page speed.
Is there an efficient way to this?
I am creating a plugin in which my custom sidebar widget changes content depending on the page it is loading on. One way to do this is
// Register and load the widget
function custom_register_widget() {
register_widget( 'custom_widget' );
}
//trigger on every sidebar load
add_action('dynamic_sidebar', 'custom_register_widget' );
However, this calls the register_widget() on every page load ( thereby making changes to WordPress DB ), thus slowing the page speed.
Is there an efficient way to this?
Share Improve this question asked Nov 27, 2018 at 3:19 mallik1055mallik1055 32 bronze badges 1-
register_widgetdoes not make changes to the database. Also remember that ifdynamic_sidebarruns every time a sidebar is loaded, and you have more than one sidebar, you'll get duplication. And if no sidebar is displayed, the widget is never registered, and won't be available in the widget admin – Tom J Nowell ♦ Commented Nov 27, 2018 at 3:34
1 Answer
Reset to default 1register_widget() doesn't make any changes to the database. All it does is make a particular widget available to be used. It shouldn't be used on the dynamic_sidebar hook. It's supposed to be used on the widgets_init hook.
If you want the contents of a widget to change depending on the current page, then that logic needs to be in the widget itself, in the widget() method:
class My_Widget extends WP_Widget {
public function __construct() {}
public function widget( $args, $instance ) {
if ( is_page() ) {
$page_id = get_queried_object_id();
echo get_the_title( $page_id );
}
}
public function form( $instance ) {}
public function update( $new_instance, $old_instance ) {}
}
That example will output the current page title, if you're viewing a page.
本文标签: plugin developmentHow to create a wordpress widget that dynamically changes according to the page
版权声明:本文标题:plugin development - How to create a wordpress widget that dynamically changes according to the page 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749150266a2323695.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


register_widgetdoes not make changes to the database. Also remember that ifdynamic_sidebarruns every time a sidebar is loaded, and you have more than one sidebar, you'll get duplication. And if no sidebar is displayed, the widget is never registered, and won't be available in the widget admin – Tom J Nowell ♦ Commented Nov 27, 2018 at 3:34