0

The below code uses the bloginfo filter to add some CSS rules to my blog’s title and outputs it to the site header, without affecting the HTML head’s <title> tag. This solution was taken from here and it works very well (tested with Firefox in Ubuntu and with Android's default browser). I like it because I want to have only a single place from where to control my child theme, without changing the parent theme templates, as far as possible. The problem are global variables used here, that is not good for WP, as I have learned, but, as was suggested by @gmazzap here, using global variables inside functions is not a very bad solution. The question is if this code must be changed, so not to use global variables. And how to do this?

I use the Wordpress 4.7 with a Twenty Seventeen child theme.

$twentyseventeen_child_in_body = false;

function twentyseventeen_child_action_wp_head_finished() {
    global $twentyseventeen_child_in_body;
    $twentyseventeen_child_in_body = true;
}
add_action( 'wp_head', 'twentyseventeen_child_action_wp_head_finished', PHP_INT_MAX );

function twentyseventeen_child_action_wp_footer_started() {
    global $twentyseventeen_child_in_body;
    $twentyseventeen_child_in_body = false;
}
add_action( 'wp_footer', 'twentyseventeen_child_action_wp_footer_started', 0 );

function twentyseventeen_child_filter_bloginfo( $name, $show = null ) {
    global $twentyseventeen_child_in_body;
    if ( 'name' == $show && $twentyseventeen_child_in_body ) {
        $name = "<span class='info-style'>Info</span>" . "<span class='psi-style'>Psi</span>" . "<span class='md-style'>.md</span>";
        return "$name";
    } else {
        return $name;
    }
}
add_filter( 'bloginfo', 'twentyseventeen_child_filter_bloginfo', 10, 2 );
1

You can encapsulate the logic in a class:

class NameSwitch
{
    private $state = false;

    private $string;

    public function __construct( $string )
    {
        $this->string = $string;
    }

    public function change_state()
    {
        $this->state = ! $this->state;

        return $this->state;
    }

    public function replace( $output, $show = NULL )
    {
        if ( 'name' !== $show )
            return $output;

        if ( ! $this->state )
            return $output;

        return $this->string;
    }
}

Then create an instance of this class on template_redirect to restrict it to the front-end and assign the methods as callbacks as you did before:

add_action( 'template_redirect', function()
{
    $switch = new NameSwitch(
        "<span class='info-style'>Info</span><span class='psi-style'>Psi</span><span class='md-style'>.md</span>"
    );

    add_action( 'wp_head',   [ $switch, 'change_state' ], PHP_INT_MAX );
    add_action( 'wp_footer', [ $switch, 'change_state' ], 0 );
    add_filter( 'bloginfo',  [ $switch, 'replace' ], 10, 2 );
});

But what you should really do is:

Replace the call to bloginfo() in your child theme with a custom function or do_action('custom_name'). Then you wouldn't have to run the filter at all, just return your custom value.

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.