簡體   English   中英

如何在自定義插件中單擊時調用wordpress函數

[英]how to call wordpress function on click in a custom plugin

我一直在努力創建自定義插件(下面的簡化代碼)。

我有我的插件文件(counter.php),其中有2個功能

displayCount() {
// output count and a link to add 1
echo '<a href="XXX">Add One</a>';

addOne() {
$count = $count + 1;

}

我的問題是用XXX替換什么,或者如何從帖子頁面調用addOne函數?

您正在嘗試在用戶端修改PHP,但是不幸的是,當用戶能夠單擊鏈接時,所有PHP都已運行。 PHP是服務器端語言,Javascript是客戶端語言。 為了來回傳遞數據,您將需要Ajax。

但是,您似乎想用純Javascript完成的工作。 像這樣:

<div id="counter"></div>
<a href="#" class="counter-add">Add one</a>

<!-- INCLUDE JQUERY HERE (or elsewhere on page, perhaps head) -->
<script type="text/javascript">
    // Create global JS var to track the count.
    var counter = 0;

    // On document ready we need to assign a click event.
    // We use document ready to be sure that we select all
    // initial elements when the page is loaded.
    jQuery('document').ready(function()
    {
        // On counter-add click...
        jQuery('.counter-add').click(function()
        {
            // Increment counter
            counter++;

            // Output counter value
            jQuery('#counter').text(counter);
        });
    });
</script>

這是一個有效的JSBIN副本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM