如何在 WordPress 的 href 上调用 PHP 函数? [英] How to call a PHP function on href in WordPress?

查看:30
本文介绍了如何在 WordPress 的 href 上调用 PHP 函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能.当用户单击超链接(取消激活我的帐户)时,我想调用此功能.在 href 点击时调用函数的最佳方法是什么?谢谢

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

推荐答案

正如 Thorben 所提到的,您不能在浏览器事件上执行 PHP 函数,因为该语言是服务器端而不是客户端.但是,有几种方法可以解决此问题:

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:

您不能在浏览器事件(例如链接点击)上调用 PHP 函数,但您可以在您点击超链接时加载的页面上首先调用它 --我们称它为next.php".为此,请在next.php"的最顶部有条件地调用您的函数 deleteUserMeta().问题是您需要将一些变量传递给此页面,以便您可以检查条件并执行函数.使用 GET 通过超链接传递变量,如下所示:

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>

您想如何传递 userId 由您决定.在上面的例子中,它是硬编码的,但你也可以像这样用 PHP 以某种方式设置它:

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>

现在在next.php"上,使用条件来评估该变量:

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

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

2.客户端 (AJAX)

执行此操作的另一种方法是在客户端使用一些 AJAX 执行此操作.如果您不熟悉 Javascript/jQuery/AJAX,我可能会坚持使用其他解决方案,因为它可能更容易实现.如果您已经在使用 jQuery,这应该不会太难.使用 jQuery,您可以将此函数绑定到超链接的实际单击事件.这样,整个操作就可以在不刷新页面的情况下进行:

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>

现在你需要做两件事:1. 第一个解决方案所需的相同 PHP 文件next.php"只包含对您的函数的调用,deleteUserMeta($userId)2. 一个名为ajaxDeleteUserMeta()的javascript函数,所以在你的JS中像这样创建一个:(因为这是一个命名函数,它不需要进入jQuery(document).ready(function(){}); 就像大多数匿名 jQuery 函数一样.)

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.

这篇关于如何在 WordPress 的 href 上调用 PHP 函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