1
votes

So I've created a child theme in WordPress, and everything works as it should except for the custom-header.php. I wish to use the one in the child theme, not the one in the parent theme. I am doing this by attempting to call it from the functions.php file in my child theme. I have my child theme's custom-header.php file in an inc folder, but have tried it in the main directory with the rest of the files.

My Code (functions.php)

<?php
/**
* Enqueues child theme stylesheet, loading first the parent theme stylesheet.
*/
function nisarg_custom_enqueue_child_theme_styles() {
    wp_enqueue_style( 'parent-theme-css', get_template_directory_uri() . '/style.css' );
}
add_action( 'wp_enqueue_scripts', 'nisarg_custom_enqueue_child_theme_styles', 11 );

/* Problem Code Below */
require_once( get_template_directory_uri() . '/inc/custom-header.php' );

The Problem

The problem is this line: require_once( get_stylesheet_directory() . '/inc/custom-header.php' ); Whenever I put any require_once statement in my functions.php file, the entire website goes blank.

My Question

How can I load my child theme's custom-header.php instead of the parent theme's?

Error Log

[Tue May 23 11:54:12.128948 2017] [:error] [pid 30729] [client 192.168.1.1:53819] PHP Warning:  require_once(): http:// wrapper is disabled in the server configuration by allow_url_include=0 in /var/www/1to1/wp/wordpress/wp-content/themes/nisarg-child/functions.php on line 11
[Tue May 23 11:54:12.129019 2017] [:error] [pid 30729] [client 192.168.1.1:53819] PHP Warning:  require_once(http://conradbowenrealestate.com/wp-content/themes/nisarg/inc/custom-header.php): failed to open stream: no suitable wrapper could be found in /var/www/1to1/wp/wordpress/wp-content/themes/nisarg-child/functions.php on line 11
[Tue May 23 11:54:12.129039 2017] [:error] [pid 30729] [client 192.168.1.1:53819] PHP Fatal error:  require_once(): Failed opening required 'http://conradbowenrealestate.com/wp-content/themes/nisarg/inc/custom-header.php' (include_path='.:/usr/share/php') in /var/www/1to1/wp/wordpress/wp-content/themes/nisarg-child/functions.php on line 11

My Code (custom-header.php)

<?php
/**
 *
 * @package Nisarg
 */

/**
 * Set up the WordPress core custom header feature.
 *
 * @uses nisarg_header_style()
 * @uses nisarg_admin_header_style()
 * @uses nisarg_admin_header_image()
 */
function nisarg_custom_header_setup() {
    add_theme_support( 'custom-header', apply_filters( 'nisarg_custom_header_args', array(
        'default-image'          => '',
        'default-text-color'     => 'fff',
        'width'                  => 1600,
        'height'                 => 400,
        'flex-height'            => true,
        'wp-head-callback'       => 'nisarg_header_style',
        'admin-head-callback'    => 'nisarg_admin_header_style',
        'admin-preview-callback' => 'nisarg_admin_header_image',
    ) ) );


    /*
     * Default custom headers packaged with the theme.
     * %s is a placeholder for the theme template directory URI.
     */
    register_default_headers( array(
        'mountains' => array(
            'url'           => '%s/images/headers/mountains.png',
            'thumbnail_url' => '%s/images/headers/mountains_thumbnail.png',
            'description'   => _x( 'food', 'header image description', 'nisarg' )
        ),  
        'skyline' => array(
            'url'           => '%s/images/headers/skyline.png',
            'thumbnail_url' => '%s/images/headers/skyline_thumbnail.png',
            'description'   => _x( 'buildings', 'header image description', 'nisarg' )
        ),
        'sea' => array(
            'url'           => '%s/images/headers/sea.png',
            'thumbnail_url' => '%s/images/headers/sea_thumbnail.png',
            'description'   => _x( 'Wood', 'header image description', 'nisarg' )
        ),
        'food' => array(
            'url'           => '%s/images/headers/food.png',
            'thumbnail_url' => '%s/images/headers/food_thumbnail.png',
            'description'   => _x( 'food', 'header image description', 'nisarg' )
        ),      
    ) );
}
add_action( 'after_setup_theme', 'nisarg_custom_header_setup' );

if ( ! function_exists( 'nisarg_header_style' ) ) :
/**
 * Styles the header image and text displayed on the blog
 *
 * @see nisarg_custom_header_setup().
 */
function nisarg_header_style() {
    $header_image = get_header_image();
    $header_text_color   = get_header_textcolor();

    // If no custom options for text are set, let's bail.
    if ( empty( $header_image ) && $header_text_color == get_theme_support( 'custom-header', 'default-text-color' ) ){
        return;
    }

    // If we get this far, we have custom styles.
    ?>
    <style type="text/css" id="nisarg-header-css">
    <?php
        if ( ! empty( $header_image ) ) :
            $header_width = get_custom_header()->width;
            $header_height = get_custom_header()->height;
            $header_height1 = ($header_height / $header_width * 1600);
            $header_height2 = ($header_height / $header_width * 768);
            $header_height3 = ($header_height / $header_width * 360);

    ?>
                .site-header {
                    background: url(<?php header_image(); ?>) no-repeat scroll top;
                    <?php if($header_height1 > 575){ ?>
                        background-size: 100% auto;
                        padding-bottom:44%
                    <?php }else{ ?>
                        background-size: 1600px 575px;
                        height: 575px;
                    <?php } ?>
                }

                @media (min-width: 768px) and (max-width: 1024px){
                    .site-header {
                        <?php if($header_height2 > 170){ ?>
                            background-size: 1024px auto;
                            height: <?php echo $header_height2;?>px;
                        <?php }else{ ?>
                            background-size: 1024px 170px;
                            height: 170px;
                        <?php } ?>              
                    }
                }

                @media (max-width: 767px) {
                    .site-header {
                        <?php if($header_height2 > 170){ ?>
                            background-size: 768px auto;
                            height: <?php echo $header_height2;?>px;
                        <?php }else{ ?>
                            background-size: 768px 170px;
                            height: 170px;
                        <?php } ?>              
                    }
                }
                @media (max-width: 359px) {
                    .site-header {
                        <?php if($header_height3 > 80){ ?>
                            background-size: 768px auto;
                            height: <?php echo $header_height3;?>px;
                        <?php }else{ ?>
                            background-size: 768px 80px;
                            height: 80px;
                        <?php } ?>

                    }

                }
                .site-header{
                    -webkit-box-shadow: 0px 0px 2px 1px rgba(182,182,182,0.3);
                    -moz-box-shadow: 0px 0px 2px 1px rgba(182,182,182,0.3);
                    -o-box-shadow: 0px 0px 2px 1px rgba(182,182,182,0.3);
                    box-shadow: 0px 0px 2px 1px rgba(182,182,182,0.3);
                }
  <?php else: ?>
    .site-header{
        -webkit-box-shadow: 0px 0px 1px 1px rgba(182,182,182,0.3);
        -moz-box-shadow: 0px 0px 1px 1px rgba(182,182,182,0.3);
        -o-box-shadow: 0px 0px 1px 1px rgba(182,182,182,0.3);
        box-shadow: 0px 0px 1px 1px rgba(182,182,182,0.3);
    }
    .site-header {
            height: 300px;
        }
        @media (max-width: 767px) {
            .site-header {
                height: 200px;
            }
        }
        @media (max-width: 359px) {
            .site-header {
                height: 150px;
            }
        }

    <?php endif; 

        // Has the text been hidden?
        if ( ! display_header_text() ) :

    ?>
        .site-title,
        .site-description {
            position: absolute;
            clip: rect(1px 1px 1px 1px); /* IE7 */
            clip: rect(1px, 1px, 1px, 1px);
        }
    <?php
        endif;
        if ( empty( $header_image ) ) :
    ?>
        .site-header .home-link {
            min-height: 0;
        }
    <?php

        // If the user has set a custom color for the text, use that.

        else:

    ?>

        .site-title,
        .site-description {
            color: #<?php echo esc_attr( $header_text_color ); ?>;
        }
        .site-title::after{
            background: #<?php echo esc_attr( $header_text_color ); ?>;
            content:"";       
        }
    <?php endif; ?>

    </style>
    <?php
}
endif; // nisarg_header_style


if ( ! function_exists( 'nisarg_admin_header_style' ) ) :

/**
 * Styles the header image displayed on the Appearance > Header admin panel.
 *
 * @see nisarg_custom_header_setup().
 */
function nisarg_admin_header_style() {
?>
    <?php
        if ( 'blank' == get_header_textcolor() || '' == get_header_textcolor() )
            $style = ' style="display:none;"';
        else
            $style = ' style="color:#' . get_header_textcolor() . ';"';
    ?>
    <style type="text/css">
        .appearance_page_custom-header #headimg{
            background: #fff;
            border: none;
        }

        #headimg .site-branding {
            margin:0;
            position: absolute;
            top: 50%;
            left: 50%;
            margin-right: -50%;
            -webkit-transform: translate(-50%,-50%);
            -ms-transform: translate(-50%,-50%);
            -o-transform: translate(-50%,-50%);
            transform: translate(-50%,-50%);
            text-align: center;
        }


        #headimg h1 { /* This is the site title displayed in the preview */
            <?php echo esc_attr($style); ?>
            text-transform: uppercase; 
            letter-spacing: 10px;
        }

        #headimg a{
            text-decoration: none;
        }

        #headimg #desc{
            <?php echo esc_attr($style); ?>
            text-decoration: none;
            padding: 0.2em 0em;
            letter-spacing: 5px;
            text-transform: capitalize;
        }
        #headimg .home-link .displaying-header-text::after{
            background: #<?php get_header_textcolor() ?>;
            content:"";
            height: 2px;
            display: block;
            width: 20%;
            margin: 5px auto;
        }
    </style>
