Подключитесь к wp head(); в плагине


Я следую учебнику, который требует, чтобы я поместил этот код выше 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;
        }
    }
?>

Я надеялся, что смогу как-нибудь подключиться к нему с помощью плагина. После проверки кодекса я надеялся, что смогу сделать что-то подобное, но это не работает.

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;

}'
Author: milo99, 2014-11-09

1 answers

Вы пробовали это?

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');
 7
Author: shanebp, 2014-11-09 17:31:57