Register additional widgets in child function.php

Well, I decided to be smart and learn about how to create child themes instead of continuing to hack at Edu Clean. :smiley:

For the most part, my baby steps have been wildly successful! But I'm having trouble with my child functions.php that I hope someone can shed some light on.

As I understand it, unlike my other child files, my child function.php will add to parent function.php rather than replace it (as with how a child footer.php would replace the parent footer.php).

My parent theme already has four widget areas. I want to add three more. I got it working when I placed my three new widget areas into parent function.php, and now I'm trying to move them into child function.php.

So far, I've tried doing this six different ways:

1. Placing just the code for my three new widgets into child function.php
2. Placing all of the original Register Widget code into child function.php
3. Place just the code for my three widgets, but wrapped in the same code that the original widgets are wrapped in:

function edus_widgets_init() {
global $bp_existed;
...
}
}
add_action( 'widgets_init', 'edus_widgets_init' );

And I tried each of these options wrapped in php tags, and without

All attempts give me an error: The code in child function.php appears in all its glory at the top of both my admin screen and my site. It ain't pretty.

I've read a few tutorials on working with child function.php, but have not found the guidance I need.

What am I doing wrong?

Thanks everybody!

  • katypurviance
    • Site Builder, Child of Zeus

    Thanks Phil.

    Right now my child function.php is blank so as to avoid the error, but pasted below is one example of what I tried, the original widgets plus my three new footer widgets, wrapped in php tags:

    [Let's see if this embed from pastebin works...]

    <script src="http://pastebin.com/embed_js.php?i=sXXkuh2b"></script>

  • katypurviance
    • Site Builder, Child of Zeus

    Nope. Okay, here's the link: http://pastebin.com/sXXkuh2b

    And here's the paste:

    <?php
    function edus_widgets_init() {
    global $bp_existed;
    register_sidebar(array(
    'name'=> __('Home Side Left', TEMPLATE_DOMAIN),
    'id' => __('home-side-left', TEMPLATE_DOMAIN),
    'description' => __('Home Side Left Widget', TEMPLATE_DOMAIN),
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget' => '',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
    ));

    register_sidebar(array(
    'name'=> __('Home Side Right', TEMPLATE_DOMAIN),
    'id' => __('home-side-right', TEMPLATE_DOMAIN),
    'description' => __('Home Side Right Widget', TEMPLATE_DOMAIN),
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget' => '',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
    ));

    register_sidebar(array(
    'name'=> __('Sidebar', TEMPLATE_DOMAIN),
    'id' => __('sidebar', TEMPLATE_DOMAIN),
    'description' => __('Sidebar Widget', TEMPLATE_DOMAIN),
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget' => '',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
    ));

    if($bp_existed == 'true') {
    register_sidebar(array(
    'name'=> __('Member Sidebar', TEMPLATE_DOMAIN),
    'id' => __('member-sidebar', TEMPLATE_DOMAIN),
    'description' => __('Member Sidebar Widget', TEMPLATE_DOMAIN),
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget' => '',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
    ));

    register_sidebar(array(
    'name'=> __('Footer Left', TEMPLATE_DOMAIN),
    'id' => __('footer-left', TEMPLATE_DOMAIN),
    'description' => __('Footer Left Widget', TEMPLATE_DOMAIN),
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget' => '',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
    ));

    register_sidebar(array(
    'name'=> __('Footer Center', TEMPLATE_DOMAIN),
    'id' => __('footer-center', TEMPLATE_DOMAIN),
    'description' => __('Footer Center Widget', TEMPLATE_DOMAIN),
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget' => '',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
    ));

    register_sidebar(array(
    'name'=> __('Footer Right', TEMPLATE_DOMAIN),
    'id' => __('footer-right', TEMPLATE_DOMAIN),
    'description' => __('Footer Right Widget', TEMPLATE_DOMAIN),
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget' => '',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
    ));

    }
    }
    add_action( 'widgets_init', 'edus_widgets_init' );
    ?>

  • katypurviance
    • Site Builder, Child of Zeus

    Hmmm...the original parent function.php had that extra curly bracket as well.

    But I tried taking it out.

    Now my admin screen and my site are gone -- white screen.
    I'll go into my host to empty that file again.

    What can I try next?

    Thank you!

  • katypurviance
    • Site Builder, Child of Zeus

    I changed:

    function edus_widgets_init() {

    to:

    function edus_child_widgets_init() {

    and:

    add_action( 'widgets_init', 'edus_widgets_init' );

    to:

    add_action( 'widgets_init', 'edus_child_widgets_init' );

    But I get that error. I'm missing something else. I just don't know what it is.

    (In the meantime, I've got "PHP for Dummies" on hold at the library. I also looked into a PHP class at the local college, but it's got two pre-requisites.)

  • katypurviance
    • Site Builder, Child of Zeus

    Yes.

    There are so many ghosts of Marketpress wrecking havoc that David has suggested that I export my site and reinstall it in a different database. So that's my big project for today. I've just been working on the structure of my site, so there's very little actual content to export. Just a whooooollle lot of settings to redo.

    After that, perhaps my child functions.php will work. I'll keep you posted!

  • katypurviance
    • Site Builder, Child of Zeus

    Okay, I've moved my test site to a new test folder. Fresh installs of everything. It even has that new site smell.

    But I'm still having the problem with my child function.php.

    I begin and end with php tags, I changed the name of the function in both instances, and I include the original widget areas along with my three new footer widgets. Here is the code I'm using for the child function.php:

    <php
    
    ////////////////////////////////////////////////////////////////////////////////
    // register widget
    ///////////////////////////////////////////////////////////////////////////////
    function edus_child_widgets_init() {
    global $bp_existed;
    register_sidebar(array(
    'name'=> __('Home Side Left', TEMPLATE_DOMAIN),
    'id' => __('home-side-left', TEMPLATE_DOMAIN),
    'description' => __('Home Side Left Widget', TEMPLATE_DOMAIN),
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget' => '</li>',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
    ));
    
    register_sidebar(array(
    'name'=> __('Home Side Right', TEMPLATE_DOMAIN),
    'id' => __('home-side-right', TEMPLATE_DOMAIN),
    'description' => __('Home Side Right Widget', TEMPLATE_DOMAIN),
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget' => '</li>',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
    ));
    
    register_sidebar(array(
    'name'=> __('Sidebar', TEMPLATE_DOMAIN),
    'id' => __('sidebar', TEMPLATE_DOMAIN),
    'description' => __('Sidebar Widget', TEMPLATE_DOMAIN),
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget' => '</li>',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
    ));
    
    if($bp_existed == 'true') {
    register_sidebar(array(
    'name'=> __('Member Sidebar', TEMPLATE_DOMAIN),
    'id' => __('member-sidebar', TEMPLATE_DOMAIN),
    'description' => __('Member Sidebar Widget', TEMPLATE_DOMAIN),
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget' => '</li>',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
    ));
    
    register_sidebar(array(
    'name'=> __('Footer Left', TEMPLATE_DOMAIN),
    'id' => __('footer-left', TEMPLATE_DOMAIN),
    'description' => __('Footer Left Widget', TEMPLATE_DOMAIN),
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget' => '</li>',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
    ));
    
    register_sidebar(array(
    'name'=> __('Footer Center', TEMPLATE_DOMAIN),
    'id' => __('footer-center', TEMPLATE_DOMAIN),
    'description' => __('Footer Center Widget', TEMPLATE_DOMAIN),
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget' => '</li>',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
    ));
    
    register_sidebar(array(
    'name'=> __('Footer Right', TEMPLATE_DOMAIN),
    'id' => __('footer-right', TEMPLATE_DOMAIN),
    'description' => __('Footer Right Widget', TEMPLATE_DOMAIN),
    'before_widget' => '<li id="%1$s" class="widget %2$s">',
    'after_widget' => '</li>',
    'before_title' => '<h3>',
    'after_title' => '</h3>',
    ));
    }
    }
    add_action( 'widgets_init', 'edus_child_widgets_init' );
    
    ?>

    What am I missing? The parent works perfectly. The child prepends my admin screen and the site with the code itself. And nobody likes that.

    Wait...does it have something to do with TEMPLATE_DOMAIN? Does it know to reach back to the parent? Do I need, I don't know, something like ../ before TEMPLATE_DOMAIN? I'm making wild guesses now.

  • DavidM
    • DEV MAN’s Mascot

    Hi Katy,

    Working with Phil on this as I believe he'll be out for the day, I notice you've got <php at the top of this particular snippet, whereas it should be <?php.

    I imagine that's why it's just outputting the bare code. Could you give that a go?

    -David

  • katypurviance
    • Site Builder, Child of Zeus

    Want to hear something really weird?

    So, I got it working with the code in the child function.php. Phenomenal!

    Then, I went back into the parent functions.php to remove another addition I had made:
    `remove_action( 'bp_init', 'bp_em_init' );'

    ...which I had put in to remove the Events link in the BP profile. All it did was take me to my error page. This was yet another casualty of my database problems, or at least I think so, since the Events BP link works perfectly in my new install.

    So I took it out...and White Screen of Death!

    I thought to myself, surely I didn't just break my site by removing this one line of code that wasn't even supposed to be there in the first place! But it was true! I tried undoing the other function work I had just done -- all through my host of course -- I blanked out the child function.php, put my new widgets back in parent function.php. The Total White Death continued. I couldn't even access Pages. It was that bad.

    I tried pasting in a fresh copy of function.php from a fresh download of Edu Clean. But all this freshity freshness amounted to nothing. Total Devastation.

    I still couldn't believe that this crazy piece of workaround BP code made my whole site die. But, I put it back in, and it works! It's like a sword you can't remove or else the patient will bleed to death.

    Meanwhile my new install's function.php works just fine without it, parent and child alike.

    So weird.

  • Philip John
    • DEV MAN’s Apprentice

    Hiya Katy,

    Glad you got the child functions sorted (many thanks David for chipping in!)

    It's likely that changing that line in itself didn't cause the white screen but prompted something else to happen.

    Your best best is simply to re-upload the original theme files (download them from here again if you need to) in order to revert any changes you've made. Uploading will simply overwrite the edits you made (maybe take a backup first).

    Thanks

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.