Archived topic
generate_inside_mobile_header
3 replies · Started by sg on September 21, 2018
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>
}
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/
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 ?
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.