Custom sidebars disappearing with Wordpress 4.4 update - wordpress

Do you have any initial ideas as to why custom sidebars would stop displaying in a Wordpress theme after update 4.4? Our login/join page showed a custom sidebar with the login/join form on the right of the page and other up until the update. This is not a custom sidebar plugin, it's part of the theme. Any thoughts? MANY thanks!
The default sidebar can be enabled, but any custom created sidebars are rendered blank.
Code registering the sidebar within functions.php:
include('includes/sidebar/sidebar.php');
Code within sidebar.php:
include('functions/custom-sidebars.php');
Full code from custom-sidebars.php:
`//// GETS OUR CUSTOM SIDEBARS
$sidebars = get_option('dd_custom_sidebars');
//// IF THEY EXIST
if($sidebars) {
//// LOOPS AND REGISTERS THEM
foreach($sidebars as $sidebar) {
$args = array(
'name' => $sidebar.' — Custom',
'id' => us_get_sidebar_id($sidebar),
'description' => $sidebar.' — Custom Sidebar',
'before_widget' => '<div class="sidebar-item">',
'after_widget' => '</div>',
'before_title' => '<h4>',
'after_title' => '</h4>',
);
//// REGISTERS IT
register_sidebar($args);
}
}
/// FUNCTION TO GENERATE OUR FRIENDLY NAME
function us_get_sidebar_id($phrase) {
$result = strtolower($phrase);
$result = preg_replace("/[^a-z0-9\s-]/", "", $result);
$result = trim(preg_replace("/[\s-]+/", " ", $result));
$result = trim(substr($result, 0, 20));
$result = preg_replace("/\s/", "-", $result);
return $result;
}`

I had the same problem because my code that was creating custom sidebars was wrong in my sidebar.php file.
I was using this …
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Block 3') ) : ?>
Instead of this
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('block-3') ) : ?>
which is the id of the sidebar from my functions.php file

As a usable workaround (but not an answer to why the custom sidebar code is not working) I'm now dumping all the various widgets into the default sidebar which works and using the plugin Display Widgets to determine which page the widgets are shown on.

Related

How do I create a shortcode for a WordPress sidebar?

