skip to Main Content

I’m trying to change an element in the WP Front End accordingly with a previous validation (if the user is logged in). Most concretly my code would be something like this:

<?php

    if ( is_user_logged_in() ) {
    // html button
    } else {
       // different html button
    }
?>

To make it work I thought that a HTML block from WP would do the trick however is not working.
Does anyone know how can I achieve this?

Thanks in advance

2

Answers


  1. Chosen as BEST ANSWER

    Finally I was able to solved it. I share my solution:

    1. I created a php file with a custom function with the code in the same folder that my functions.php file. (I think it wasn’t necessary but I prefer to keep code independent)
    2. I created a shortcode with the function I refer in the step 1.
    3. Finally, I used the shortcode block to place the code in my front end.

  2. It actually works. I’ve just tried it. Just make sure you have a session you are logged in and one in a private window that you are not logged in. Besides that you can also check (after ensure they are logged in, to avoid errors) what kind of role the user is and decide accordingly.

    $get_user_info = wp_get_current_user();
    $role = $get_user_info->roles[0];
    in_array($role, ['administrator', 'otherCustomRole']);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search