Wordpress custom theme: Widget area not configurable in Customizer - php

I am currently developing a custom wordpress theme for my own website to use. My main concern is to include widget areas. I managed to include a widget area so that it is configurable from Appearance > Widgets and the result also shows up in the customizer preview and on the live website.
The only problem is that I am not able to configure the widgets from within the customizer. When I click on Widgets in the customizer, it only shows this message:
Your theme has a widget area, but this particular page doesn’t display it. You can navigate to other pages on your site while using the Customizer to view and edit the widgets displayed on those pages.
Well, since the under Appearance > Widgets configured widget is actually showing, my site seems to have a widget area. But I guess something is missing for the customizer to know that.
Here is the code registering the sidebar / widget area that I took from many similar tutorials:
<?php
function tgf_customize_register( $wp_customize ) {
//All our sections, settings, and controls will be added here
}
add_action( 'customize_register', 'tgf_customize_register' );
function tgf_widgets_init() {
register_sidebar(array(
'name' => 'Sidebar Widget',
'id' => 'main_sidebar_widget',
'description' => 'Widget Area',
'before_widget' => '<div class=”widget”>',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>',
) );
}
add_action( 'widgets_init', 'tgf_widgets_init' );
?>
This is the code displaying the sidebar:
<?php if ( is_active_sidebar( 'main_sidebar_widget' ) ) : ?>
<?php dynamic_sidebar( 'main_sidebar_widget' ); ?>
<?php endif; ?>
I also tried showing it with this code, but then the widget doesn't show on the site at all, even when I configure it from Appearance > Widgets:
<?php if ( is_active_sidebar( 'main_sidebar_widget' ) ) : ?>
<?php get_sidebar( 'main_sidebar_widget' ); ?>
<?php endif; ?>
Why can't I configure this widget from within the customizer?
This question WordPress theme creation : There are no widget areas on the page shown mentions the same error message in the customizer, but does not provide a solution that applies to my specific problem.
Since I couldn't find anything by researching this problem, I think I am missing something fundamental. Which could very well be, since I'm new to wordpress theme development and PHP. Thanks in advance!

Found out myself what none of the tutorials told me:
I have to add <?php wp_head() ?> at the start of the page and <?php wp_footer() ?> at the end of the page.
Wordpress seems to need these hooks in order to function properly.

Related

Adding search bar/form to a custom HTML navigation in Wordpress using get_search_form() function

