WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Override twentyeleven functions.php using child theme (7 posts)

  1. raxdy
    Member
    Posted 2 years ago #

    Hi all,

    Please excuse my lack of knowledge when it comes to WordPress as I try to explain my issue:

    I'm creating a child theme using twentyeleven as the parent. The widget-title is wrapped within an <h3> tag. I want to override that <h3> tag with an <h5>. I see where it's called in the twentyeleven functions.php file, but how do I/and what is the proper procedure when trying to override that specific function?

    This is what I see in the twentyeleven functions.php file:

    function twentyeleven_widgets_init() {

    register_widget( 'Twenty_Eleven_Ephemera_Widget' );

    register_sidebar( array(
    'name' => __( 'Main Sidebar', 'twentyeleven' ),
    'id' => 'sidebar-1',
    'before_widget' => '<aside id="%1$s" class="widget %2$s">',
    'after_widget' => "</aside>",
    'before_title' => '<h3 class="widget-title">',
    'after_title' => '</h3>',
    ) );
    }

  2. wpismypuppet
    Member
    Posted 2 years ago #

    First, you'll want to read this:

    http://codex.wordpress.org/Child_Themes

    In there, there is a line that says

    TIP FOR THEME DEVELOPERS. The fact that a child theme’s functions.php is loaded first means that you can make the user functions of your theme pluggable —that is, replaceable by a child theme— by declaring them conditionally. E.g.:

    if (!function_exists('theme_special_nav')) {
        function theme_special_nav() {
            //  Do something.
        }
    }

    In that way, a child theme can replace a PHP function of the parent by simply declaring it again.

    Best of luck!

  3. raxdy
    Member
    Posted 2 years ago #

    Thanks wpismypuppet. I read the article, I'm just not sure how to alter the function as such...

    if (!function_exists('theme_special_nav')) {
        function theme_special_nav() {
            //  Do something.
        }
    }

    In my case, would that be:

    if (!function_exists('twentyeleven_widgets_init')) {
        function twentyeleven_widgets_init() {
            //  Do something.
        }
    }

    Thanks for your help thus far.

  4. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    Nah... you can't just override it, because it isn't pluggable (twentyeleven_widgets_init isn't wrapped in !function_exists)

    It's an action, so it needs removed first, then build your own (copy paste from 2011, rename, add your action

    remove_action( 'widgets_init', 'twentyeleven_widgets_init' );
    
    function voodoo_widgets_init() {
    
       sidebar codes copied and edited
    
    }
    add_action( 'widgets_init', 'voodoo_widgets_init' );

    Should do it - obviously this takes away ALL 2011 sidebars, so you'll wanna replace them all. It also deregisters the ephemera widget, if you use that, you'll wanna paste that line back into your custom widgets init

    And I usually do sidebar adding/removing within my main after_setup_theme function

    https://digitalraindrops.net/2010/08/widgetized-areas/
    Is an article I found that explains it pretty well

  5. wpismypuppet
    Member
    Posted 2 years ago #

    @Voodoo

    Good call on the not being pluggable piece. I'm surprised it isn't!!! WordPress developed the theme... you'd think they would use their own advice for development :)

    No harm, no foul... but a nice article! Thanks.

  6. raxdy
    Member
    Posted 2 years ago #

    @Voodoo

    Worked perfectly! Thanks very much.
    Similarly, did this:

    // !AFTER_SETUP_THEME
    add_action( 'after_setup_theme', 'raxdy_setup' );
    function raxdy_setup() {
    
    	/* Remove the twentyeleven registered sidebars */
    	remove_action( 'widgets_init', 'twentyeleven_widgets_init' );
    
    	function raxdy_widgets_init() {
                 register_sidebar (copied and edited)
            }
           add_action( 'widgets_init', 'raxdy_widgets_init' );
    }

    Kudos to you both, thanks @voodoo @wpismypuppet

  7. Rev. Voodoo
    Volunteer Moderator
    Posted 2 years ago #

    Coolio, glad that worked for you!

Topic Closed

This topic has been closed to new replies.

About this Topic