Adding widgets to header in twenty eleven theme
-
Hi,
I’m a wordpress neophyte and have tried a number of different ways I found online to add a section at the top of my website https://proteus-commodities.com where I can place widgets. I am using a twenty-eleven child theme.
None of these methods has worked so far, so I was hoping someone would be able to walk me through the process, step-by-step. (Probably with properly creating function.php and header.php files). The end goal is to place the Transposh translation widget near the top of each page.
-
Here’s how I did a simple example using the code in the Codex page https://codex.www.ads-software.com/Widgetizing_Themes
1. Copy the code
<?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' ); ?>
into the file functions.php in the folder of your child theme. If you don’t have functions.php already in the folder with your child theme files, simply create an empty file and put the code above into it.
2. Change the line
'before_widget' => '<div>',
to
'before_widget' => '<div id="arphabet">',
[It doesn’t matter what you call the ID; you just need something to be able to refer to this area later.]3. Copy the header.php file from the twentyeleven folder to your child folder. Edit it to put the lines from the Codex article.
<?php if ( dynamic_sidebar('home_right_1') ) : else : ?> <?php endif; ?>
just before the line
<?php get_search_form(); ?>
4. Now edit style.css in your child theme to do something reasonable with the div you’re creating with the code above. Since you talked about putting it near the top of the page, I just used the CSS for the search form and modified it slightly:
#branding #arphabet { position: absolute; top: 3.8em; right: 30.6%; text-align: right; }
[The “right: 30.6%;” makes this box sit to the left of the search form.]
5. Finally go to your Admin panel > Appearance > Widgets. You should see a widget area over on the right labeled “Home right sidebar”. I dragged a text box widget over to that and added a header and a bit of text, and clicked Save.
Then the text I entered into the widget appeared to the left of the search form in the header.
Summary: So along with functions.php and header.php in your child theme folder, it’s likely you’ll need corresponding changes in style.css, and the code you put into functions.php will need to lay the groundwork for those style.css changes. I’m not the one to help you with fine tuning the CSS. If you do get it “almost” working you may want to open another incident and share a link to your site with a comment about what’s not working to get CSS suggestions. But I hope this at least gets you started with a new widget area at the top of your page.
Converting2WP — Thanks for this detail reply. You really went above and beyond what I would have expected on an open support forum.
I will try this out tomorrow morning and post my results.
Thank *you* for asking a specific question and giving some indication of how much detail you wanted. Fortunately the WordPress community is large enough that some of us have time to tackle questions like this. WordPress can seem to have a large number of moving parts, but I hope you do figure out how to leverage its flexibility.
Hi again, I have had other priorities at work and couldn’t get to this until today — sorry for the delayed reply!
I tried doing this step-for-step, but I think I may have gone awry somewhere. When I finished and went to the “Widgets” section of my admin page, I did not see the “Home right sidebar” area. Does this mean I did something wrong when putting together the functions.php folder?
My domain host is network solutions — is it simply possible that there is a delay in picking up the new .php files I created/uploaded?
The only thing I know that would affect picking up new PHP files would be caching plugins — and I don’t know if those affect the admin area. But if you have any, turn them off.
If you copied the code above, a widget area “Home right sidebar” should appear above the list of other widget areas (“Main sidebar”, “Showcase Sidebar”, “Footer Area One”, “Footer Area Two”, “Footer Area Three”).
Have you had success putting widgets into one of the other widget areas?
If you change that string ‘Home right sidebar’ to ‘JTL2744 Widget Area’, then the widget will be labeled with the new string — and may be easier to pick out (assuming that the code was copied and modified correctly).
Hi again, hope you had a good weekend.
In answer to your questions: I do not have any caching plugins installed. The widget area is not there. I currently have one widget functioning in the “Footer Area One” area (this is actually the widget I want to move to the header).
I know so little about FTP — would it make any difference at all if I created the file online (as text) as opposed to creating the file on my computer in TextEdit (plain text format)?
If it doesn’t make a difference then I must have copy/pasted/mistyped something incorrectly, right?
I really appreciate you spending the time to hold my hand on this.
Well, the file needs to be on the server in the wp-content/themes/<what.ever.you.called.your.child.theme> folder. The changes to the header.php and style.css files must be done there, too. If you are simply making the changes on your local machine, that explains why you’re not seeing any updates.
One way to get the file there is to create the file on your local machine and then use an FTP client (I like filezilla) and the login information from your hosting service to put it in the right place. Another way is to login to the server (using a “shell”) and edit the file onsite — but FTP is undoubtedly easier.
Once you’ve created a functions.php you can use the WordPress administration Appearance > Editor to change it. But I don’t see a way to *create* the file there.
Note that a syntax error in functions.php can take down your entire site (and you may not be able to get into the dashboard to correct the problem), so it’s good to know how to make the edits (and keep backups) on your local machine so you can delete a problematic functions.php and start over. [That may be why the “create a file” functionality has not been implemented.]
Check with your hosting service — some have “web based ftp” that you can find in the control panel, or do search for “ftp tutorial” for information on how to use Filezilla, FireFTP (firefox plugin) or other popular, free FTP tools.
[Mentioning your hosting service might turn up someone who can direct you on how to use their tools. Personally I use Dreamhost most often.]
Best wishes.
Oh I was probably unclear, my /themes/childoftwentyelevenfolder currently contains:
functions.php
header.php
style.cssSo I think they’re in the correct folder. What I meant was that I created one of the files (the header.php) online with my hosting service (NetworkSolutions) because it was uploading too slowly (despite being only 5700 bytes).
When I went in to look at my functions.php that I created, however, I got some sort of server error:
“500: Internal server error
This error is generated when a script running on the server could not be implemented or permissions are incorrectly assigned for files or directories
Troubleshooting suggestions:
Temporarily disable any rewrite rules by renaming your .htaccess file if it exists.
Ensure that any CGI or Perl scripts have at least .755. permissions.
If trying to run PHP and you get this error, you may have an invalid php.ini in your /cgi-bin, or may be missing your php.dat file in this folder.”
So maybe I copy-pasted correctly but network solutions isn’t applying the .php files I created?
When say you went to “look at your functions.php file” do you mean from the Admin Panel >Appearance > Editor ? Then the error is complaining about something in the wp-admin section, not the functions.php itself.
Do you get a different error when you go to the WordPress site itself (the front end)? Or does that seem to be working fine.
If you have shell access (or can see this through your FTP client) you might check to make sure the files you uploaded have the same owner and permissions as the files in the original TwentyEleven theme. If they do, you may have to contact Network Solutions for help on the Server Error (unless doing a fresh install sounds attractive).
[I’ll be offline for most of the rest of the week.]
I meant “look at my function.php file” via Network Solutions’ FTP file manager — I can open it on Network Solutions and edit it there.
This is a great point though — the edit section only contains style.css and rtl.css — that means that the files in themes/childoftwentyeleven on NetworkSolutions are not being applied, right?
Please follow up with me on this when you’re available next week ?? Thanks again for your time!
So it turns out that I was putting files in the wrong “theme folder” — I’d renamed (and was actively using) a different child theme (though still based on twentyeleven).
After some manipulation of style.css and realizing that my widget had to be set to .css format to get size correct, I have things the way I want them!
The company website is https://www.proteus-commodities.com (and the translation widget is right where I want it).
Thank you so much for your help converting2wp!
That’s happened to me — and to many others, I’m sure.
Glad to help!
Hi
I’ve been trying to do the exact same thing, and followed the above. The whole site came down, just appearing as a white screen. So I stepped back each bit to see what was causing it. I have a child theme and this is the content of my functions.php
<?php function my_header_image_height($height) { return 150; } add_filter( 'twentyeleven_header_image_height', 'my_header_image_height'); // It's a wide layout so make the content wider by 140px if ( ! isset( $content_width ) ) $content_width = 724; <?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' ); ?>
Converting2wp, you say that a syntax error would cause this problem. Can you see what the problem is above please?
Remove the second
<?php
.Found it! missing ?>
I now have a widget, thank you so much for going into detail on this.
- The topic ‘Adding widgets to header in twenty eleven theme’ is closed to new replies.