Custom "sidebars" in WP - php

I am running a Avada theme together with WooCommerce on a wordpress site.
The issue is that I'm trying to get a search function on the top of the page, but only in responsive mode (on mobile devices). I've been able to add a third option for a sidebar in woocommerce. this shows up in the config (where to choose which widgets should go in). But I can NOT get it to show.
I know that you get the original sidebars like this (copied directly from original archive-product.php):
<?php
do_action( 'woocommerce_sidebar' );
?>
So I figured it should be possible to call for our own sidebar with the following line :
<?php
do_action( 'woo_sidebar-3' );
?>
This does not work though.
Since this has to be able to be shown only on responsive devices we used a custom css option of fusion to hide the other sidebars the following way :
#media only screen and (max-width: 720px) {
#sidebar {
display: none;
}
}
Which of course is too simple since it would block ALL sidebars. But that's something we could easily change. So far, the 3rd sidebar that we created does not show on responsive nor on desktop versions of the site. while the others do show on the desktop but not on the responsive (as desired).
EDIT-:
So i tried this with the following code :
<?php if ( !function_exists('dynamic_sidebar') ||
!dynamic_sidebar('woo_sidebar_3') ) { ?>
<p>Oops... this displays if the topbar is not set up</p>
<?php } ?>
It shows the error as given in the code, so it tries to pull the sidebar as we created. But it doesn't show. Is it because the following might not work correctly? :
'woo_sidebar_3' => array(
'label' => esc_html__( 'Global WooCommerce Product Sidebar 3', 'Avada' ),
'description' => esc_html__( 'Custom sidebar 3', 'Avada' ),
'id' => 'woo_sidebar_3',
'default' => 'None',
'type' => 'select',
'choices' => $sidebar_options,
'active_callback' => array( 'Avada_Options_Conditionals', 'is_woo' ),
Which was copied from the one right above that, which is called woo_sidebar_2?
EDIT 2 -:
Here is a JSfiddle with the complete sidebars.php code in it, and the dynamic_sidebar code (as HTML). the error I was pointing at is also visible when you run te script.

I'm not familiar enough with the Avada theme to know what some of those options mean and your block of code doesn't show the register_sidebar() call even though Avada must be using it.
To make things simpler, you could use standard WordPress code for registering a widget area
In your theme (ideally a child theme) add:
function so_38033924_widgets_init() {
register_sidebar( array(
'name' => __( 'Global WooCommerce Product Sidebar 3', 'your-theme' ),
'id' => 'woo_sidebar_3',
'description' => __( 'Custom sidebar 3', 'your-theme' ),
'before_widget' => '<div class="widget">',
'after_widget' => '</div>',
'before_title' => '<h2 class="widgettitle">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'so_38033924_widgets_init' );
You would need to make sure the before_widget, before_title, type of parameters match the parent theme so that your widget will get the same styling.
And then your template code looks right to me:
<?php if ( !function_exists('dynamic_sidebar') ||
!dynamic_sidebar('woo_sidebar_3') ) { ?>
<p>Oops... this displays if the topbar is not set up</p>
<?php } ?>

Related

Custom menus not showing in Menu Settings in WordPress

I'm a WordPress beginner working on a local server I set up with MAMP. I created the style, index, footer, header and functions docs, and PHP assembled everything with no problems. The website published all my files as expected. Then I tried to add custom menus to the appearance panel / menus in the admin dashboard, but they wouldn't show up.
Here's a snapshot of my folder hierarchy:
And here's a screenshot of the admin page:
And this is all the code in my functions.php file so far:
<?php
function macsc_script_enqueue() {
wp_enqueue_style('customstyle', get_template_directory_uri() . '/css/macsc.css', array(), '1.0.0', 'all');
wp_enqueue_script('customjs', get_template_directory_uri() . '/js/macsc.js', array(), '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'macsc_script_enqueue');
function register_my_menus() {
register_nav_menus(
array(
'header-menu' => __( 'Header Menu' ),
'extra-menu' => __( 'Extra Menu' )
)
);
}
add_action( 'init', 'register_my_menus' );
I've read several stack overflow threads and consulted the WordPress Codex support documentation. The register_my_menus function was a direct copy and paste from the WordPress doc. As far as I can tell, I'm doing everything correctly (obviously not, of course).
One thing that seems odd to me is that there were already tabs for "widgets," "menus" and "header" in the Appearance panel. Considering this is a custom theme that started with an empty folder, I'm not sure why those are there at all.
Any help would be greatly appreciated.
Typically menus are registered on the after_setup_theme hook. I'd try using that instead of init.
Also you don't need to register custom widgets or widget area support for the Widgets menu to show up, there's a lot that WordPress introduces in the core, regardless of your theme or active plugins.
And on second glance, it appears the TwentySeventeen theme is active, considering the Top Menu and Social Links Menu are the default menus in the TwentySeventeen (default) theme.
From TwentySeventeen/functions.php:
register_nav_menus( array(
'top' => __( 'Top Menu', 'twentyseventeen' ),
'social' => __( 'Social Links Menu', 'twentyseventeen' ),
) );
Go to Appearance > Themes and make sure your custom theme is activated.
I always register the menu in this way:
if ( !function_exists( 'theme_setup' ) ) {
function theme_setup() {
/*
* Some settings functions
*/
register_nav_menus(
array(
'header-menu' => __( 'Header Menu', 'domain' ),
'extra-menu' => __( 'Extra Menu', 'domain' )
)
);
}
}
add_action( 'after_setup_theme', 'theme_setup' );

Wrap new registered Wordpress Widget in a HTML Section

I would like to create a one-page wordpress website, where the navigation/menu points to different sections of the website. I would like to wrap my new Contact Form Widget in a section where the menu item "Contact" could point to. As seen in the code, I already have a 'before' div around the widget of "div class="contact-form-widget-wrapper". I can technically turn that into a section, however I would like to have more than one widget per section. Would anybody be able to help on how to wrap the widget/s in a section?
I'm using the Genesis Child Theme, which I'm trying to customize.
/* Contact Form Widget***************/
function contact_form_widget() {
register_sidebar( array(
'name' => __( 'Contact Form', 'genesis' ),
'id' => 'contactwidget',
'description' => __( 'ContactForm', 'genesis' ),
'before_widget' => '<div class="wrap contact-form-widget">',
'after_widget' => '</div>',
) );
}
add_action( 'widgets_init', 'contact_form_widget' );
//* Add the Contact Form Widget in place
function contact_form_widget_visible() {
genesis_widget_area ('contactwidget', array(
'before' => '<div class="contact-form-widget-wrapper">',
'after' => '</div>',));
}
add_action( 'genesis_before_footer', 'contact_form_widget_visible' );

How to rotate widgets in a sidebar

I'm creating a custom WordPress template which has 4 "sidebars" one for the header, a separate sidebar for the left and right columns, and then another sidebar for the footer, all that's fine and working properly. However, with my template, the header sidebar can only hold one widget at a time, so I was wondering if there was a way to add multiple widgets to the sidebar, but manipulate the code somehow to only display a random (or even in some specific order) widget at a time, which will change on each new page reload.
Code I used to make the sidebar:
if (function_exists('register_sidebar')) {
register_sidebar(array(
'name' => 'Header Sidebar Widgets',
'id' => 'sidebar-widgets-header',
'description' => 'The Header Can Only Support One (1) Widget.',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>'
));
}
And then the code I used to add the sidebar to my template:
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('sidebar-widgets-header') ) :
endif;
Here is an image of the sidebar within my admin currently:
(source: illstudios.com)
Quote One and Featured Videos are custom widgets I created. So is there a way to only choose one random widget from the selection and only display one widget at a time?
There's a filter that returns all widgets in all sidebars. We can shuffle and trim one of the sidebars with it:
if( !is_admin() )
{
add_filter( 'sidebars_widgets', 'sidebar_so_23691473' );
}
function sidebar_so_23691473( $sidebars_widgets )
{
shuffle( $sidebars_widgets['sidebar-widgets-header'] );
$only_one = array_slice( $sidebars_widgets['sidebar-widgets-header'] , -1 );
$sidebars_widgets['sidebar-widgets-header'] = $only_one;
return $sidebars_widgets;
}

how to use php to remove inline styling in wordpress

I'm building a site into which I'm incorporating a wordpress blog and during my customisation of a theme I've run into a slight snag.
I'm using a couple of widgets in the footer to display recent posts etc but they are displaying with their own inline styling. I can't find anywhere that may be adding the styling but I have found the function in the functions.php file that adds the id and class for the elements in question and I'm wondering whether I can add some php to the function in order to remove the styling that follows:
code is as follows:
function twentythirteen_widgets_init() {
register_sidebar( array(
'name' => __( 'Main Widget Area', 'twentythirteen' ),
'id' => 'sidebar-1',
'description' => __( 'Appears in the footer section of the site.', 'twentythirteen' ),
'before_widget' => '<aside id="%1$s" class="widget %2$s">',
'after_widget' => '</aside>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
) );
the on-page code is like this:
<aside id="recent-posts-2" class="widget widget_recent_entries masonry-brick" style="position: absolute; top: 0px; left: 0px;">
so i want to leave the id and class but just strip out the "style" elements
I know php may not be the best way to do this so i'm open to suggestions but if i can pop a preg replace or reg ex etc into the function that would be great.
Cheers guys
I had this problem earlier and as suggested in the comments by #Damien Pirsy, the issue was that the masonry jquery plugin was enqueued in the theme's functions.php file like so:
<?php
// Remove the following
if ( is_active_sidebar( 'sidebar-3' ) ) {
wp_enqueue_script( 'jquery-masonry' );
}
?>
This is a relic from the original WordPress twentyfouteen theme so make sure to test well after you remove it.

How to display secondary widget area for specific category in sidebar?

I am new to wordpress. I use the code below to display secondary widgets. This code shows all recent posts in the sidebar but I want a secondary widget for a specific category.
<?php dynamic_sidebar( 'secondary-widget-area' ); ?>
You have to register/declare your widgetized area before you can use it. This happens in the functions.php, so that your theme is aware of the widget area.
<?php
/**
* Register our sidebars and widgetized areas.
*
*/
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' );
?>
If you're just trying to add a secondary widget to an existing widgetized area, then no coding is needed. Just go to Appearance>Widgets and drag and drop HTML blocks or other custom widgets to the appropriate area.
More on Using Widgets
More on Widgetizing

Categories