1

I am new to wordpress themes and PHP. (I don't believe this is specific to WP) I would like to know the best way to store a block of HTML (with embeded PHP code) in a PHP variable.

<?php
function rfttc_insert_nav_menu ($position){
$poswanted = 'above';
$nav_code = 
    '<nav id="site-navigation" class="main-navigation" role="navigation">
        <h3 class="menu-toggle"><?php _e( \'Menu\', \'twentytwelve\' ); ?></h3>
        <a class="assistive-text" href="#content" title="<?php esc_attr_e( \'Skip to content\', \'twentytwelve\' ); ?>"><?php _e( \'Skip to content\', \'twentytwelve\' ); ?></a>
        <?php wp_nav_menu( array( \'theme_location\' => \'primary\', \'menu_class\' => \'nav-menu\' ) ); ?>
    </nav><!-- #site-navigation -->'

if($position == $poswanted)
    return $nav_code;
}
?>

Reading through many questions here leads me to believe that nowdoc and heredoc are not good choices. I have tried single quotes and escaping the single quotes inside. Same with double quotes. Both attempts resulted in error messages.

Any help here would be appreciated.

EDIT* The following code is what finally worked.

<?php
function rfttc_insert_nav_menu ($position){
    $poswanted = 'below';
    if($position == $poswanted){?>
        <nav id="site-navigation" class="main-navigation" role="navigation">
        <h3 class="menu-toggle"><?php __( 'Menu', 'twentytwelve' )?> </h3>
        <a class="assistive-text" href="#content" title="<?php
        esc_attr_e( 'Skip to content', 'twentytwelve' );
        ?>"><?php  _e( 'Skip to content', 'twentytwelve' );?>"</a>
        <?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu' ) );?>
        </nav><!-- #site-navigation -->
        <?php
    }
}
?>

Thanks to TJohnW for pointing me in the right direction: processing the inline PHP instead of trying to insert it.

5
  • Does this earlier article help? Commented Jan 26, 2013 at 5:26
  • Floris: I am not sure how 'htmlentities()' helps with a constant string. Commented Jan 26, 2013 at 5:29
  • Why do you need to do that? Can you elaborate on the reasoning, there is most likely another way that makes more sense. Commented Jan 26, 2013 at 5:30
  • ATOzTOA: I want to make it optional where to place the menu (above or below header image). '$poswanted' will come from a setting once this is working. Commented Jan 26, 2013 at 5:30
  • Why not just process it before and then output the navcode ? You don't need php inside of a string of html text. Commented Jan 26, 2013 at 5:33

1 Answer 1

1

I think I know what you are trying to do here. Give this a shot.

<?php
function rfttc_insert_nav_menu ($position){
   $poswanted = 'above';
   $nav_code = 
  '<nav id="site-navigation" class="main-navigation" role="navigation">
   <h3 class="menu-toggle">'. _e( 'Menu', 'twentytwelve' ) . '</h3>
   <a class="assistive-text" href="#content" title="' . esc_attr_e( 'Skip to content', 'twentytwelve' ) . '">' . _e( 'Skip to content', 'twentytwelve' ) . '</a>' . wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu' ) ) . '</nav><!-- #site-navigation -->';

  if($position == $poswanted) return $nav_code;
}
?>
Sign up to request clarification or add additional context in comments.

4 Comments

The above code gives me the following error: 'Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /homepages/.../wp-content/themes/.../functions.php on line 6'
There I fixed it. try it now.
That's closer than I have gotten this far. The output is a bit mangled. I think I see why.
Good, could I get an answer check :P

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.