Support » Theme: Attitude » Child functions.php

  • I am trying to create a child functions.php so that I can make adjustments for the menu I am using but I am a little bit confused about how I go about that with this theme. The actual code that I need to alter is in the header-extensions.php file which is only referenced in the functions.php. If I put the correct code into the header-extensions.php file, how do I create a child functions.php that will acknowlwdge the child header-extensions.php file change first and then refer to the parent file from that point on? Thanks in advance for your help!

Viewing 8 replies - 1 through 8 (of 8 total)
  • Ayman

    (@aymanalzarrad)

    1. Make a copy of your header-extensions.php into your theme root directory and rename it to something like child-header-extensions.php

    2. Create a new file in your child theme root directory and call it functions.php

    3. Add the following code to the child functions.php:

    <?php
    function get_child_header_extentions {
        require_once( get_stylesheet_directory(). '/child-header-extensions.php' );
    }
    ?>

    4. Go back and see which function of the functions.php file of the parent theme is calling the header-extensions.php

    And surround it by an if statement.. For example:

    We already have our functions.php that is calling header-extensions.php like this:
    require_once( get_stylesheet_directory(). '/header-extensions.php' );

    We should than change it to this:

    if (!function_exists('get_child_header_extentions') ){
    require_once( get_stylesheet_directory(). '/header-extensions.php' );
    }

    The fact is that a child functions.php is loaded before the parent one so what we did is that we told the parent one to check first if our child-header-extensions.php is already being called so don’t call header-extensions.php and if its not than call header-extensions.php

    5. Now go to your child-header-extensions.php and modify it as you need.

    Thanks for your help Ayman! That all makes prefect sense. The only thing I am not sure how to incorporate is the last part about calling the child-header-extensions.php file because this theme has load files functions set up (please see code below):

    <?php
    /**
     * Attitude defining constants, adding files and WordPress core functionality.
     *
     * Defining some constants, loading all the required files and Adding some core functionality.
     * @uses add_theme_support() To add support for post thumbnails and automatic feed links.
     * @uses register_nav_menu() To add support for navigation menu.
     * @uses set_post_thumbnail_size() To set a custom post thumbnail size.
     *
     * @package Theme Horse
     * @subpackage Attitude
     * @since Attitude 1.0
     */
    
    /**
     * Set the content width based on the theme's design and stylesheet.
     */
    if ( ! isset( $content_width ) )
    	$content_width = 700;
    
    add_action( 'attitude_init', 'attitude_constants', 10 );
    /**
     * This function defines the Attitude theme constants
     *
     * @since 1.0
     */
    function attitude_constants() {
    
    	/** Define Directory Location Constants */
    	define( 'ATTITUDE_PARENT_DIR', get_template_directory() );
    	define( 'ATTITUDE_CHILD_DIR', get_stylesheet_directory() );
    	define( 'ATTITUDE_IMAGES_DIR', ATTITUDE_PARENT_DIR . '/images' );
    	define( 'ATTITUDE_LIBRARY_DIR', ATTITUDE_PARENT_DIR. '/library' );
    	define( 'ATTITUDE_ADMIN_DIR', ATTITUDE_LIBRARY_DIR . '/admin' );
    	define( 'ATTITUDE_ADMIN_IMAGES_DIR', ATTITUDE_ADMIN_DIR . '/images' );
    	define( 'ATTITUDE_ADMIN_JS_DIR', ATTITUDE_ADMIN_DIR . '/js' );
    	define( 'ATTITUDE_ADMIN_CSS_DIR', ATTITUDE_ADMIN_DIR . '/css' );
    	define( 'ATTITUDE_JS_DIR', ATTITUDE_LIBRARY_DIR . '/js' );
    	define( 'ATTITUDE_CSS_DIR', ATTITUDE_LIBRARY_DIR . '/css' );
    	define( 'ATTITUDE_FUNCTIONS_DIR', ATTITUDE_LIBRARY_DIR . '/functions' );
    	define( 'ATTITUDE_SHORTCODES_DIR', ATTITUDE_LIBRARY_DIR . '/shortcodes' );
    	define( 'ATTITUDE_STRUCTURE_DIR', ATTITUDE_LIBRARY_DIR . '/structure' );
    	if ( ! defined( 'ATTITUDE_LANGUAGES_DIR' ) ) /** So we can define with a child theme */
    		define( 'ATTITUDE_LANGUAGES_DIR', ATTITUDE_LIBRARY_DIR . '/languages' );
    	define( 'ATTITUDE_WIDGETS_DIR', ATTITUDE_LIBRARY_DIR . '/widgets' );
    
    	/** Define URL Location Constants */
    	define( 'ATTITUDE_PARENT_URL', get_template_directory_uri() );
    	define( 'ATTITUDE_CHILD_URL', get_stylesheet_directory_uri() );
    	define( 'ATTITUDE_IMAGES_URL', ATTITUDE_PARENT_URL . '/images' );
    	define( 'ATTITUDE_LIBRARY_URL', ATTITUDE_PARENT_URL . '/library' );
    	define( 'ATTITUDE_ADMIN_URL', ATTITUDE_LIBRARY_URL . '/admin' );
    	define( 'ATTITUDE_ADMIN_IMAGES_URL', ATTITUDE_ADMIN_URL . '/images' );
    	define( 'ATTITUDE_ADMIN_JS_URL', ATTITUDE_ADMIN_URL . '/js' );
    	define( 'ATTITUDE_ADMIN_CSS_URL', ATTITUDE_ADMIN_URL . '/css' );
    	define( 'ATTITUDE_JS_URL', ATTITUDE_LIBRARY_URL . '/js' );
    	define( 'ATTITUDE_CSS_URL', ATTITUDE_LIBRARY_URL . '/css' );
    	define( 'ATTITUDE_FUNCTIONS_URL', ATTITUDE_LIBRARY_URL . '/functions' );
    	define( 'ATTITUDE_SHORTCODES_URL', ATTITUDE_LIBRARY_URL . '/shortcodes' );
    	define( 'ATTITUDE_STRUCTURE_URL', ATTITUDE_LIBRARY_URL . '/structure' );
    	if ( ! defined( 'ATTITUDE_LANGUAGES_URL' ) ) /** So we can predefine to child theme */
    		define( 'ATTITUDE_LANGUAGES_URL', ATTITUDE_LIBRARY_URL . '/languages' );
    	define( 'ATTITUDE_WIDGETS_URL', ATTITUDE_LIBRARY_URL . '/widgets' );
    
    }
    
    add_action( 'attitude_init', 'attitude_load_files', 15 );
    /**
     * Loading the included files.
     *
     * @since 1.0
     */
    function attitude_load_files() {
    	/**
    	 * attitude_add_files hook
    	 *
    	 * Adding other addtional files if needed.
    	 */
    	do_action( 'attitude_add_files' );
    
    	/** Load functions */
    	require_once( ATTITUDE_FUNCTIONS_DIR . '/i18n.php' );
    	require_once( ATTITUDE_FUNCTIONS_DIR . '/custom-header.php' );
    	require_once( ATTITUDE_FUNCTIONS_DIR . '/functions.php' );
    
    	require_once( ATTITUDE_ADMIN_DIR . '/attitude-themeoptions-defaults.php' );
    	require_once( ATTITUDE_ADMIN_DIR . '/theme-options.php' );
    	require_once( ATTITUDE_ADMIN_DIR . '/attitude-metaboxes.php' );
    	require_once( ATTITUDE_ADMIN_DIR . '/attitude-show-post-id.php' );
    
    	/** Load Shortcodes */
    	require_once( ATTITUDE_SHORTCODES_DIR . '/attitude-shortcodes.php' );
    
    	/** Load Structure */
    	require_once( ATTITUDE_STRUCTURE_DIR . '/child-header-extensions.php' );
            require_once( ATTITUDE_STRUCTURE_DIR . '/header-extensions.php' );
    	require_once( ATTITUDE_STRUCTURE_DIR . '/searchform-extensions.php' );
    	require_once( ATTITUDE_STRUCTURE_DIR . '/sidebar-extensions.php' );
    	require_once( ATTITUDE_STRUCTURE_DIR . '/footer-extensions.php' );
    	require_once( ATTITUDE_STRUCTURE_DIR . '/content-extensions.php' );
    
    	/** Load Widgets and Widgetized Area */
    	require_once( ATTITUDE_WIDGETS_DIR . '/attitude_widgets.php' );
    }
    
    add_action( 'attitude_init', 'attitude_core_functionality', 20 );
    /**
     * Adding the core functionality of WordPess.
     *
     * @since 1.0
     */
    function attitude_core_functionality() {
    	/**
    	 * attitude_add_functionality hook
    	 *
    	 * Adding other addtional functionality if needed.
    	 */
    	do_action( 'attitude_add_functionality' );
    
    	// Add default posts and comments RSS feed links to head
    	add_theme_support( 'automatic-feed-links' );
    
    	// This theme uses Featured Images (also known as post thumbnails) for per-post/per-page.
    	add_theme_support( 'post-thumbnails' ); 
    
    	// Remove WordPress version from header for security concern
    	remove_action( 'wp_head', 'wp_generator' );
    
    	// This theme uses wp_nav_menu() in header menu location.
    	register_nav_menu( 'primary', __( 'Primary Menu', 'attitude' ) );
    
    	// Add Attitude custom image sizes
    	add_image_size( 'featured', 670, 300, true );
    	add_image_size( 'featured-medium', 230, 230, true );
    	add_image_size( 'slider-narrow', 1038, 460, true ); 		// used on Featured Slider on Homepage Header for narrow layout
    	add_image_size( 'slider-wide', 1400, 460, true ); 			// used on Featured Slider on Homepage Header for wide layout
    	add_image_size( 'gallery', 474, 342, true ); 				// used to show gallery all images
    	add_image_size( 'icon', 80, 80, true );						//used for icon on business layout
    
    	/**
    	 * This theme supports custom background color and image
    	 */
    	add_theme_support( 'custom-background' );
    }
    
    /**
     * attitude_init hook
     *
     * Hooking some functions of functions.php file to this action hook.
     */
    do_action( 'attitude_init' );
    ?>

    How do I incorporate the if (!function_exists('get_child_header_extensions') portion into this?

    Thank you again for your help!

    Ayman

    (@aymanalzarrad)

    Note the part of the functions.php where it stats:

    /** Load Structure */
    	require_once( ATTITUDE_STRUCTURE_DIR . '/child-header-extensions.php' );
            require_once( ATTITUDE_STRUCTURE_DIR . '/header-extensions.php' );
    	require_once( ATTITUDE_STRUCTURE_DIR . '/searchform-extensions.php' );
    	require_once( ATTITUDE_STRUCTURE_DIR . '/sidebar-extensions.php' );
    	require_once( ATTITUDE_STRUCTURE_DIR . '/footer-extensions.php' );
    	require_once( ATTITUDE_STRUCTURE_DIR . '/content-extensions.php' );

    And replace it with :

    /** Load Structure */
    	require_once( ATTITUDE_STRUCTURE_DIR . '/child-header-extensions.php' );
            if (!function_exists('get_child_header_extentions') ){
            require_once( ATTITUDE_STRUCTURE_DIR . '/header-extensions.php' );
            }
    	require_once( ATTITUDE_STRUCTURE_DIR . '/searchform-extensions.php' );
    	require_once( ATTITUDE_STRUCTURE_DIR . '/sidebar-extensions.php' );
    	require_once( ATTITUDE_STRUCTURE_DIR . '/footer-extensions.php' );
    	require_once( ATTITUDE_STRUCTURE_DIR . '/content-extensions.php' );

    Note that you are even calling the child-header-extensions.php in the functions.php using this:
    require_once( ATTITUDE_STRUCTURE_DIR . '/child-header-extensions.php' );

    While you should be doing it in the child functions.php file.

    Thank you again Ayman, you have been a tremendous help. Please forgive me, since the functions.php file is a little different I want to be sure I am placing everything in the correct spot.

    So, in the child functions.php file I place the following code:

    <?php
    function get_child_header_extentions {
        require_once( get_stylesheet_directory(). '/child-header-extensions.php' );
    }
    ?>'
    
    Then, in the parent functions.php file I place the following code under the "Load Structure":

    require_once( ATTITUDE_STRUCTURE_DIR . ‘/child-header-extensions.php’ );
    if (!function_exists(‘get_child_header_extentions’) ){
    require_once( ATTITUDE_STRUCTURE_DIR . ‘/header-extensions.php’ );
    }`

    Replacing the portion where I tried to add the child-header-extentions require_once command.

    Then I place the child-header-extensions.php file in the folder where the original header-extensions.php file is stored so that it will be recognized by the parent function.php file structure. Is all of that correct? I tried it and it’s not working for me but I am clearly no php expert so I may be confused.

    Thank you again for your help!

    Christie

    Ayman

    (@aymanalzarrad)

    Ok, so lets make a clear map of this…

    Path to the child theme should be something like this:
    domain-name/wp-content/themes/our-child-theme
    1- The child-header-extensions.php should be placed in the directory of the child theme so the final path should look like this:
    domain-name/wp-content/themes/our-child-theme/child-header-extensions.php

    2- The child functions.php should be placed in the directory of the child theme so the final path should look like this:
    domain-name/wp-content/themes/our-child-theme/functions.php

    3- The following code should be placed in our child functions.php that should be found @ domain-name/wp-content/themes/our-child-theme/functions.php
    The code to add:

    <?php
    function get_child_header_extentions {
        require_once( get_stylesheet_directory(). '/child-header-extensions.php' );
    }
    ?>

    4- The /** Load Structure */ part in the parent functions.php should be exactly like this:

    /** Load Structure */ /* Just COPY and Past replacing the old one */
            if (!function_exists('get_child_header_extentions') ){
            require_once( ATTITUDE_STRUCTURE_DIR . '/header-extensions.php' );
            }
    	require_once( ATTITUDE_STRUCTURE_DIR . '/searchform-extensions.php' );
    	require_once( ATTITUDE_STRUCTURE_DIR . '/sidebar-extensions.php' );
    	require_once( ATTITUDE_STRUCTURE_DIR . '/footer-extensions.php' );
    	require_once( ATTITUDE_STRUCTURE_DIR . '/content-extensions.php' );

    At this point I believe everything should be working fine… Let me know if you keep on facing problem with it.

    Thank you for going through the steps for me. I did everything listed above but it’s not working for some reason. I did note that “extensions” was spelled differently in a couple of places and changed that but it didn’t seem to resolve the issue. I see how the child functions.php refers to the child-header-extensions.php file and the parent functions.php file refers to the child-header-extensions.php file but how does the parent functions.php file know where to look for the child-header-extensions.php file. Also, when I perform an upgrade will the parent functions.php file be written over and then I will lose that function? Should we be putting the command to override the header-extensions.php in the child functions.php file instead maybe? Wish I was better at this php stuff. I am so appreciative of your help Ayman!

    Hikescdnrckys

    (@hikescdnrckys)

    I ran into the same issue as SQSBMedia. When I accessed my website I was getting an error re an unexpected ‘{‘ – expecting ‘(‘ on line 2.

    I think the solution is a missing set of brackets in the functions.php code after get_child_extensions. It should be:

    <?php
    function get_child_header_extentions() {
        require_once( get_stylesheet_directory(). '/child-header-extensions.php' );
    }
    ?>

    This worked for me – at least until I try to modify child-header-extensions.php file.

    Does this work with content-extensions??

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Child functions.php’ is closed to new replies.