WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Proper Way To Do A Child Theme Functions (8 posts)

  1. Andy Galaxy
    Member
    Posted 7 months ago #

    In the parent theme:

    function et_add_background_image(){
    	$bg = et_get_option( 'styleshop_bg_image' );
    	if ( '' == $bg ) $bg = get_template_directory_uri() . '/images/body-bg.jpg';

    In the Child theme I am using:

    if ( ! function_exists( 'et_add_background_image' ) ) {
    function et_add_background_image(){
    	$bg = et_get_option( 'styleshop_bg_image' );
    	if ( '' == $bg ) $bg = get_template_directory_uri() . '/images/new-body-bg';

    But get "Fatal error: Cannot redeclare et_add_background_image() (previously declared in..."

    What am I doing wrong there?

  2. paulwpxp
    Font hero
    Posted 7 months ago #

    Child's function loads before parent's.

    The !function_exists() if used, will be used in parent's only, to allow child function to override it.

  3. Andy Galaxy
    Member
    Posted 7 months ago #

    Sorry I don't really know how to override the function with the child. I put:

    function et_add_background_image(){
    	$bg = et_get_option( 'styleshop_bg_image' );
    	if ( '' == $bg ) $bg = get_template_directory_uri() . '/images/new-body-bg';
    }

    And get "Fatal error: Cannot redeclare et_add_background_image().."

  4. paulwpxp
    Font hero
    Posted 7 months ago #

    If a function in parent's is not wrapped in if not exists, see how that function get hooked in. Then in child theme, remove that action hook first and rehook it with child's version. There are more to it like priority order, and also some function is filterable.

    Review this article
    http://themeshaper.com/2009/05/25/action-hooks-wordpress-child-themes/

    Here are the add remove action codex
    http://codex.wordpress.org/Function_Reference/add_action
    http://codex.wordpress.org/Function_Reference/remove_action

  5. Andy Galaxy
    Member
    Posted 7 months ago #

    Thanks dude. I I'm trying to get my head round this. I've used:

    function unhook_original_functions() {
        remove_action('styleshop_bg_image','et_add_background_image');
    }
    add_action('init','unhook_original_functions');
    
    function my_add_background_image(){
    	$bg = et_get_option( 'styleshop_bg_image' );
    	if ( '' == $bg ) $bg = get_template_directory_uri() . '/images/new-body-bg.jpg';
    }

    and get the error:
    "Warning: Cannot modify header information - headers already sent by..."

  6. Andy Galaxy
    Member
    Posted 7 months ago #

    Actually the action hook was not 'styleshop_bg_image'. So actual function is now:

    function unhook_original_functions() {
        remove_action('wp_head','et_add_background_image');
    }
    add_action('init','unhook_original_functions');
    
    function my_add_background_image(){
    	$bg = et_get_option( 'styleshop_bg_image' );
    	if ( '' == $bg ) $bg = get_template_directory_uri() . '/images/new-body-bg.jpg';
    }

    but still same error.

  7. Andy Galaxy
    Member
    Posted 7 months ago #

    To be more precise. The error is
    "Warning: Cannot modify header information - headers already sent by (output started at /path/to/functions.php:17)..."

    Line 17 in parent functions.php is:
    require_once( $template_directory . '/epanel/custom_functions.php' );
    If it helps, I have not entered anything in the epanel custom background.

  8. Andy Galaxy
    Member
    Posted 7 months ago #

    well, after the last couple of hours searching for an answer I just deleted the ?> from the end of my functions.php and all good in da hood.
    Thanks for your help earlier Paul - it pointed me in the right direction - very helpful dude.
    Andy

Reply

You must log in to post.

About this Topic