5

I am following a tutorial that requires I put this code above wp_head();

<?php
    $example_position = get_theme_mod( 'logo_placement' );
    if( $example_position != '' ) {
        switch ( $example_position ) {
            case 'left':
                // Do nothing. The theme already aligns the logo to the left
                break;
            case 'right':
                echo '<style type="text/css">';
                echo '#main-header #logo{ float: right; }';
                echo '</style>';
                break;
            case 'center':
                echo '<style type="text/css">';
                echo '#main-header{ text-align: center; }';
                echo '#main-header #logo { text-align: center; float: none; margin: 0 auto; display:block; }';
                echo '</style>';
                break;
        }
    }
?>

I was hoping I could hook into it somehow from a plugin. After checking the codex I was hoping I could do something like this but it doesn't work.

add_action('wp_head','hook_header');

function hook_header()
{

$output="<?php
    $example_position = get_theme_mod( 'logo_placement' );
    if( $example_position != '' ) {
        switch ( $example_position ) {
            case 'left':
                // Do nothing. The theme already aligns the logo to the left
                break;
            case 'right':
                echo '<style type="text/css">';
                echo '#main-header #logo{ float: right; }';
                echo '</style>';
                break;
            case 'center':
                echo '<style type="text/css">';
                echo '#main-header{ text-align: center; }';
                echo '#main-header #logo { text-align: center; float: none; margin: 0 auto; display:block; }';
                echo '</style>';
                break;
        }
    }
?>";

echo $output;

}'

1 Answer 1

9

Did you try this?

function hook_header() {
  $example_position = get_theme_mod( 'logo_placement' );
    if( $example_position != '' ) {
        switch ( $example_position ) {
            case 'left':
                // Do nothing. The theme already aligns the logo to the left
                break;
            case 'right':
                echo '<style type="text/css">';
                echo '#main-header #logo{ float: right; }';
                echo '</style>';
                break;
            case 'center':
                echo '<style type="text/css">';
                echo '#main-header{ text-align: center; }';
                echo '#main-header #logo { text-align: center; float: none; margin: 0 auto; display:block; }';
                echo '</style>';
                break;
        }
    }
}
add_action('wp_head','hook_header');
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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