In functions.php
write this code
function my_optionally_widgets()
{
$option = get_options('wantwidget');
if($option == 'yes')
{
register_widget('mywidget');
/* add other widgets for registration here */
}
}
add_action('widgets_init', 'my_optionally_widgets');
function my_optionally_sidebars()
{
$option = get_options('wantsidebar');
if($option == 'yes')
{
register_sidebar($args);
/* add other sidebars for registration here */
}
}
add_action('init', 'my_optionally_sidebars');
Remember that Widget Areas
and Sidebars
are the exact same thing. But the code above shows you how to enable and disable widgets and sidebars from appearing in the dashboard.
And this is how you display them on the front-end:
$option = get_options('wantsidebar');
if($option == 'yes')
{
dynamic_sidebar($index);
}
Related Posts:
- Add multiple custom fields to the general settings page
- Customising widget titles/headings in WP 5.8
- Hook specific functions if on a specific admin page
- “options.php” not found
- Random text changing to weird icons in both admin & front end
- How to load scripts/styles specific for a page
- Child theme – Overriding ‘require_once’ in functions.php
- How to add css class to cloud tag anchors?
- How to add first name & last name to default registration form?
- Issue with get_theme_mod returning a blank value instead of the saved value
- bloginfo() vs get_option?
- Need to create a Theme demo site that features multiple themes
- Hourly Routine Not Firing ( wp_schedule_event() )
- Show/hide Widgets in Dashboard Based on Current Advanced Custom Fields Option
- How to create a widgetized sidebar for every category dynamically?
- Create new user without password
- How to display error messages using WP_Error class?
- which is the function that removes accented vowels?
- remove from text-widget
- Programmatically Add Font-Awesome Icons to Category Widget
- Theme Demo in live Site
- Dynamically Register Sidebars For Each Top Level Page
- Restrict user registration to emails on a single domain
- Widgets Section not displaying in theme customizer
- What is the most efficient method for loading widgets in functions.php
- Theme options WP Editor
- Updating users_can_register option?
- Where does function_exists() look to decide whether a function exists? [closed]
- Theme-Config tab in admin Pannel?
- Ajaxing function in widget class
- Increment price for Woocommerce Minicart [closed]
- Custom registration URL lost when user makes signup mistake
- Fatal error when trying to get admin email on contact form
- options.php problem – data not saved
- Missing argument 2 for a custom function widgets_init
- How to remove howdy dropdown menu content
- How to add custom fields to my custom registration form
- Adding widget to dashboard with wp_dashboard_setup not working
- How can I change the do_action function in footer (copyright area)
- How to insert custom widget in custom sidebar in theme activation?
- Echo tags used to describe the theme
- is_customize_preview() like function to check if Customizer “Live Preview” in JavaScript
- changing behaviour of get_search_form
- Warning: Creating default object… when altering Customize panels
- How to load jQuery in the footer – nothing works for me
- Translation of theme using child theme
- Display alert on successful user Sign Up
- Theme functions.php file auto delete everyday [closed]
- WordPress website Rs changed to currency symbol
- Two Types of settings in WP – The Doubt
- Functions in custom theme
- Parse error after modifying the template menu [closed]
- How to call particular funcion when the check box is checked in plugin options page
- Move Custom Fields on General Settings Page to New Location
- Want to be able to sign up subscribers as authors
- WordPress Shortcode function display outside of widget
- Get rid of the widget Item Class=”widget-item”
- Not all CSS elements transferring via parent to child?
- Can’t Write Custom Widget Code In One ECHO
- Filter nav menu items HTML tags and wrap inner text with span
- Redirect website domain (including all pages) to external URL after Popup message (few seconds)
- Adding additional roles on registration
- Why is it so hard to add a class to the tag in the sidebar widget?
- The correct way to add a JavaScript in the functions.php [closed]
- Passing user enetered value in widget: number of words, for example
- Register_Sidebar overwriting itself and doesn’t exist in global $wp_registered_sidebars;
- widgets not working
- How to echo Widget Title in Custom Frontend-Template Box
- the_widget() inside functions.php
- How do I preserve changes to a theme’s functions.php across updates?
- How to conditionally add a widget via a hook in functions.php
- Creating a custom wordpress widget and stopping js from running twice(once in active widget once in widget selector)?
- How Do I include/Import a Custom Widget from the Parent theme into My Child Theme?
- What is the most efficient way of adding additional functionaliy for admin only
- Include widget file in functions.php of child theme
- Astra theme – cannot set short product description under the product title (product name)
- Class parameter in get_avatar args doesn’t get added to output
- Unable to login after registration
- CSS style and app.js not loading
- Extend WordPress Audio Widget to accept HLS (.m3u8) URLs
- my wordpress doesn’t save items to customized widget area
- Copying a widget class to functions.php results on a blank page
- Set display name from nickname with register process
- Changing the register url is not working
- Getting back a blank WordPress site following functions code edit
- us states dropdown function and echo in theme template files
- How to pass a variable from a template page to the child theme functions.php
- Expanding a function to call a plugin or show a link
- How to programmatically GET (to know) which Sidebar (or) which Menu is being used on Different Pages?
- Settings in functions.php used by a plugin
- Mail function not working with user accounts
- Additional Sidebar
- Give a function a unique ID
- Add the title of a widget as an ID – for anchor links
- Help finishing script to export WP user data when form submitted
- Which template file to edit to edit homepage in Mystile [closed]
- Adding function to function.php error 505 [closed]
- My website is generating weired url parameters of paginated pages
- Swap WordPress “Widget Area” based on Page Template
- How to automatically load Google Fonts on pages only as they’re used?