Support » Themes and Templates » can i create child functions.php file for twentyten?

  • Resolved goldmember

    (@goldmember)



    i’m doing everything i can to employ child files for the twentyten theme files so that when the theme updates i wont have issues.

    but i want to make some modifications to the functions.php file. when i add a functions.php file to my child folder, the site no longer works.

    what’s up with that???

Viewing 15 replies - 1 through 15 (of 25 total)
  • I think it depends what you have in your functions.php file

    Use http://www.pastebin.com put your child theme functions.php code in a post,and add the link here.

    Then people can help!

    David

    thanks. i dont even remember what i was trying to do so when i’ll post the code in a new thread when it re-emerges.

    i remember now. so i want to edit the functions.php file so that the my single postings (click here for example) say “Posted on September 7, 2010” and I remove the rest, “by Admin”.

    i do see where that functionality resides (line 442) in my functions.php file: http://pastebin.com/vRmeZM64

    but if i’m building a child theme of Twenty Ten, should I be editing the functions.php file that’s in the parent folder? Because when I add a functions.php file to my child folder, the site doesn’t work.

    please advise. thanks in advance.

    f i’m building a child theme of Twenty Ten, should I be editing the functions.php file that’s in the parent folder?

    No. You build the new function in the child’s functions.php. If the site then falls over, there must be an error in your child’s function code.

    here is the specific error message I get when I upload functions.php to my child theme folder:

    “Fatal error: Cannot redeclare twentyten_page_menu_args() (previously declared in /home/content/p/o/o/poolteesguy/html/green/wordpress/wp-content/themes/twentytenchild/functions.php:214) in /home/content/p/o/o/poolteesguy/html/green/wordpress/wp-content/themes/twentyten/functions.php on line 214”

    so what is it about line 214 (http://pastebin.com/vRmeZM64) that’s causing the error?

    fyi, php is definitely not my strong point so sorry if i’m missing something obvious here.

    When you overide calls in the parent functions you call them inside a function, this is actioned ‘after_setup_theme’.

    Here is a simple example there are a few examples out there, have a Google and read a few posts.

    Example adding a second menu location.

    <?php
    
    /** Tell WordPress to run child_theme_setup()
    when the 'after_setup_theme' hook is run.
    */
    add_action( 'after_setup_theme', 'child_theme_setup' );
    
    /** This function will hold our new calls and over-rides */
    if ( !function_exists( 'child_theme_setup' ) ):
    function child_theme_setup() {
    
        /*
        We want a Second Navigation Bar right at the top
        This theme uses wp_nav_menu() in two locations.
        */
        register_nav_menus( array(
            'secondary' => __( 'Top Navigation', 'twentyten' ),
        ) );
    }
    endif;

    There are new functions like remove_filter and remove_action you may need to use, here is another example from Arron Jorbin that may help.

    HTH

    David

    thanks. so i’ve been reading up and i’m starting to get my head around this stuff (kind of).

    My functions.php code in my child theme is below but for some reason I’m getting an error message that says “Parse error: syntax error, unexpected T_ENDIF in /home/content/p/o/o/poolteesguy/html/green/wordpress/wp-content/themes/twentytenchild/functions.php on line 48”

    <?php
    
    /** Tell WordPress to run child_theme_setup()
    when the 'after_setup_theme' hook is run.
    */
    add_action( 'after_setup_theme', 'child_theme_setup' );
    
    /** This function will hold our new calls and over-rides */
    if ( !function_exists( 'child_theme_setup' ) ):
    function child_theme_setup() {
    
        /*
        I want the Posted On date to show, but not the author.
        */
    
    function twentyten_posted_on() {
    
            printf( __( '<span class="%1$s">Posted on</span> %2$s', 'twentyten' ),
    
                    'meta-prep meta-prep-author',
    
                    sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
    
                            get_permalink(),
    
                            esc_attr( get_the_time() ),
    
                            get_the_date()
    
                    ),
    
                    sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
    
                            get_author_posts_url( get_the_author_meta( 'ID' ) ),
    
                            sprintf( esc_attr__( 'View all posts by %s', 'twentyten' ), get_the_author() ),
    
                            get_the_author()
    
                    )
    
            );
    
    }
    endif; ?>

    I’m not sure what I did wrong here, or am missing. please advise. thanks!

    Look to use a colored syntax text editor, notepad++ is a free one, these will highlight mistakes like this.

    Missing a closing brace ‘}’

    }
    
    }
    endif;

    HTH

    David

    thanks, i now have this at the bottom of my functions.php file

    get_the_author()
    
                    )
    
            );
    }
    
    }
    endif;

    but now i get the error: “Parse error: syntax error, unexpected ‘}’ in /home/content/p/o/o/html/green/wordpress/wp-content/themes/twentytenchild/functions.php on line 48”

    FYI, I’m a php newbie trying to re-create a functions.php file that works properly. i put the code into notepad++ but i’m not sure what i’m looking at or what to look for. please advise. thanks!

    Not sure what is happening but you have 45 lines in the code you pasted above, but the error is saying line 48, so we are not getting the full text or picture.

    Can you use pastebin for your file code so we can download it instead of copying, also did you change and damage the parents files, if so download twenty ten and overwrite them all and start fresh.

    Remove the innner function of your childs functions.php to see if that is the problem, taking little steps are a good way to learn, less damage when you fall, and soonest mended!

    Regards

    David

    here’s the pastebin of the current code I have: http://pastebin.com/NjCJnDSC

    when i take out the inner function, there’s still errors.

    thoughts???

    Ok here’s the scenario, you need to declare the function before the parent does so all you need in your child functions.php is:

    /* Test Override */
    function twentyten_posted_on() {
    	printf( __( '<span class="%1$s">Posted on</span> %2$s <span class="meta-sep"></span> %3$s', 'twentyten' ),
    		'meta-prep meta-prep-author',
    		sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
    			get_permalink(),
    			esc_attr( get_the_time() ),
    			get_the_date()
    		),
    		sprintf('')
    	);
    }

    I have just removed ‘by’ and the admin links, not played with to much, it has been tested and works!

    Quick Analysis:
    The ‘after_setup_theme’ function is where you would unload an action, filter or change parameters etc:

    In the parents functions.php there is a line:
    if ( ! function_exists( 'twentyten_posted_on' ) ) :
    This tells wordpress to only load the function if it has not already been loaded, as the child themes functions.php is run first our function is already loaded.

    So the sequence is:
    1. child themes functions.php
    2. parent themes functions.php (any conditional functions already loaded then skip em!)
    3. back to the child themes function ‘after_setup_theme’ run this to unload any parent actions or functions, declare any new variables etc:

    Phew!

    HTH

    David

    BTW: line 16 of your “pastebin” code has a closing brace } this is not in the code above, and that would be a problem!

    i kind of get the sequence but i’m not sure i understand yet how to put the pieces of the file together in the proper context.

    i used ONLY the code that you posted above in a new version of functions.php, uploaded and that didnt work. Am I supposed to replace just lines 18 to 48 of the pastebin file (http://pastebin.com/NjCJnDSC) with that code? And leave line 49? I tried that too, but was still getting error messages. Or should I be doing something else?

    please advise. thanks adeptris for bearing with me here.

    Nearly there, you do not need the ‘child_theme_setup’ function as we want the code to run before the parents functions.php file is run.

    So as in the sequence above number 1, all you need is just these 13 lines in the childs functions.php.

    Download the pastebin contents, this file will be run first and the function will now be loaded before the parents functions.php is run.

    When the functions.php is run the duplicate function will be ignored by the line:
    if ( ! function_exists( 'twentyten_posted_on' ) ) :

    The ! is the not operator so it reads ‘if not function exists’, as it does exist skip code to the endif;

    The child_theme_setup function you would use to change or add something after the functions are loaded, like the second menu

    HTH

    David

Viewing 15 replies - 1 through 15 (of 25 total)
  • The topic ‘can i create child functions.php file for twentyten?’ is closed to new replies.