<?php
}
endif; // nisarg_admin_header_style

if ( ! function_exists( 'nisarg_admin_header_image' ) ) :
/**
 * Custom header image markup displayed on the Appearance > Header admin panel.
 *
 * @see nisarg_custom_header_setup().
 */
function nisarg_admin_header_image() {
?>

<?php
        if ( 'blank' == get_header_textcolor() || '' == get_header_textcolor() )
            $style = ' style="color: #fff;"';
        else
            $style = ' style="color:#' . get_header_textcolor() . ';"';

        ?>

    <div id="headimg">      
        <div class="site-branding">
            <div class="home-link">

                <h1 class="displaying-header-text"><a id="name" style="<?php echo esc_attr( $style ); ?>" onclick="return false;" href="<?php esc_url('#'); ?>" tabindex="-1"><?php bloginfo( 'name' ); ?></a></h1>
                <h2 id="desc" class="displaying-header-text" style="<?php echo esc_attr( $style ); ?>"><?php bloginfo( 'description' ); ?></h2>
            </div>
        </div>
        <?php $header_image = get_header_image();
        if ( ! empty( $header_image ) ) : ?>
            <style type="text/css">
                .site-header {
                    background: url(<?php header_image(); ?>) no-repeat scroll top;
                }
            </style>
        <?php endif; ?>
        <?php if(''!= get_header_textcolor() || 'blank' != get_header_textcolor()){ ?>
                <style type="text/css">
                #headimg h1::after{
                background: #<?php get_header_textcolor() ?>;
                content:"";
                height: 2px;
                display: block;
                width: 20%;
                margin: 5px auto;
                }       
            </style>
        <?php } ?>
    </div>
