Identify Visitor and User in Wordpress

Asked

Viewed 1,181 times

1

I am working on a wordpress plugin, and I need to change the menu name Login for My account when the user is logged in.

I also need to change the title of the page. I have tried using the filter below, but ends up changing all the links of the site.

add_filter('the_title', 'function_name');

and when changing the menu name, I found nothing about it, someone has some suggestion about something?

Thanks in advance...

THE CODE

Trying to add a shortcode to be used as above!

if ( !function_exists('custom_login_form') )
{
    function custom_login_form()
    {
        $args=array(
            "echo" => false,
            "label_log_in" => "Entrar",
            "remember"  => true,
        );
        if ( !is_user_logged_in() ){
            return wp_login_form($args);
        }else{
            return 'Logado em Minha conta! <br />' . wp_loginout('index.php');
        }
    }
}
add_shortcode('login_form', 'custom_login_form');

This works perfectly, I would now like to make the changes in the menu and the title of the page!

RESOLUTION

After many researches managed to find a solution to the problem, which by coincidence I had already tried and did not work at all.

Using the following filter.

add_filter('wp_nav_menu_items', 'function_name', 10, 2);
function func_name_wp_nav($items, $args){
    return $items;
}

Then you adding items according to the menu, which is usually LI it adds as last element, example.

$item .= "<li><a href='#'>Item 1</a></li>";

This worked perfectly for me! Any questions regarding the post, comment and I will try to answer in what you know.

Thank you...

1 answer

1

You must be looking to use the function is_user_logged_in(), if the user is logged in the return value is true, otherwise it is phony.

if ( is_user_logged_in() ) {
    // Fazer algo quando esse usuário estiver logado
} else {
    // Fazer algo caso seja um visitante
}

On the second question, the function the_title should work, however if you prefer to use something else clean, use the_title_attribute, both have practically the same goal, however, the latter has one more parameter that is $post which makes it possible to specify the ID or object of a post, can obtain or change, by default the current post is searched.

To change the menu name, here (Changing Admin Menu Labels - Wordpress Development SE) have a solution that maybe works for you.

  • @Qmechanic73 The function is_user_logged_in() is correct, I was already trying with it, but what I can not do is change the menu and the title of the page correctly, when changed the title of the page, is changed all other links as well. And when changing the link in the main bar, I could not and the link you passed is related to the admin menus. Thank you!

  • @Italoizaac Let’s split =D. The title, try to make a conditional expression using in_the_loop() to check whether the context of the_title(since the_title_attribute seems not to be needed here) is inside the loop.

  • @Qmechanic Using the in_the_loop() worked perfectly modifying the title of the page without modifying other links of the same. Now the second part is "MACABRA", rsrs, thanks in advance...

  • The same idea passed by the post is the same logic for this here, but the solution presented does not work and is apparently incorrect.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.