skip to Main Content

I have a link on a member site that after clicked js will disable it but upon refresh the link becomes active again. I would like the link to remain disabled permanently for the user after the one click. I would like to create php code that will check to see if the user has clicked the link before and disable it if they have.

I’m attempting to use user meta created based on the "disabled" class being on the link (which only happens via js after it’s clicked). If $clicked is false, they have not clicked, the code will add user meta information. If $clicked is true then the class "disabled" is added to the ‘a’ tag (to disable the link again). I am using wordpress mysql for a database.

There might be a better way to do this. Including my js code and php code. Your help is appreciated.

Js code:

$("#no-link").click(function (event) {
if ($(this).hasClass("disabled")) {
    event.preventDefault();
}
$(this).addClass("disabled");
});

PHP code:

$user_id = get_current_user_id();
$key = 'did_user_click';
$clicked = 1;

function firstClick(){
if (get_user_meta ($key) == '0'){
    add_user_meta ( $user_id, $key, $clicked );
} else {
    if(get_field('invite_button_join > a')){
        echo "disabled";
    }
  }
}
if (isset($_GET[ 'disabled' ])){
firstClick();
}

2

Answers


  1. You can use the $_SESSION or $_COOKIE global variables, but the recommendation for this is to use $_SESSION

    Login or Signup to reply.
  2. There are different ways to approach this. If you want to keep it permanently disabled, the only way to ensure this would be storing this status in a database (and working with authenticated users, of course).

    A more temporary way to achieve what you want with PHP, is storing the click status in a session variable. By default, the PHP session will expire when the user closes the browser or after a specific time.

    In this alternative, you would be running this PHP code once the user clicks:

    session_start();
    $_SESSION['did_user_click']  = True;
    

    And the variable "clicked" could be declared consulting this session variable:

    $clicked = False
    if($_SESSION['did_user_click']){
       $clicked = True
    }
    

    The third way to do this, is setting the click status inside the user’s localStorage. This method would survive everything except for refreshed caches. When the user clicks, you should run the following code:

    localStorage.setItem('did_user_click', true)
    

    And you would be validating the click status with javascript:

    var clicked = false
    if(localStorage.getItem('did_user_click') === "true"){
       clicked = true
    }
    

    Note that this javascript approach will only be attached to the device the user is using. If you are trying to work with authenticated users, and bind the click status to the user in a permanent way, independently of which device they are using, you should consider using a database as I suggested earlier.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search