<?php
}
endif; // nisarg_admin_header_image

All code posted in this question is from the child theme.

3
please show the problematic custom-header.php file contentDRC
@DRC The custom-header.php file works fine when I put it in the parent theme's inc folder. The problem is I can't load it in the child theme. But I'll go ahead and post the code anyway.z7r1k3
Check with firebug (F12) to see if its loading the css file and if you have the path of the php file correct, plus I think you are loading the php file into the head since you're using enqueue scripts for thisK. Tromp
Check your error log or turn on error reporting.Nathan Dawson
@K.Tromp Not sure what firebug is, but that pulls up inspector for me. I'll see if I notice anything there.z7r1k3

3 Answers

2
votes

Please go through the below following steps, may helps you

For example Your child theme is {parent theme name}-child

Step-1: Your child theme style.css file should be like this

/* 
Theme Name:      {Parent theme name} Child Theme 
Theme URI:       http://www.example.com/
Description:     Theme by Site <a href=\"http://www.example.com//\">http://childthemegenerator.com/</a>
Author:          some XYZ
Author URI:      http://childthemegenerator.com/
Template:        {Parent theme name}
Version:         1.0.0
Text Domain:     {Parent theme name}-child
-------------------------------------------------------------- */ 

/* =Theme customization starts here
------------------------------------------------------- */

