How to add multiple copies of a widget from “available widgets” - php

I've found a widget that I need to add to more than one sidebar widget area.
However, apparently its been coded so that once you drag it from "available widgets" over to a sidebar widget, it disappears from the "available widgets" listing.
here is the code from the functions.php
if (function_exists('register_sidebar')) {
register_sidebar(array(
'name' => 'Blog Widgets',
'id' => 'sidebar-widgets',
'description' => 'Widget Area',
'before_widget' => '<div id="one" class="two">',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
}
here is the code from my index.php
<p><?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar Widgets') ) : ?>
<?php endif; ?></p>
you help will appreciated
thank you

Related

How to create a search widget in Wordpress with the theme Hello Elementor

I'm using the theme Hello Elementor for Wordpress and I want to include in the main menu a search option for giving the visitors the possibility of searching for an entry. I've seen that other themes include this possibility: they include a widget area in the menu, but I don't want to change my theme.
Would it be possible?
I have tried by creating a widget area by codding on PHP but it creates the search option below the menu, and I want it to be next to it.
This is the PHP code I've used:
functions.php
function register_widgets() {
register_sidebar( array(
'name' => __( 'After Content Widget Area'),
'id' => 'after-content-widget-area',
'description' => __( 'Widget area after the content' ),
'before_widget' => '<div id="%1$s" class="widget-container %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
}
header.php
if ( is_active_sidebar( 'after-content-widget-area' ) ) { ?>
<aside class="after-content widget-area full-width" role="complementary">
<?php dynamic_sidebar( 'after-content-widget-area' ); ?>
</aside>
<?php }

How to register and show 2nd sidebar only if the screen is wider than 1682px in wordpress?

I want to show a 336px width second sidebar (left side on all pages) for destop/tv screens wider than 1682px.
If the second sidebar has its own class then I can hide it with the {display: none;} CSS tag inside the media query feature.
Currently, my template only has 1 sidebar which can be shown either on the right side or left side. It is currently on the right side.
This script is already present on my functions.php
/*-----------------------------------------------------------------------------------*/
// Register widgetized areas, including two sidebars and four widget-ready columns in the footer.
// To override skeleton_widgets_init() in a child theme, remove the action hook and add your own
// function tied to the init hook.
/*-----------------------------------------------------------------------------------*/
if ( !function_exists( 'skeleton_widgets_init' ) ) {
function skeleton_widgets_init() {
// Area 1, located at the top of the sidebar.
register_sidebar( array(
'name' => __( 'Posts Widget Area', 'smpl' ),
'id' => 'sidebar-1',
'description' => __( 'Shown only in Blog Posts, Archives, Categories, etc.', 'smpl' ),
'before_widget' => '<div id="%1$s" class="widget-container %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
// Area 2, located below the Primary Widget Area in the sidebar. Empty by default.
register_sidebar( array(
'name' => __( 'Pages Widget Area', 'smpl' ),
'id' => 'sidebar-2',
'description' => __( 'Shown only in Pages', 'smpl' ),
'before_widget' => '<div id="%1$s" class="widget-container %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
// Area 3, located in the footer. Empty by default.
register_sidebar( array(
'name' => __( 'First Footer Widget Area', 'smpl' ),
'id' => 'footer-widget-area-1',
'description' => __( 'The first footer widget area', 'smpl' ),
'before_widget' => '<div class="%1$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
// Area 4, located in the footer. Empty by default.
register_sidebar( array(
'name' => __( 'Second Footer Widget Area', 'smpl' ),
'id' => 'footer-widget-area-2',
'description' => __( 'The second footer widget area', 'smpl' ),
'before_widget' => '<div class="%1$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
// Area 5, located in the footer. Empty by default.
register_sidebar( array(
'name' => __( 'Third Footer Widget Area', 'smpl' ),
'id' => 'footer-widget-area-3',
'description' => __( 'The third footer widget area', 'smpl' ),
'before_widget' => '<div class="%1$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
// Area 6, located in the footer. Empty by default.
register_sidebar( array(
'name' => __( 'Fourth Footer Widget Area', 'smpl' ),
'id' => 'footer-widget-area-4',
'description' => __( 'The fourth footer widget area', 'smpl' ),
'before_widget' => '<div class="%1$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
}
/** Register sidebars by running skeleton_widgets_init() on the widgets_init hook. */
add_action( 'widgets_init', 'skeleton_widgets_init' );
}
/** Next code follows from this point...
This is the code present on sidebar.php
<?php
/**
* The Sidebar containing the primary blog sidebar
*
*/
// hide sidebars with sidebars=false custom field
if (is_singular() && get_post_meta($post->ID, "sidebars", $single = true) == "false") {
return;
}
if ( is_active_sidebar( 'sidebar-1' ) ) {
do_action('skeleton_before_sidebar');
dynamic_sidebar( 'sidebar-1' );
do_action('skeleton_after_sidebar');
}
?>
As per this guide, https://wordpress.stackexchange.com/questions/135403/display-sidebar-that-created-in-functions-php , I added the code of the first answer to the functions.php file
// 2nd Sidebar
add_action( 'widgets_init', 'wpsites_add_widget' );
function wpsites_add_widget() {
register_sidebar(array(
'name'=>'Sidebar-Aries',
'id' => 'sidebar-aries',
'description' =>'Display all the contents of sidebar at Aries page.',
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
));
}
Then added the other code into the single.php file
<?php
/**
* The Template for displaying all single posts.
*
* #package WordPress
* #subpackage skeleton
* #since skeleton 0.1
*/
get_header();
do_action('skeleton_before_content');
get_template_part( 'loop', 'single' );
do_action('skeleton_after_content');
get_sidebar();
get_footer();
<?php if ( is_active_sidebar( 'sidebar-aries' ) ) : ?>
<ul id="sidebar">
<?php dynamic_sidebar( 'sidebar-aries' ); ?>
</ul>
<?php endif; ?>
?>
Then I visited Appearance > Widgets and checked if there is any new sidebar but there is none.
I also tried the code from another question but it didn't work for me as well.
Then I followed this guide - https://smallbusiness.chron.com/build-website-php-sidebar-60499.html
Step: 5 - Locate the lines of code that register the sidebars in
"functions.php" -- they are usually near the top. Edit the code to
make “register_sidebar” plural, and add a number “2” between the
brackets in the line below, like this:
if ( function_exists('register_sidebars') ) register_sidebars(2);
Click "Update File." The sidebar is now available by going to
"Appearance" and then "Widgets."
But the code present on my functions.php looks a bit different and it didn't work.
What should I do?
STEP I: Open the functions.php and paste the following codes:
function __widgets_init() {
register_sidebar( array (
'name' => __('2nd Sidebar', 'textdomain'),
'id' => 'sidebar-secondary',
'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
'after_widget' => "</li>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
}
add_action( 'widgets_init', '__widgets_init' );
STEP II: Now, copy the sidebar.php and paste it and rename it into: sidebar-secondary.php. Now erase all the codes inside, and paste the following:
<aside id="sidebar-secondary-wrapper" role="complementary">
<?php if ( is_active_sidebar('sidebar-secondary') ) : ?>
<div id="secondary" class="widget-area">
<ul class="xoxo">
<?php dynamic_sidebar('sidebar-secondary'); ?>
</ul>
</div>
<?php endif; ?>
</aside>
STEP III: Get into the index.php or any other files you want. Paste the code get_sidebar( 'secondary' ); in the appropriate places in the get_sidebar(); or get_footer(); sections as follows::
<?php get_header();
// paste here: get_sidebar( 'secondary' );
...
...
or
<?php get_header();
...
...
// paste here: get_sidebar( 'secondary' );
get_sidebar();
get_footer();
?>
STEP IV:
Now your layout consists of two sidebars. Rest is up to you.
#media screen and (max-width: 1681px) {
#secondary-sidebar-wrapper {
display: none !important;
}
}

why my wordpress plugin is not showing sidebar?

i'm programming a plugin in php for wordpress for managing widgets, their places in a page and in which page they should appear. I'm having problems because i can't activate my sidebar and i dont know why. I post my code to see if something is not right and you can help me. this first file is the functions.php that, in theory, should register sidebars.
require_once('widgets.php');
function my_register_sidebars(){
// Register the 'primary' sidebar.
register_sidebar(
array(
'id' => 'primary',
'name' => __('Primary Sidebar'),
'description' => __('A short description of the sidebar.'),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
)
);
register_sidebar(
array(
'id' => 'adios',
'name' => __('adios'),
'description' => __('A short description of the sidebar.'),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
)
);
}
add_action('widgets_init', 'my_register_sidebars');
after a series of try-outs, I have also seen that add_action is not doing his work, do you have answers about it?
After that i instantiated a new file(sidebar.php) to recall the sidebar.
<?php
if (is_active_sidebar('primary')) : ?>
<aside id="secondary" class="sidebar widget-area" role="complementary" style="background-color = 'black';">
<?php dynamic_sidebar('primary'); ?>
</aside><!-- .sidebar .widget-area -->
<?php endif; ?>
And finally to call the sidebar i use get_sidebar();
but i cannot show the sidebar and it seems it is not even active.
do you have also ideas on how i can add widgets to this "activated" sidebar?
Thank you for your helps.
Use this code in your function.php
function my_custom_sidebar() {
register_sidebar(
array (
'name' => __( 'Custom', 'your-theme-domain' ),
'id' => 'custom-side-bar',
'description' => __( 'Custom Sidebar', 'your-theme-domain' ),
'before_widget' => '<div class="widget-content">',
'after_widget' => "</div>",
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
)
);} add_action( 'widgets_init', 'my_custom_sidebar' );
And put this code to your template file
<?php if ( is_active_sidebar( 'custom-side-bar' ) ) : ?>
<?php dynamic_sidebar( 'custom-side-bar' ); ?>
Try This code

Wordpress SideBar area not Appearing

I want to place an advert on the side bar, but the widget area appears in the wp-admin panel(appearance>widgets)
However, when I place my ad in the Side bar widget area, it does not appear on the site.
I used the code below to create the widget area and placed it in fuctions.php file of my theme. Where am I going wrong?
if (function_exists('register_sidebar')) {
register_sidebar(array(
'name' => 'Sidebar Widgets',
'id' => 'sidebar-widgets',
'description' => 'Widget Area',
'before_widget' => '<div id="one" class="two">',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
}
I'll be grateful.
Below is my page template.
<?php
/**
The template for displaying all pages.
*/
get_header();
?>
<div id="main" class="<?php echo $solid_content_class; ?> clearfix" role="main">
<?php
do_action('solid_before_content');
while ( have_posts() ) : the_post();
get_template_part( 'lib/content/content', 'page' );
endwhile; // end of the loop.
do_action('solid_after_content');
// If the theme supports comments in pages and comments are open or we have at least
one comment, load up the comment template
if( solid_theme_supports( 'comments', 'pages' ) && ( comments_open() || '0' !=
get_comments_number() ) ) comments_template( '', true );
?>
</div><!-- #main -->
<?php if( $solid_sidebar_location === 'left' || $solid_sidebar_location === 'right' )
{ ?>
<aside id="sidebar" class="sidebar <?php echo $solid_sidebar_class; ?>">
<div id="sidebar-main" class="sidebar">
<?php get_sidebar(); ?>
</div><!--sidebar-main-->
</aside>
<?php }
get_footer(); ?>'
function debatingday_widgets_init()
{
// Primary Widget area (left, fixed sidebar)
register_sidebar( array(
'name' => __( 'Primary Widget Area', 'debatingday' ),
'id' => 'primary-widget-area',
'description' => __( 'Here you can put one or two of your main widgets (like an intro text, your page navigation or some social site links) in your left sidebar. The sidebar is fixed, so the widgets content will always be visible, even when scrolling down the page.', 'debatingday' ),
'before_widget' => '<div class="s-widget" id="%1$s">',
'after_widget' => '</div>',
'before_title' => '<h5><i class="fa fa-folder-open color"></i> ',
'after_title' => '</h5>',
) );
// Secondary Widget area (right, additional sidebar)
register_sidebar( array(
'name' => __( 'Secondary Widget Area', 'debatingday' ),
'id' => 'secondary-widget-area',
'description' => __( 'Here you can put all the additional widgets for your right sidebar.', 'debatingday' ),
'before_widget' => '<li id="%1$s" class="widget-container %2$s">',
'after_widget' => '</li>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
}
/* Register sidebars by running debatingday_widgets_init() on the widgets_init hook. */
add_action( 'widgets_init', 'debatingday_widgets_init' );

Wordpress dynamic sidebar not work

Hi everyone i am developing a theme for wordpress i read a lot for dynamic sidebar but they aren't work my function code :
<?php
if ( function_exists('dynamic_sidebar') )
register_sidebar(array(
'before_widget' => '<div class="wcon">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>',
));
?>
and my sidebar.php code :
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
<?php endif; ?>
and i am geting sidebar.php using this code :
<?php get_sidebar(); ?>
It seems be fine but i can't add widget to it no link in wordpress panel and no direct access.
In your functions.php
add this below
//initialize addiional sidebars.
if(function_exists('register_sidebar')){
register_sidebar(
array(
'name' => 'second-sidebar' ,
'id' => 'second-sidebar',
'before_widget' => '<li class ="widget-container>"',
'after_widget' => '</li>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>'
)
);}
then where ever you want it to appear, add this
<?php dynamic_sidebar('second-sidebar'); ?>
Then go to your wordpress backend, in the widgets area, youll see a "second-sidebar" tab on the right hand side. Drop in your widgets and you should be good to go.
hope this helps
Try this instead:
functions.php
add_action( 'widgets_init', 'my_register_sidebars' );
function my_register_sidebars() {
register_sidebar(
array(
'id' => 'primary',
'name' => __( 'Primary' ),
'description' => __( 'Main Sidebar' ),
'before_widget' => '<div class="wcon">',
'after_widget' => '</div>',
'before_title' => '<h3>',
'after_title' => '</h3>'
)
);
}
sidebar.php
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
<?php dynamic_sidebar('primary'); ?>
<?php endif; ?>
Here is a simpler answer:
//sidebar php file
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar-name') ) : ?>
<p>You're not using a dynamic sidebar</p>
<?php endif; ?>
//functions.php
if ( function_exists('register_sidebar') )
register_sidebar(array(
'name'=>'sidebar-name',
'before_widget' => '<div class="your-class">',
'after_widget' => '</div>',
'before_title' => '<h2>', //h3, h4, h5, whatever size header you prefer
'after_title' => '</h2>',
));

Categories