I've created a custom widget area in the plugin I'm creating.
function arphabet_widgets_init() {
register_sidebar( array(
'name' => 'Home right sidebar',
'id' => 'home_right_1',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'arphabet_widgets_init' );
dynamic_sidebar( 'home_right_1' );
And to enable showing the widgets stored in this widget I have inserted this code inside the footer.php of the theme I'm currently using for testing.
<?php if ( is_active_sidebar( 'home_right_1' ) ) : ?>
<div id="primary-sidebar" class="primary-sidebar widget-
area" role="complementary">
<?php dynamic_sidebar( 'home_right_1' ); ?>
</div><!-- #primary-sidebar -->
<?php endif; ?>
It works, when I add widgets to my widget area, they are shown in footer of the website. But I want to make the plugin works without changing the footer.php manually. How can I check which theme is activated and then find that theme, the footer.php file and add that code exacltly where I want (to be shown below any other footer created with the theme when is installed. Also, I would like to remove the footer that was generated before.
Related
I can't create left sidebar.
I created a file into child theme folder: sidebar-left.php:
<?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly ?>
<aside class="sidebar">
<!-- SIDEBAR WIDGET AREA -->
<?php if ( is_active_sidebar( 'lhsidebar' ) ) : ?>
<?php dynamic_sidebar( 'lhsidebar' ); ?>
<?php else : ?>
<p><?php esc_html_e('No widgets added', 'rehub-theme'); ?></p>
<?php endif; ?>
</aside>
Then, I have added this code into child theme's function.php:
// Left SideBar
register_sidebar( array(
'name' => esc_html__( 'Left Sidebar', 'rehub' ),
'id' => 'lhsidebar',
'description' => esc_html__( 'Add widgets here.', 'rehub' ),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
) );
But where should I put the get_sidebar('sidebar-left')?
You have to call the sidebar inside any template of your child theme either by using the get_sidebar() function:
<?php get_sidebar('left'); ?>
Note: the name of the file without sidebar- part at the begin of the name as mentionned in the doc.
Either by using the get_template_part() function if your template file is not in the same directory as the sidebar file.
<?php get_template_part( 'sidebar-templates/sidebar', 'left' ); ?>
Note: here the full name of the sidebar is mentionned in the function, as well as its path from the root of the child-theme, cf doc.
i am working on a simple WordPress blog and i am trying to display a widget that appears in every page in the website .. i have followed the dynamic sidebar widget approach, but the problem ,
here is the Function.php file
function wpb_widgets_init() {
register_sidebar( array(
'name' => 'Custom Header Widget',
'id' => 'custom-header-widget',
'before_widget' => '<div class="chw-widget">',
'after_widget' => '</div>',
'before_title' => '<h2 class="chw-title">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'wpb_widgets_init' );
and my header.php file
<?php if ( is_active_sidebar( 'custom-header-widget' ) ) : ?>
<div id="header-widget-area" class="chw-widget-area widget-area" role="complementary">
<?php dynamic_sidebar( 'custom-header-widget' ); ?>
</div>
<?php endif; ?>
and this works fine only in any single post.
but in the home page/or any template file nothing shows !!!
Any ideas ?
You can try this structure and make sure your sidebar active
I have figured it out
i hade to create a template file for my new sideBar called: sidebar-.php
and use the get_sidebar(id) method to call it
Voilet it works in all templates ...
I have a calendar plugin I wish to use, I tested it first on the WP '17 default theme and it displays just fine. I have my own custom theme I'm building using w3.css, very basic stuff.
I created a display area for widgets using
<?php
/**
* Register our sidebars and widgetized areas.
*
*/
function arphabet_widgets_init() {
register_sidebar( array(
'name' => 'Home right sidebar',
'id' => 'home_right_1',
'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', 'arphabet_widgets_init' );
add_theme_support( 'customize-selective-refresh-widgets' );
wp_enqueue_script("jquery");
?>
And put the following in an appropriate div within my index.php:
<?php dynamic_sidebar( 'home_right_1' ); ?>
As far as I can tell, when my "theme" outputs the widget there is no stylesheet attached to it. It's a calendar widget and displays the entire list of days of the month in a long list, rather than an enclosed area.
Any suggestions would be appreciated.
Okay, I hadn't called the footer which is apparently where the function hooks to. Now it's working as expected.
<?php get_footer(); ?>
(Placed just before </body> in the index file.)
I also ended up using the following to call it:
<?php if ( is_active_sidebar( 'home_right_1' ) ) : ?>
<div id="primary-sidebar" class="primary-sidebar widget-area" role="complementary">
<?php dynamic_sidebar( 'home_right_1' ); ?>
</div><!-- #primary-sidebar -->
<?php endif; ?>
my footer shows in the normal page fine. I created a new post template and have this in the footer:
<?php if ( !function_exists('dynamic_sidebar') || dynamic_sidebar('sidebar-2'))
get_footer();
Like this my widgets show fine. In my functions file I have this:
add_action( 'widgets_init', 'child_register_sidebar' );
function child_register_sidebar(){ register_sidebar(array(
'name' => 'Sidebar 2 for single_withnotes',
'id' => 'sidebar-2',
'description' => '',
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<span class="widget-title">',
'after_title' => '</span>',
));
}
and my new sidebar look like this:
if ( ! is_active_sidebar( 'sidebar-2' ) ) {
return;
}
?>
<div id="secondary" class="widget-area" role="complementary">
<?php dynamic_sidebar( 'sidebar-2' ); ?>
</div><!-- #secondary -->
Can anyone suggest why this is? Thanks.
In your code, your footer only shows when the "if sentence" is right. dynamic_sidebar("sidebar-2") only returns true when widget "sidebar-2" exists and it has content.
Make sure you have added content to the sidebar-2 in Appearance> Widgets.
Also you should use "is_active_sidebar" in an "if sentence", because dynamic_sidebar sends content to output buffer.
If you have content in "Sidebar 2...", other possibility is that there are an error in some of the widgets linked to that position.
Put at the begining of your file:
error_reporting(E_ALL);
ini_set('display_errors', 1);
And watch if some errors are showned in the page.
Also you can remove all content from Sidebar 2 and tried only with some text.
I followed the instructions here for how to add a widget to a template. I added this code to functions.php:
function arphabet_widgets_init() {
register_sidebar( array(
'name' => 'Carousel',
'id' => 'carousel_widget',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'arphabet_widgets_init' );
and I added this to my custom template:
<?php if ( is_active_sidebar( 'carousel_widget' ) ) : ?>
<div id="carousel-widget" class="carousel widget-area">
<?php dynamic_sidebar( 'carousel_widget' ); ?>
</div>
<?php endif; ?>
It worked, but my new widget showed up as a sidebar, and I want it to be in the main body of the page. Obviously this is because of the dynamic_sidebar function, but the documentation is not very helpful on how to place a widget on the page NOT as a sidebar.
I tried replaced the php with this:
<div id="carousel-widget" class="carousel widget-area">
<?php the_widget( 'carousel_widget' ); ?>
</div>
But now it doesn't show up at all.
How do I place this widget on my page without it showing up as an absolutely positioned sidebar?