0

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 2

1

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.
Sign up to request clarification or add additional context in comments.

2 Comments

How did you get WordPress to read your php file that you created with the custom function?
0

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']);

1 Comment

Hi @billybadass !! Thanks for your answer. My problem is not the code, but the way to implement it. I’m working with Elementor CMS so I used a custom html block to place this code. However this is not a valid format because I can see the <?php opening tags as plain text…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.