Step-2: Override your parent theme "inc/custom-header.php" into child theme "inc/custom-header.php"

Step-3: Edit the child theme "inc/custom-header.php" file of each function, because these function already defined in parent theme "inc/custom-header.php".

Same functions are using in your child theme "inc/custom-header.php" file. This is the reason you are getting website goes blank.

So you should rename each and every fnction of the child theme "inc/custom-header.php" file and save it. I have already renamed each function in the child theme "inc/custom-header.php" file.

The child theme "inc/custom-header.php" file should be like this

<?php
/**
 *
 * @package Nisarg Child Theme 
 */

/**
 * Set up the WordPress core custom header feature.
 *
 * @uses nisarg_child_header_style()
 * @uses nisarg_child_admin_header_style()
 * @uses nisarg_child_admin_header_image()
 */
function nisarg_child_custom_header_setup() {
    add_theme_support( 'custom-header', apply_filters( 'nisarg_child_custom_header_args', array(
        'default-image'          => '',
        'default-text-color'     => 'fff',
        'width'                  => 1600,
        'height'                 => 400,
        'flex-height'            => true,
        'wp-head-callback'       => 'nisarg_child_header_style',
        'admin-head-callback'    => 'nisarg_child_admin_header_style',
        'admin-preview-callback' => 'nisarg_child_admin_header_image',
    ) ) );

    /*
     * Default custom headers packaged with the theme.
     * %s is a placeholder for the theme template directory URI.
     */
    register_default_headers( array(
        'mountains' => array(
            'url'           => '%s/images/headers/mountains.png',
            'thumbnail_url' => '%s/images/headers/mountains_thumbnail.png',
            'description'   => _x( 'food', 'header image description', 'nisarg' ),
        ),
        'skyline' => array(
            'url'           => '%s/images/headers/skyline.png',
            'thumbnail_url' => '%s/images/headers/skyline_thumbnail.png',
            'description'   => _x( 'buildings', 'header image description', 'nisarg' ),
        ),
        'sea' => array(
            'url'           => '%s/images/headers/sea.png',
            'thumbnail_url' => '%s/images/headers/sea_thumbnail.png',
            'description'   => _x( 'Wood', 'header image description', 'nisarg' ),
        ),
        'food' => array(
            'url'           => '%s/images/headers/food.png',
            'thumbnail_url' => '%s/images/headers/food_thumbnail.png',
            'description'   => _x( 'food', 'header image description', 'nisarg' ),
        ),
    ) );
}
add_action( 'after_setup_theme', 'nisarg_child_custom_header_setup' );

