Skip to content

Instantly share code, notes, and snippets.

@mustafakucuk
Created November 17, 2018 20:11
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 mustafakucuk/cc72c3d353ec9cafb4b034b4c5f889df to your computer and use it in GitHub Desktop.
Save mustafakucuk/cc72c3d353ec9cafb4b034b4c5f889df to your computer and use it in GitHub Desktop.
WordPress add widgets to sidebars during theme activation.
<?php
// This codes works when theme activate.
function switch_theme(){
$widgets = get_option( 'sidebars_widgets' );
$widgets['home_sidebar'] = array(
'search-1', // widgetname-1
'text-1',
'categories-1',
);
// widget = prefix
// Modify categories widget settings.
update_option( 'widget_categories', array( 1 => array( 'title' => 'this is custom title.', 'count' => 'on' ) ) );
// Update sidebars widgets with new settings.
update_option( 'sidebars_widgets', $widgets );
}
add_action( 'after_switch_theme', 'switch_theme' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment