Support » Fixing WordPress » Problem redeclaring funtion in functions.php in a child theme

  • Resolved non_descript

    (@non_descript)


    Hi,
    I have been looking for a solution here but couldn’t find it. If I missed it I apologize and please redirect me to it.
    First let me say that I am new to WP and PHP so may be the question is stupid and the solution is simple but I can’t figure it out.
    Here is my problem:
    I am trying to create a child theme based on Twenty Eleven Theme. I need to change twentyeleven_continue_reading_link() function. I know that the most simple way is to change the function in the twentyeleven\functions.php but it’s not good solution since when twentyeleven is updated the changes will be lost. So I decided to try to overwrite it.
    Here is the child theme’s functions.php code

    <?php
     if (!function_exists('twentyeleven_continue_reading_link')){
    	function twentyeleven_continue_reading_link() {
    		return ' <a href="'. esc_url( get_permalink() ) . '">' . __( '<span class="meta-nav">>> </span> прочети', 'twentyeleven' ) . '</a>';
    	}
    }
    ?>

    When I run it I get the following error: ” Cannot redeclare twentyeleven_continue_reading_link() (previously declared in wp-content\themes\sharko\functions.php:12) in \wp-content\themes\twentyeleven\functions.php on line 328″
    I read that child theme’s functions.php is loaded first and this allows a function to be replaced. Can someone clarify that for me?
    Is it possible that the system checks if the twentyeleven_continue_reading_link() function exists and since it doesn’t it declares it but when functions.php from the parent theme is loaded it tries to declare it again and that’s why it gives the error?
    I am opened to any suggestions for a solution.

    Thank you!

Viewing 6 replies - 1 through 6 (of 6 total)
  • Is it possible that the system checks if the twentyeleven_continue_reading_link() function exists and since it doesn’t it declares it but when functions.php from the parent theme is loaded it tries to declare it again and that’s why it gives the error?

    That is correct. Twenty Eleven does not check for the existence of the function and tries to re-declare it.

    However, as far as I can tell, that function is only called by the twentyeleven_custom_excerpt_more() function which is added as a filter to the get_the_excerpt filter hook.

    From the TwentyEleven fuctions.php:

    * To override this link in a child theme, remove the filter and add your own
    * function tied to the get_the_excerpt filter hook.

    So you would remove the filter from get_the_excerpt and add your own which calls your own continue_reading_link function.

    Thread Starter non_descript

    (@non_descript)

    Thank you!
    I tried that but bumped into another problem. Now I don’t get the error but I got two “Continue reading ” links :). The first is as I want it and the second is the default. As if the old filter stays and both filters are called – first the one from the child theme then the default… Any help, plese!
    Here is the code:

    remove_filter( 'get_the_excerpt', 'twentyeleven_custom_excerpt_more' );
    function sharko_custom_excerpt_more( $output ) {
    	if ( has_excerpt() && ! is_attachment() ) {
    		$output .= sharko_continue_reading_link();
    	}
    	return $output;
    }
    add_filter( 'get_the_excerpt', 'sharko_custom_excerpt_more' );

    Probably the remove_filter didn’t work because the original filter is created in the parent theme’s functions.php which is loaded after the child theme?

    I wonder if it would work to put the remove_filter inside the function?

    Thread Starter non_descript

    (@non_descript)

    The most recent post now has the two links. The others are ok.

    OK. You will need to add an action to the ‘after_setup_theme’ hook.

    Try using this in your functions.php:

    function sharko_remove_excerpt_filter() {
       remove_filter( 'get_the_excerpt', 'twentyeleven_custom_excerpt_more' );
    }
    add_action( 'after_setup_theme', 'sharko_remove_excerpt_filter' );
    function sharko_custom_excerpt_more( $output ) {
    	if ( has_excerpt() && ! is_attachment() ) {
    		$output .= sharko_continue_reading_link();
    	}
    	return $output;
    }
    add_filter( 'get_the_excerpt', 'sharko_custom_excerpt_more' );
    Thread Starter non_descript

    (@non_descript)

    Thank You!!!
    That did it.
    I am posting the whole code for future reference – just in case.

    <?php
    /**
     * This file provides additional functions for sharko's theme.
     */
    
     /**
     * Returns a "Continue Reading" link in BG for excerpts
     */
    function sharko_continue_reading_link() {
    	return ' <a href="'. esc_url( get_permalink() ) . '">' . __( 'прочети <span class="meta-nav">&rarr;</span>', 'twentyeleven' ) . '</a>';
    }
    
    /*
     * Removes the get_the_exerpt filter
     */
    function sharko_remove_excerpt_filter() {
       remove_filter( 'get_the_excerpt', 'twentyeleven_custom_excerpt_more' );
    }
    add_action( 'after_setup_theme', 'sharko_remove_excerpt_filter' );
    
    /**
     * Adds a pretty "Continue Reading" link to custom post excerpts.
     *
     */
    
    function sharko_custom_excerpt_more( $output ) {
    	if ( has_excerpt() && ! is_attachment() ) {
    		$output .= sharko_continue_reading_link();
    	}
    	return $output;
    }
    add_filter( 'get_the_excerpt', 'sharko_custom_excerpt_more' );
    ?>

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Problem redeclaring funtion in functions.php in a child theme’ is closed to new replies.