[Support request] generate_inside_mobile_header

Home Forums Support [Support request] generate_inside_mobile_header

Home Forums Support generate_inside_mobile_header

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #683091
    sg

    Hi, i am trying to add login logout link that change accordingly.

    this is my rough code. however its giving error. do you know why pls.

    add_action( 'generate_inside_mobile_header','tu_mobile_header_html' );
    function tu_mobile_header_html( $items) { ?>
        <div class="mobile-header-content"> 
      <i class="fal fa-user-circle"></i>
      if( is_user_logged_in( ) )
            $link = '<a href="' . wp_logout_url( $redirect ) . '" title="' .  __( 'Logout' ) .'">' . __( 'Logout' ) . '</a>';
        else  
    	  $link = '<a href="/login" title="' .  __( 'Login' ) .'">' . __( 'Login' ) . '</a>';
        return $items.= '<li id="log-in-out-link" class="menu-item menu-type-link">'. $link . '</li>';
      
      
      </div>
     }
    #683116
    David
    Staff
    Customer Support

    Hi there,

    you are missing the correct script tags around the PHP.

    add_action( 'generate_inside_mobile_header','tu_mobile_header_html' );
    function tu_mobile_header_html( $items) { ?>
        <div class="mobile-header-content"> 
            <i class="fal fa-user-circle"></i>
            
            <?php
            if( is_user_logged_in( ) )
            $link = '<a href="' . wp_logout_url( $redirect ) . '" title="' .  __( 'Logout' ) .'">' . __( 'Logout' ) . '</a>';
            else  
            $link = '<a href="/login" title="' .  __( 'Login' ) .'">' . __( 'Login' ) . '</a>';
            return $items.= '<li id="log-in-out-link" class="menu-item menu-type-link">'. $link . '</li>';
            ?>
      
      </div><?php
     }

    You could alternatively use the Hook Element to hook the link and use the display rules for User:

    https://docs.generatepress.com/article/hooks-element-overview/

    #683126
    sg

    Thank you David.

    However the code is not working for me , i am using code snippets plugins, thus no need php tags.

    2. i am keen to look into the hooks too. its been sometime i mess with GP 🙂 .is this something new recently added ?

    #683143
    David
    Staff
    Customer Support

    PHP tags are required within the code when you are adding HTML. The first ?> is closing the PHP to allow for the HTML. Subsequent PHP then needs to be wrapped again. Try this instead:

    add_action( 'generate_inside_mobile_header','add_login_logout_link' );
    function add_login_logout_link() {
      if ( is_user_logged_in() ) {
    		echo '<i class="fal fa-user-circle"></i><a href="'.wp_logout_url().'" title="Logout">Logout</a>';
    	} else {
    		echo '<i class="fal fa-user-circle"></i><a href="'.wp_login_url().'" title="Login">Login</a>';
    }
    }

    Yes, Elements came with the 1.7 update.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.