if ( ! function_exists( 'nisarg_child_header_style' ) ) :
    /**
     * Styles the header image and text displayed on the blog
     *
     * @see nisarg_custom_header_setup().
     */
    function nisarg_child_header_style() {
        $header_image = get_header_image();
        $header_text_color   = get_header_textcolor();
        // If no custom options for text are set, let's bail.
        if ( empty( $header_image ) && $header_text_color == get_theme_support( 'custom-header', 'default-text-

color' ) ) {
            return;
        }
        // If we get this far, we have custom styles.
        ?>
        <style type="text/css" id="nisarg-header-css">
        <?php
        if ( ! empty( $header_image ) ) :
            $header_width = get_custom_header()->width;
            $header_height = get_custom_header()->height;
            $header_height320 = ( $header_height / $header_width * 320 );
            $header_height360 = ( $header_height / $header_width * 360 );
            $header_height768 = ( $header_height / $header_width * 768 );
            $header_height980 = ( $header_height / $header_width * 980 );
            $header_height1280 = ( $header_height / $header_width * 1280 );
            $header_height1366 = ( $header_height / $header_width * 1366 );
            $header_height1440 = ( $header_height / $header_width * 1440 );
            $header_height1600 = ( $header_height / $header_width * 1600 );
            $header_height1920 = ( $header_height / $header_width * 1920 );
            $header_height2560 = ( $header_height / $header_width * 2560 );
            $header_height2880 = ( $header_height / $header_width * 2880 );
            ?>
            .site-header {
                background: url(<?php header_image(); ?>) no-repeat scroll top;
                background-size: cover;
            }
            @media (min-width: 320px) and (max-width: 359px ) {
                .site-header {
                    height: <?php echo absint( $header_height320 );?>px;
                }
            }
            @media (min-width: 360px) and (max-width: 767px ) {
                .site-header {
                    height: <?php echo absint( $header_height360 );?>px;
                }
            }
            @media (min-width: 768px) and (max-width: 979px ) {
                .site-header {
                    height: <?php echo absint( $header_height768 );?>px;
                }
            }
            @media (min-width: 980px) and (max-width: 1279px ){
                .site-header {
                    height: <?php echo absint( $header_height980 );?>px;
                }
            }
            @media (min-width: 1280px) and (max-width: 1365px ){
                .site-header {
                    height: <?php echo absint( $header_height1280 );?>px;
                }
            }
            @media (min-width: 1366px) and (max-width: 1439px ){
                .site-header {
                    height: <?php echo absint( $header_height1366 );?>px;
                }
            }
            @media (min-width: 1440px) and (max-width: 1599px ) {
                .site-header {
                    height: <?php echo absint( $header_height1440 );?>px;
                }
            }
            @media (min-width: 1600px) and (max-width: 1919px ) {
                .site-header {
                    height: <?php echo absint( $header_height1600 );?>px;
                }
            }
            @media (min-width: 1920px) and (max-width: 2559px ) {
                .site-header {
                    height: <?php echo absint( $header_height1920 );?>px;
                }
            }
            @media (min-width: 2560px)  and (max-width: 2879px ) {
                .site-header {
                    height: <?php echo absint( $header_height2560 );?>px;
                }
            }
            @media (min-width: 2880px) {
                .site-header {
                    height: <?php echo absint( $header_height2880 );?>px;
                }
            }
            .site-header{
                -webkit-box-shadow: 0px 0px 2px 1px rgba(182,182,182,0.3);
                -moz-box-shadow: 0px 0px 2px 1px rgba(182,182,182,0.3);
                -o-box-shadow: 0px 0px 2px 1px rgba(182,182,182,0.3);
                box-shadow: 0px 0px 2px 1px rgba(182,182,182,0.3);
            }
        <?php else : ?>
            .site-header{
                -webkit-box-shadow: 0px 0px 1px 1px rgba(182,182,182,0.3);
                -moz-box-shadow: 0px 0px 1px 1px rgba(182,182,182,0.3);
                -o-box-shadow: 0px 0px 1px 1px rgba(182,182,182,0.3);
                box-shadow: 0px 0px 1px 1px rgba(182,182,182,0.3);
            }
            .site-header {
                    height: 300px;
                }
                @media (max-width: 767px) {
                    .site-header {
                        height: 200px;
                    }
                }
                @media (max-width: 359px) {
                    .site-header {
                        height: 150px;
                    }
                }
        <?php endif;
        // Has the text been hidden?
        if ( ! display_header_text() ) :
        ?>
            .site-title,
            .site-description {
                position: absolute;
                clip: rect(1px 1px 1px 1px); /* IE7 */
                clip: rect(1px, 1px, 1px, 1px);
            }
        <?php
        endif;
        if ( empty( $header_image ) ) :
            ?>
            .site-header .home-link {
                min-height: 0;
            }
            <?php
            // If the user has set a custom color for the text, use that.
        else : ?>
            .site-title,
            .site-description {
                color: #<?php echo esc_attr( $header_text_color ); ?>;
            }
            .site-title::after{
                background: #<?php echo esc_attr( $header_text_color ); ?>;
                content:"";
            }
        <?php endif; ?>
        </style>
        <?php
    }
endif; // nisarg_child_header_style


if ( ! function_exists( 'nisarg_child_admin_header_style' ) ) :
    /**
     * Styles the header image displayed on the Appearance > Header admin panel.
     *
     * @see nisarg_custom_header_setup().
     */
    function nisarg_child_admin_header_style() {
    ?>
        <?php
        if ( 'blank' == get_header_textcolor() || '' == get_header_textcolor() ) {
            $style = ' style="display:none;"';
        } else {
            $style = ' style="color:#' . get_header_textcolor() . ';"';
        }
        ?>
        <style type="text/css">
            .appearance_page_custom-header #headimg{
                background: #fff;
                border: none;
            }

            #headimg .site-branding {
                margin:0;
                position: absolute;
                top: 50%;
                left: 50%;
                margin-right: -50%;
                -webkit-transform: translate(-50%,-50%);
                -ms-transform: translate(-50%,-50%);
                -o-transform: translate(-50%,-50%);
                transform: translate(-50%,-50%);
                text-align: center;
            }
            #headimg h1 { /* This is the site title displayed in the preview */
                <?php echo esc_attr( $style ); ?>
                text-transform: uppercase;
                letter-spacing: 10px;
            }

            #headimg a{
                text-decoration: none;
            }
            #headimg #desc{
                <?php echo esc_attr( $style ); ?>
                text-decoration: none;
                padding: 0.2em 0em;
                letter-spacing: 5px;
                text-transform: capitalize;
            }
            #headimg .home-link .displaying-header-text::after{
                background: #<?php get_header_textcolor() ?>;
                content:"";
                height: 2px;
                display: block;
                width: 20%;
                margin: 5px auto;
            }
        </style>
    <?php
    }