I have added a custom sidebar to my WordPress installation using the following script that also creates a shortcode:
// Register Sidebars
function custom_sidebars() {
$args = array(
'id' => 'gutenbar',
'class' => 'abeng',
'name' => __( 'Abeng sidebar', 'text_domain' ),
'description' => __( 'Sidebar for block', 'text_domain' ),
);
register_sidebar( $args );
}
add_action( 'widgets_init', 'custom_sidebars' );
add_shortcode( 'add_sidebar', 'custom_sidebars' );
When I add the [add_sidebar] shortcode to a Gutenberg block the sidebar does not appear. However, when I use a plugin called Sidebar Shortcode [sidebar id="gutenbar"] the sidebar is displayed perfectly. I assume that my shortcode is associated with a specific function and does not need a name or id for the sidebar to be chosen.
I had been thinking that because the sidebar was added using a plugin that allows the insertion of functions available site-wide rather than only in a theme using its functions.php, that was the reason the shortcode did not do its job. But since the sidebar shows in the widgets area and allowed me to add widgets, and now works with this plugin, that's obviously not the issue.
Looking under the hood of the concisely written Sidebar Shortcode, I can't see what I might want to add to make my code functional.
Why am I doing this? I'm using full-width pages for my posts, disabling the theme's default right sidebar (which doesn't play well in a responsive design) but still need to reinsert the sidebar in a column.
Thanks for any help.
With the help of Prashant Singh on the WordPress support forum, I learned that the function that creates the sidebar cannot be used to create a shortcode to place it on a page. The shortcode has to be created calling the WP native function dynamic_sidebar() that gives access to all sidebars (https://developer.wordpress.org/reference/functions/dynamic_sidebar/). Below is the full script that includes cleanup code to allow the correct positioning inside a page.
/**
* Create sidebar and shortcode
*/
// Register Sidebars
function custom_sidebars() {
$args = array(
'id' => 'gutenbar',
'class' => '',
'name' => __( 'Abeng sidebar', 'text_domain' ),
'description' => __( 'Sidebar for block', 'text_domain' ),
);
register_sidebar( $args );
}
add_action( 'widgets_init', 'custom_sidebars' );
/* Add the shortcode and allow positioning in page*/
add_shortcode( 'add_sidebar', 'my_custom_sidebar' );
function my_custom_sidebar(){
ob_start();
dynamic_sidebar( 'gutenbar');
$sidebar_left = ob_get_clean();
$html = ' <div class="sidebar-content"> ' . $sidebar_left . ' </div> ';
return $html;
}

Wordpress - display widget

I'm creating a custom theme and in my contact page i want to add a widget to display google maps map throught Wp Google Map plugin but i can't show the map in my page.
In my functions.php i have this:
function arphabet_widgets_init() {
register_sidebar(array(
'name' => 'My_Widgtet_Area',
'id' => 'map-area',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
));
}
In my html file:
<?php
$map = get_page_by_title( 'Mapa' );
echo $map->post_content;
?>
In my page content:
[wpgmza id="1"]
How can i display or associate the widget to my page?
Thank's
You can add the map directly using the given shortcode or you can add the shortcode in the widget and add the widget in page.
To add the map directly in the page using shortcode
echo do_shortcode('[wpgmza id="1"]');
To add widget in the page
dynamic_sidebar('map-area');
Hope it helps...
Please ask if you have any doubts.

What default function need to be include in new wordpress theme

I am creating a wordpress theme from scratch.
I created my own page.php, header.php , footer.php, sinngle.php .
When i install any 3rd party plugin , it does not work but works on any third party themes.
Do i need to include any predefined wordpress API to make it work ?
Yes, there are several functions you need to add in your theme to make fullfledged WordPress theme.
First read this thoroughly. https://codex.wordpress.org/Theme_Development
Some important points:
In header.php, there should be wp_head() function call just before </head>
In footer.php, there should be wp_footer() function call just before </body>
You need to use WordPress loop properly. https://codex.wordpress.org/The_Loop
If you want to register Navigation, Sidebar, etc then it should be done in functions.php
Display your widget in your theme pages like this:
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('header_sidebar') ) :
endif; ?>
You of course can replace the dynamic_sidebar above with the actual tag name of the 3rd-party widget you wish to integrate.
If you make your own widgets, register them in your functions.php
http://wpgyan.com/how-to-create-a-widget-area-in-wordpress-theme/
Example:
<?php
/**
* Register Widget Area.
*
*/
function wpgyan_widgets_init() {
register_sidebar( array(
'name' => 'Header Sidebar',
'id' => 'header_sidebar',
'before_widget' => '<div>',
'after_widget' => '</div>',
'before_title' => '<h2 class="rounded">',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'wpgyan_widgets_init' );
?>

activate plugins on custom theme

I'm new to Wordpress and just created my first template.
In the functions.php I have put the following code, that function what I understand should call the plugins from the plugins directory:
if (function_exists('register_sidebar')) {
register_sidebar(array(
'name' => 'Widgetized Area',
'id' => 'widgetized-area',
'description' => '',
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => ''
));
}
I do see all the plugins, and in the widget screen I also see the 'widgets' screen and it allows me to drag widgets into the working area.
The website itself displays the plugin's html but neither js nor css is working.
What am I missing?
Above code that you have added in your functions.php is to register sidebar not to call any plugin. It do not have any connection to plugin.
You can call sidebar you added in template as follow:
if ( is_active_sidebar( 'widgetized-area' ) ) {
dynamic_sidebar( 'widgetized-area' );
}

How to add a widget by its shortcode on Wordpress posts

Is there any way we can assign a shortcode to already created widget and later use that
shortcode in our specific posts and pages to show that widget instead of trivial method of
showing widgets in sidebar ? I googled about this stuff didn't find anything relative.
I haven't understood your question completely. But as far as what I have understood, may be the below solution help you:
This is the plugin to call widgets on pages/posts:
http://wordpress.org/plugins/widgets-on-pages/
Try the above mentioned plugin. It might help you.
Hope it helps you..!!!
add this function to fucntions.php of your theme
function widget($atts) {
global $wp_widget_factory;
extract(shortcode_atts(array(
'widget_name' => FALSE
), $atts));
$widget_name = wp_specialchars($widget_name);
if (!is_a($wp_widget_factory->widgets[$widget_name], 'WP_Widget')):
$wp_class = 'WP_Widget_'.ucwords(strtolower($class));
if (!is_a($wp_widget_factory->widgets[$wp_class], 'WP_Widget')):
return '<p>'.sprintf(__("%s: Widget class not found. Make sure this widget exists and the class name is correct"),'<strong>'.$class.'</strong>').'</p>';
else:
$class = $wp_class;
endif;
endif;
ob_start();
the_widget($widget_name, $instance, array('widget_id'=>'arbitrary-instance-'.$id,
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => ''
));
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_shortcode('widget','widget');
and use the short code in you post like this
[widget widget_name="Your_Custom_Widget"]

Resources