How to call a PHP function on href in WordPress?

I have following function. I want to call this function when user click on Hyperlink (unactivate my account). what is the best way to call function on href click? thanks

function deleteUserMeta($userID) {
    delete_usermeta($userID, 'subscription_id');
    delete_usermeta($userID, 'ref_id');
    delete_usermeta($userID, 'users_name');
    delete_usermeta($userID, 'trans_key');
}

Related posts

Leave a Reply

3 comments

  1. As Thorben mentioned, you can’t execute PHP functions on browser events, since the language is server-side and not client-side. However, there are a couple of ways to address this:

    1. SERVER-SIDE (PHP)

    You can’t call a PHP function on a browser event (link click, for example), but you can call it first thing on the page that’s loaded when you click though the hyperlink — let’s call it ‘next.php’. To do this, call your function deleteUserMeta() conditionally, at the very top of ‘next.php’. The catch is you’ll need to pass some variables to this page so that you can check the condition, and execute the function. Use GET to pass a variable through the hyperlink like this:

    <a href="next.php?unsubscribe=true&userID=343">Unsubscribe</a>
    

    How you want to pass the userId is up to you. In the above example it’s hard-coded, but you might also somehow set it with PHP like so:

    <a href="next.php?unsubscribe=true&userID=<?php echo $userID;?>">Unsubscribe</a>
    

    Now on ‘next.php’, use a condition to assess that variable:

    <?php
    if($_REQUEST['unsubscribe'] == 'true'){
        deleteUserMeta($_REQUEST['userID']);
    }
    ?>
    

    2. CLIENT-SIDE (AJAX)

    The other way to perform this operation is to do it client-side with some AJAX. If you’re not familiar with Javascript/jQuery/AJAX I would probably stick to the other solution, as it’s probably easier to implement. If you’re using jQuery already though, this shouldn’t be too hard. With jQuery, you can bind this function to the actual click event of your hyperlink. This way, this entire operation can happen without refreshing the page:

    <a href="#" onclick="ajaxDeleteUserMeta()">Unsubscribe/a>
    

    Now you’ll need two things:
    1. The same PHP file “next.php” that was required for the first solution just contains a call to your function, deleteUserMeta($userId)
    2. A javascript function called ajaxDeleteUserMeta(), so create one in your JS like this: (since this is a named function, it doesn’t need to go inside jQuery(document).ready(function(){}); like most anonymous jQuery functions do.)

    function ajaxDeleteUserMeta(){
    
    var userID = ''; // fill this in to somehow acquire the userID client-side...
    
    jQuery.ajax({
       type: "POST",
       url: "next.php", /* this will make an ajax request to next.php, which contains the call to your original delete function. Essentially, this ajax call will hit your original server-side function from the client-side.*/
       data: "userID="+userID+"&unsubscribe=true", /*here you can pass a POST variable to next.php that will be interpreted by the conditional function.*/
       success: function(msg){
         alert( "Data Saved: User ID " + userID + " deleted." );
       }
    
    });
    
    }
    

    Long-winded, but I hope some of that makes a little sense.

  2. This is PHP code? There is no way you can directly call a PHP function from a browser.
    You can try to append a GET or POST variable to your request, read it in your PHP and then eventually execute above function.

  3. You could do a post using ajax to a URL that just calls that method.

    Example using jQuery:
    onclick="$.post('http://yourdomain/delete_user?userid',callBackFunction());"

    then in your php have that url mapped to your php function. I have never used PHP or wordpress so I don’t know how you do that but it should be straight forward as its a common case.