Join our new community forum for support & discussion

Join Now

Home Support Themes ListingHive Redirect if not user not logged in

Redirect if not user not logged in

  • Author
    Posts
  • Iva

    Hello,
    I’m using this code snippet for a call button:
    📞
    I would like that if user who is not logged in click the button it redirects him to log in page (like it does when they want to send a message). I believe it has to be done with user_login_modal but I’m not that much into coding. Could you please help me with that? Thanks.

    Iva

    <a class="button button--primary hp-button--wide" href="tel:%value%" target="_blank" rel="nofollow">📞</a>
    Sorry, this code

    yevhen developer

    If you add this code snippet as part of template changes with some HivePress hooks then you need to add condition if ( is_user_logged_in(){... to check if user is logged in. If user is logged in then you show current code. If user is not logged in then you show this code snippet <a class="button button--primary hp-button--wide" href="#user_login_modal" target="_blank" rel="nofollow">📞</a>

    Iva

    Thank you for your answer!
    It works fine – when user is not logged in and clicks to call the log in page appears. But when the user is logged in and clicks to call it just refreshes the page.

    yevhen developer

    If you make conditions in this way

    if ( is_user_logged_in(){
        // Your custom code here which you have post above
    }else{
       // Code to open log in form
    }

    then if user is logged in user should see this code

    <a class="button button--primary hp-button--wide" href="tel:%value%" target="_blank" rel="nofollow">📞</a>

    and if user is not logged in user should see this code

    <a class="button button--primary hp-button--wide" href="#user_login_modal" target="_blank" rel="nofollow">📞</a>

    Iva

    I also tried that way but in that case two buttons and some code appear – one that calls and one that opens log in page. Maybe I’m not inserting the code in the right place? hivepress/templates/listing/view/block/listing-attributes-primary.php
    Thank you for your help!

    yevhen developer

    Please try hivepress/v1/templates/listing_view_block hook to make changes in listing block.
    Also please check this tutorial how to customise templates https://youtu.be/LkojYp-8uwY

    Please do not make changes in core files

    Iva

    I watched the tutorial and read in the forum but still unable to figure it out.
    However, thanks for your replies.

    yevhen developer

    Please try to put this code snippet in hivepress/templates/listing/view/block/listing-attributes-primary.php with child theme

    <?php if ( is_user_logged_in() ) :
    		?>
    			<a class="button button--primary hp-button--wide" href="tel:%value%" target="_blank" rel="nofollow">📞</a>
    	<?php else : ?>
    			<a class="button button--primary hp-button--wide" href="#user_login_modal" target="_blank" rel="nofollow">📞</a>
    	<?php endif; ?>
    Iva

    That’s it. Thank you!

Viewing 10 posts - 1 through 10 (of 10 total)

New Reply

This forum has been archived and is no longer accepting new posts or replies. Please join our new community forum for support & discussion.