0

I am trying to change the h1 tag in the head of my child theme from "Blog" to "Resources". The parent theme enqueues files with require_once and get_template_directory so it's very difficult to overwrite file through child theming. If anyone has a solution to that it would be great, but if not I thought I would just attempt to modify the variable in my functions file instead.

The parent theme uses this code basically.

function print_page_title() {
    global $post;
    $page_title = get_the_title($page_id);
    if(is_home()) {
        $page_title = esc_html__('Blog','crexis');
    } ?>

    <h1><?php echo esc_textarea($page_title); ?></h1><?php
}

(For testing purposes) if I modify it this way it works:

function print_page_title() {
    global $post;
    $page_title = get_the_title($page_id);
    if(is_home()) {
        $page_title = esc_html__('Blog','crexis');
    }

    if ( 'Blog' == $page_title ) {
        $page_title = 'Resources' ;
    } ?>
    <h1><?php echo esc_textarea($page_title); ?></h1><?php
}

But this (and other variations I've tried) doesn't work if I put the following in my child's functions.php:

function resources_header() {
    global $page_title;
    if ( 'Blog' == $page_title ) {
        $page_title = 'Resources' ;
    }
}
add_action( 'print_page_title', 'resources_header' );

I've also tried add_filter instead of add_action OR after_setup_theme instead of print_page_title. What am I doing wrong?

  • Where is print_page_title used in parent theme and how? Is it hooked to any action? – bravokeyl Sep 12 '16 at 4:35
  • It's just if(!function_exists('print_page_title')) { function print_page_title() {...}. Found in /framework/functions/header-functions.php. I can't find another reference to it anywhere. – Heather Sep 12 '16 at 4:39
  • Actually, that's not correct, I just fond it in the header.php file: if(!is_front_page() && ( theme_option('header_title') != 0 || theme_option('header_title') == '') && get_post_meta(get_the_ID(), 'page_header', true) != 'no-header' && !is_page_template('template-onepager.php') || is_search() && theme_option('header_title') != 0) { print_page_title(); } – Heather Sep 12 '16 at 4:40
1

To override the variable directly add the fucntion print_page_title in child theme functions.php this should work as the parent theme uses if(!function_exists('print_page_title')) condition to check for the function, since child theme's function.php is loaded first this is used instead of parent theme.

Place the following code in your child-theme's functions.php

function print_page_title() {
    global $post;
    $page_title = get_the_title($page_id);
    if(is_home()) {
        $page_title = esc_html__('Blog','crexis');
    }

    if ( 'Blog' == $page_title ) {
        $page_title = 'Resources' ;
    } ?>
    <h1><?php echo esc_textarea($page_title); ?></h1><?php
}

add_action( 'print_page_title', 'resources_header' );

The above doesn't work because there is no action hook print_page_title. It works if the code in your parent theme is like:

if(!is_front_page() && ( theme_option('header_title') != 0 '' ....//conditions)  {
    do_action('print_page_title');
}

add_filter( 'print_page_title', 'resources_header' );

will work if the funcion in parent theme function is like:

function print_page_title() {
    global $post;
    $page_title = get_the_title($page_id);
    if(is_home()) {
        $page_title = esc_html__('Blog','crexis');
    } 

    $page_title = apply_filters('print_page_title',$page_title);

    ?>

    <h1><?php echo esc_textarea($page_title); ?></h1><?php
}
  • Thanks, I'll give it a try, the only problem is it's a HUGE function. I cut a lot out for simplicity's sake. I think other things hook into it from there. – Heather Sep 12 '16 at 5:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.