endif; // nisarg_child_admin_header_style

if ( ! function_exists( 'nisarg_child_admin_header_image' ) ) :
    /**
     * Custom header image markup displayed on the Appearance > Header admin panel.
     *
     * @see nisarg_child_custom_header_setup().
     */
    function nisarg_child_admin_header_image() {
        if ( 'blank' == get_header_textcolor() || '' == get_header_textcolor() ) {
            $style = ' style="color: #fff;"';
        } else {
            $style = ' style="color:#' . get_header_textcolor() . ';"';
        } ?>
    <div id="headimg">
        <div class="site-branding">
            <div class="home-link">
                <h1 class="displaying-header-text"><a id="name" style="<?php echo esc_attr( $style ); ?>" 

onclick="return false;" href="<?php esc_url('#'); ?>" tabindex="-1"><?php bloginfo( 'name' ); ?></a></h1>
                <h2 id="desc" class="displaying-header-text" style="<?php echo esc_attr( $style ); ?>"><?

php bloginfo( 'description' ); ?></h2>
            </div>
        </div>
        <?php $header_image = get_header_image();
        if ( ! empty( $header_image ) ) : ?>
            <style type="text/css">
                .site-header {
                    background: url(<?php header_image(); ?>) no-repeat scroll top;
                }
            </style>
        <?php endif; ?>
        <?php if ( '' != get_header_textcolor() || 'blank' != get_header_textcolor() ) { ?>
            <style type="text/css">
                #headimg h1::after{
                    background: #<?php get_header_textcolor() ?>;
                    content:"";
                    height: 2px;
                    display: block;
                    width: 20%;
                    margin: 5px auto;
                }
            </style>
        <?php } ?>
    </div>
    <?php
    }
endif; // nisarg_child_admin_header_image

Step-4: Your child theme fnctions.php should be like this

<?php 
     add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_styles' );
     function my_theme_enqueue_styles() { 
          wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' ); 
          } 


      require_once( get_stylesheet_directory() . '/inc/custom-header.php' );            

 ?>
1
votes

You're attempting to require a file using a URL instead of a path. get_template_directory_uri() will return the URL to the parent theme folder.

You need get_stylesheet_directory() to get the path to the child theme.

Replace this:

require_once( get_template_directory_uri() . '/inc/custom-header.php' );

With:

require_once( get_stylesheet_directory() . '/inc/custom-header.php' );
0
votes

It helps you by using the get_header() fnction

Your custom header name should be like this "header-name.php", then call custom header name in that function.

<?php get_header('name'); ?>