My problem is that I built the navigation menu inside the 'top' section in the widgets area in WordPress (which is perhaps a mistake, as I can't add php in there).
Assume my menu is built this way:
<nav>
<div class="title-and-logo">...</div>
<div class="menu-links">...</div>
<div class="search-bar"> </div>
</nav>
The relevant css:
nav{
display: flex;
}
I need to add the search bar into the .search-bar div. My solution is to move the whole nav menu out of widgets area and into functions.php. (I am not very comfortable with PHP yet, that is why I am reluctant to mess around with it).
My question: is there a way to add the search bar generated by get_search_form() into the section with class="search-bar" without needing to remove my code somewhere that allows PHP?
Or: can I generate a normal search bar using HTML without needing to use the get_search_form() provided by WordPress? (but maybe the search function of WordPress is better?).
Any help is appreciated. You can suggest javascript and jquery fixes too.
You could create a custom widget area in your functions.php for your top nav like so:
<?php
/**
* Register our sidebars and widgetized areas.
*
*/
function my_custom_widget_area_init() {
register_sidebar( array(
'name' => 'Top Nav - Search',
'id' => 'top_nav_search',
'before_widget' => '',
'after_widget' => '',
'before_title' => '',
'after_title' => '',
) );
}
add_action( 'widgets_init', 'my_custom_widget_area_init' );
?>
Then you adjust your theme to display the widget:
<nav>
<div class="title-and-logo">...</div>
<div class="menu-links">...</div>
<?php if ( is_active_sidebar( 'top_nav_search' ) ) : ?>
<div class="search-bar"><?php dynamic_sidebar( 'top_nav_search' ); ?></div>
<?php endif; ?>
</nav>
Okay so I had a very specific problem which I doubt anyone else would have, but anyway, here is how I solved it:
I already had a plugin installed called 'widget shortcode' which transforms any widget I have into a shortcode that can be used inside text areas on the website.
A bit irrelevant but recall that I also had my navigation placed inside an HTML widget (but I was not using it as a shortcode, it was placed directly into the 'top widget area'.)
So all I had to do was get the shortcode of the search widget (as we have a search widget by default on Wordpress) and insert it inside the HTML of my Nav.
Problem: turns out we can't put shortcodes by default inside html widgets. Solution: place one line code in functions.php that would allow us to do so. Problem solved.

Wordpress custom sidebar template not active

I am working on a wordpress site (my first), where I want to use a specific sidebar for a specific page template. The sidebar is not the regular widget area, but a template (sidebar-2.php) that calls another template (boeking.php) of a form.
I used the default page.php and sidebar.php as a starting point, copied almost everything, and thus created a template for the page (reisbeschrijving.php) that calls sidebar-2 by using get_sidebar('2'). I also created a template sidebar-2.php:
It worked at first, but while I was working on another part of the website it stopped working all of a sudden. The page template works fine, but it doesn't load the sidebar-2, because the is_active_sidebar() returns false. However, I don't know why it returns false. When I call get_sidebar() (default) in reisbeschrijving.php, it does display sidebar.php, and is_active_sidebar() returns true. If I remove the if is_active_sidebar() clause, sidebar-2 displays just fine. both sidebar.php and sidebar-2.php are in the same folder (of the child theme).
Did I miss something? Should I register the new sidebar-2 template somewhere in the header or something? I know that this is the case for the custom dynamic sidebars with widget area's, but this is an altogether new template and I thought I wouldn't have to (I tried anyway, but to no avail).
Any hints on why is_active_sidebar returns 'false' on my second sidebar is greatly appreciated.
1st Register sidebar - functions.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' );
Put this code in template
if ( is_active_sidebar( 'custom-side-bar' ) ) :
dynamic_sidebar( 'custom-side-bar' );
endif;
This will work 100%

Menu is not showing in admin panel

I try to make my own theme in wordpress. Stucked a problem: i can't see menu button in admin panel. I've registered menu in functions.php of my theme
<?php
register_nav_menu( 'menu', 'Menu on the main page' );
?>
But there is no menu button appear in admin panel - appereance. I did all that was recommended on youtube video (https://youtu.be/I0zu6Dc3JDI?t=95). Nothing. Please help =) I have last version of Wordpress.
You was half way there, you had the right idea for registering the menu however you missed out telling WordPress to run your register menu command.
What you need to do is wrap it in a function and call the function on 'init' which is the initialisation of the admin area (one of many WordPress hooks), see below:
function register_child_menus() {
register_nav_menus(
array(
'my-menu' => __( 'My Menu' )
)
);
} add_action( 'init', 'register_child_menus' );

Wordpress Shortcodes not rendering content (2 themes)

I've been having this problem for several weeks now, and would really like to get it resolved. I'm seeing issues like this all over the internet, however everyone is coming up empty.
I have 2 custom wordpress themes. One on a Linux (cPanel) Server, and one on a Windows (IIS) Server. Both of them have different custom themes, both of them are loaded with content. The two server php.ini's are identical (or as close as they can be). When I use the provided wordpress theme shortcodes are working fine. However when I take either of the custom themes, and make them active, shortcodes cease to work, they will show up on sidebars, and posts, but there is NO content, and by no content I mean: It shows the title of the widget, but not the actual widget itself.
I am quite literally at my wits-end here. This will be the 4th forum post in over a month on different sites, trying to get an answer. No one can help me anywhere. I'm hoping since I've had luck here with other issues, that maybe someone has seen this happen. Even custom shortcodes are not rendering.
I thought maybe it was because a script isn't called in the custom theme, but comparing the Wordpress TwentyTwelve theme and mine there are not any differences in the header file.
I've called wp_head(); and wp_meta();....So confused. Please help!!!
functions.php
<?php
function wpb_widgets_init() {
register_sidebar( array(
'name' => __( 'Main Sidebar', 'wpb' ),
'id' => 'sidebar-1',
'description' => __( 'The main sidebar appears on the right on each page except the front page template', 'wpb' ),
'before_widget' => '',
'after_widget' => '</div></div>',
'before_title' => '<div class="padding"><div class="top">',
'after_title' => '</div><div class="bottom">'));
}
add_action( 'widgets_init', 'wpb_widgets_init' );
apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file ))
?>
sidebar.php
<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?>
<?php dynamic_sidebar( 'sidebar-1' ); ?>
<?php endif; ?>
Without code this is difficult to answer, but a good shot might be the use of get_the_content() instead of the_content() within your loop. Using get_the_content() will not apply the the_content filters. See:
If you use plugins that filter content (add_filter('the_content')), then this will not apply >the filters, unless you call it this way (using apply_filters):
<?php apply_filters('the_content',get_the_content( $more_link_text, $stripteaser, $more_file )) ?>
http://codex.wordpress.org/Function_Reference/get_the_content

Customize my wordpress plugin in home page

I am using this plugin http://wordpress.org/extend/plugins/simple-rss-feeds-widget/
This works fine for me. i am using WordPress 3.3.1.
Now i want my plugin to be there in the centre of my home page and not in the side widget.
How can i achieve this.?
I mean, my plugin is there in the widget list,and i can drag my plugin to the sidebar of my template in back-end
and it reflects in the front-end at right corner.
How can i get my plugin in home page at centre of the page?
Hope someone wil help me.
In other words, how can i call my customized plugin in my template file?
Thanks
Haan
You need to register a new widget position in your functions.php file and then add the code to render out the contents of that widget in the relevant template file.
So as an example. In functions.php you would find the section fo code that is registering your existing widget positions (look for if (function_exists('register_sidebar')) { ) and register a new position below it;
register_sidebar(array(
'name' => 'Home Widget Container',
'id' => 'home-widget',
'description' => 'These are widgets for the home position.',
'before_widget' => '',
'after_widget' => '',
'before_title' => '<div class="home-widget-header"><h2>',
'after_title' => '</h2></div>'
));
And then in the template file you are using for your homepage;
<?php
if (!function_exists('dynamic_sidebar') || !dynamic_sidebar('Home Widget Container')) :
// This will include your widgets.
endif;
?>

Categories