why my wordpress plugin is not showing sidebar? - php

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

Related

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;
}
}

How do i add a widget that will only appear on WooCommerce shop page

I want to add a widget that will only appear on the shop page, but I couldn't get any results.
functions.php
function kucuksun_widgets_init() {
register_sidebar(
array(
'name' => esc_html__( 'Sidebar', 'kucuksun' ),
'id' => 'sidebar-1',
'description' => esc_html__( 'Add widgets here.', 'kucuksun' ),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
),
register_sidebar(
array(
'name' => esc_html__( 'Shop', 'kucuksun' ),
'id' => 'shop',
'description' => esc_html__( 'Add widgets here.', 'kucuksun' ),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
)
)
);
}
add_action( 'widgets_init', 'kucuksun_widgets_init' );
Following my code: sidebar.php
<?php
if ( ! is_active_sidebar( 'sidebar-1' ) ) {
return;
}
?>
<aside id="secondary" class="widget-area">
<?php dynamic_sidebar( 'sidebar-1' ); ?>
</aside>
I'm not exactly sure but I need to print it in
woocommerce.php dynamic_sidebar( 'shop' );
To create a sidebar with widgets that will only appear on the shop page,
you can apply the following simple steps:
Create a new php file, name it sidebar-shop.php and place it in your themes root folder
Copy/paste the code from sidebar.php to sidebar-shop.php
Place a widget in the sidebar, and it should only be visible on the shop page
Explanation: page-{slug}.php — If no custom template has been assigned, WordPress looks for and uses a specialized template that contains the page’s slug.

How To Add a Class To a Custom Sidebar in WordPress

I just learned how to create a custom WordPress theme. I would like to assign a class to my sidebar for styling, but I have not found anything online that explains how to do this. The code I added to my functions.php file to register my sidebar is:
if ( function_exists('register_sidebar') )
register_sidebar(array(
'id' => 'sidebar-1',
'description' => __( 'Add widgets here to appear in your sidebar.', 'twentysixteen' ),
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
));
And the code I added to my sidebar.php file is:
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar() ) : ?>
<?php endif; ?>
Any help would be greatly appreciated!
Use before_widget and after_widget to add a wrapper.
register_sidebar( array(
'name' => esc_html__( 'Sidebar', 'twentysixteen' ),
'id' => 'sidebar-1',
'description' => 'Add widgets here to appear in your sidebar.',
'before_widget' => '<aside id="%1$s" class="widget custom-class %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h2 class="widget-title custom-class">',
'after_title' => '</h2>',
) );
Try this in functions.php file

register sidebar in wordpress theme

I have a html template and I have this code in my html and wants to convert this in wordpress
<div id="home">
<!-- Home Page -->
<p class="blue">NEED A DESIGNER?</p>
<p class="orange">I AM HERE</p>
<span><i class="fa fa-phone-square"></i> +1 234 567 876 54</span>
<!-- / Home Page -->
</div>
I want to call this as dynamic_sidebar(); ....
how i can register sidebar in functions.php file...
I tried as following
add_action( 'widgets_init', 'homepage_widget' );
function homepage_widget() {
register_sidebar( array(
'name' => __( 'main homepage', 'theme-slug' ),
'id' => 'homepagelol',
'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'theme-slug' ),
'class' => 'orange',
'before_widget' => '<div id="home">',
'after_widget' => '</div>',
'before_title' => '<p class="blue">',
'after_title' => '</div>',
) );
}
There are few steps you need to follow:
Step 1 : Register your sidebar
add_action( 'widgets_init', 'theme_slug_widgets_init' );
function theme_slug_widgets_init() {
register_sidebar( array(
'name' => __( 'Main Sidebar', 'theme-slug' ),
'id' => 'sidebar-1',
'description' => __( 'Widgets in this area will be shown on all posts and pages.', 'theme-slug' ),
'before_widget' => '<li id="%1$s" class="widget %2$s">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>',
) );
}
Step 2: In widget Area, you see your sidebar, then add text widget and put your html in text widget
Step 3: Then call your sidebar in index.php
<?php dynamic_sidebar('your-sidebar-unique-id'); ?>
Then save your page and reload..
Hope this will help you.
thanks
You can try this code to register sidebar, hope it helps.
if ( function_exists('register_sidebar') )
register_sidebar(array('name'=>'MiddleSidebar',
'before_widget' => '<li class="widget">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h3>',
));
register_sidebar(array('name'=>'FooterSidebar',
'before_widget' => '<li class="widget">',
'after_widget' => '</li>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h3>',
));

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