How to call PHP function on href in WordPress?

I have the following function. I want to call this function when a user clicks on a hyperlink (disables my account). What is the best way to call a function when href is pressed? thanks

function deleteUserMeta($userID) { delete_usermeta($userID, 'subscription_id'); delete_usermeta($userID, 'ref_id'); delete_usermeta($userID, 'users_name'); delete_usermeta($userID, 'trans_key'); } 
+4
source share
3 answers

As Torben mentioned, you cannot perform PHP functions in browser events, since the language is server-side, not client-side. However, there are several ways to solve this problem:

1. SERVER-SIDE (PHP)

You cannot call the PHP function in a browser event (for example, click the link), but you can call it the first thing on the page loaded when you click on the hyperlink - let it call next_php ". For this, call the deleteUserMeta() function conditionally, at the very top parts of 'next.php'. Trap - you need to pass some variables to this page so you can check the condition and execute the function. Use GET to pass the variable through a hyperlink as follows:

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

How you want to pass userId is up to you. In the above example, it is hardcoded, but you can also somehow install it using PHP as follows:

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

Now in 'next.php' use the condition to evaluate this variable:

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

2. CLIENT PARTY (AJAX)

Another way to perform this operation is to do it on the client side with some AJAX. If you are not familiar with Javascript / jQuery / AJAX, I would probably stick with a different solution, as it is probably easier to implement. If you are using jQuery already, this should not be too complicated. With jQuery, you can bind this function to the actual click event of your hyperlink. Thus, all this operation can happen without refreshing the page:

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

Now you need two things: 1. The same PHP file β€œnext.php” that was needed for the first solution just contains a call to your function, deleteUserMeta($userId) 2. A javascript function called ajaxDeleteUserMeta() , so create it in its JS as follows: (since it is a named function, it does not need to go inside jQuery(document).ready(function(){}); as 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-term, but I hope some of them make little sense.

+10
source

You can make a message using ajax for the url that just calls this method.

An example of using jQuery:
onclick="$.post('http://yourdomain/delete_user?userid',callBackFunction());"
then in your php there is a URL mapped to your php function. I have never used PHP or wordpress, so I don’t know how you do it, but it should be straight forward, as a normal case.

+1
source

Is this PHP code? You cannot directly call a PHP function from a browser. You can try adding a GET or POST variable to your request, read it in your PHP, and then ultimately execute the above function.

+1
source

Source: https://habr.com/ru/post/1344904/


All Articles