Skip to content

Instantly share code, notes, and snippets.

@ronalfy
Created January 12, 2015 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ronalfy/b1a7fdfe893d2806a7c9 to your computer and use it in GitHub Desktop.
Save ronalfy/b1a7fdfe893d2806a7c9 to your computer and use it in GitHub Desktop.
WordPress - WP Page Widget working on is_home
<?php
add_action( 'init', 'opubco_filter_sidebar_widgets_init' );
function opubco_filter_sidebar_widgets_init() {
add_filter('widget_display_callback', 'opubco_filter_widget_display_instance', 12, 3);
add_filter( 'sidebars_widgets', 'opubco_filter_sidebar_widgets', 15, 1 );
}
function opubco_filter_widget_display_instance( $instance, $widget, $args ) {
if ( is_home() ) {
$page_id = get_queried_object_id();
$enable_customize = get_post_meta( $page_id, '_customize_sidebars', true);
if ( $enable_customize && $enable_customize == 'yes' ) {
$widget_instance = get_option('widget_' . $page_id . '_' . $widget->id_base);
if ($widget_instance && isset($widget_instance[$widget->number])) {
$instance = $widget_instance[$widget->number];
}
}
}
return $instance;
}
function opubco_filter_sidebar_widgets( $sidebars_widgets ) {
if ( is_home() ) {
$page_id = get_queried_object_id();
$enable_customize = get_post_meta( $page_id, '_customize_sidebars', true);
$_sidebars_widgets = get_post_meta( $page_id, '_sidebars_widgets', true);
if (isset($enable_customize) && $enable_customize == 'yes' && !empty($_sidebars_widgets)) {
if (is_array($_sidebars_widgets) && isset($_sidebars_widgets['array_version']))
unset($_sidebars_widgets['array_version']);
$sidebars_widgets = wp_parse_args($_sidebars_widgets, $sidebars_widgets);
}
}
return $sidebars_widgets;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment