- This topic has 3 replies, 2 voices, and was last updated 5 years, 7 months ago by David.
-
AuthorPosts
-
September 21, 2018 at 8:05 am #683091sg
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> }
September 21, 2018 at 8:29 am #683116DavidStaffCustomer SupportHi 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/
September 21, 2018 at 8:38 am #683126sgThank 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 ?
September 21, 2018 at 9:06 am #683143DavidStaffCustomer SupportPHP 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.
-
AuthorPosts
- You must be logged in to reply to this topic.