I’m trying to create two widget areas in my theme, they show up at the Widgets screen, I add widgets to the areas, save and everything looks fine. But if I leave Widgets screen and come back, the widget areas are empty, and the widgets I added before are now moved to Inactive Widgets, and of course there’s no output. I works fine with ClassicPress Theme, so I must have missed something somewhere… But I can’t figure out what or where!
Is there anything more I should include? I’ve compared the files with the ones in ClassicPress Theme, but can’t find anything that seems relevant. Still, I must have missed something, so any help would be much appreciated.
I don’t spot anything obviously wrong, but give this a try in your functions file:
remove (or comment out while trying this approach) the first line if(function_exists('register_sidebar')){ (this ensures backwards compatibility with earlier versions of WP, which really isn’t necessary)
replace that line with function cp_widgets_init() {
AFTER the closing brace, add this line: add_action('widgets_init', 'cp_widgets_init'); (this runs cp_widgets_init() on the widgets_init hook)
Thank you for answering, I tried your suggestion but that didn’d do the trick either.
Widgets aren’t really necessary to me right now, there are other ways to achieve what I want for the theme, I just find it hard to leave an unsolved problem.
I think your problem is not with the code you have provided here. Like Ray, apart from an unnecessary space in the first iteration of <div class=" widget-container, I don’t see anything wrong with your code (and that space wouldn’t cause your problem).
Are you getting any JavaScript errors in your browser console?
No, no errors exept for something about a logo image missing. That warning has been there from start, and hasn’t anything to do with this, I guess? The complete console message (if you can read out anything strange about it):
Thinking about it, viewing the page’s console wouldn’t help, as the problem is at the widget settings.
So, I added a widget to a widget area (drag and drop), and got this message:
It’s a parent, made by me. It uses Bootstrap, so there’s quite a lot of javascript. I’ll try to disconnect BS later on, to see if it make any difference.
But the theme doesn’t touch anything at in the admin area, so it seems odd that Bootstrap could hurt anything there.
I’ve made my own themes from scratch for ever (when was WP born?), so I’m not unfamiliar with that. I use to solve problems like this with pure stubborness (and a bit of searching), but now I’m stuck.
I doubt it’s caused by Bootstrap, because I’d expect that to show in the console. What else do you have in functions.php? I’d try commenting out the rest of that file so far as is possible, and seeing if it works then.
The usual things like enqueueing scripts, registering menu and such. Plus a few custom querys (that I will move to a plugin instead). But I’ll try that too, commenting out as much as possible later tonight.
Well, I have now tested with everything exept setup_theme and register_sidebar disabled in functions.php. Same behavior as before. I’ve also double (and triple) checked wp_footer is in the right place in the footer template, if it has any impact.
If anyone have any more suggestions for me to test, I will do that. Otherwise we leave it for now, I guess you have better and more important things to do. As I mentioned, it’s not a necessary thing right now.
This isn’t likely to be caused by anything on the front-end. My own inclination would be to swap out your functions.php file for one from a theme that works. (The simpler the file, the better.) Check that it does indeed work (although your sidebars will have the wrong names at this point) and then start modifying that. You can then check every step to see where the problem lies.
@Lena You could also try to set WP_DEBUG to true and WP_DEBUG_LOG to true and see if anything gets added to /wp-content/error.log (or /wp-content/debug.log).
Maybe a fatal error gets triggered behind the scenes, and this is the only way to see it.
Please correct me if I’m wrong, but if I haven’t misunderstood it completely, that format is required for translation, and since my themes are not intended to go anywhere, I’ve never used it.
I usually use the hook like you do, and did so this time too. But when it didn’t work, I copied how it was done in CPTheme, where the hook isn’t used.
Anyway, now I have turned debugging and log filing on, but I didn’t get any help from that. Next thing is to take a functions.php from a working theme, but I’m getting too sleepy now, and don’t really know what I’m doing anymore.
Tomorrow is another day. Thank you all for your ideas and suggestions so far!
I think that’s a bit over my head, I would need a detailed step-by-step explanation.
Today I first tested with Chrome browser as well, the problem’s still there.
Now I made a brand new theme, I thought starting from scratch and adding things one at a time must show me were I make the mistake.
But guess what? The files (index.php, style.css, functions.php) are nearly empty, and the problem is still there. I’m beginning to think I’m to old for this, who am I thinking I know anything about any code at all… Everyone of the pre-installed themes works as expected. None of mine.
<?php
function debugAddScripts(){
wp_enqueue_style('style', get_template_directory_uri().'/style.css');
}
add_action('wp_enqueue_scripts', 'debugAddScripts');
function debugSetup(){
add_theme_support('widgets');
}
add_action('after_setup_theme', 'debugSetup');
function debugWidgetsInit(){
register_sidebar(array('id' => 'mainSidebar', 'name' => 'Main Sidebar'));
}
add_action('widgets_init', 'debugWidgetsInit');
?>
With these files, not doing anything, it must be something I’m doing wrong. Probably a small thing I don’t see because I’ve been starring at i too long. And you all will have a good laugh (or shaking your heads and mumble something about old ladies should stick to their knitting).
You have a closing ?> tag at the end of at least one file. You need to remove all of them. I don’t know if that’s the cause of your problem, but I do know that including such closing tags causes lots of different issues.