如何创建执行函数的表单按钮?

时间:2015-12-17 作者:Manu

考虑以下示例:我们构建了一个会议网站(可以是WordCamp),允许演讲者提交演讲,并由陪审团决定接受哪些演讲。

要在WordPress中处理此问题,我们只需使用:

“talk”自定义帖子类型是一种“talk status”分类法,术语为“accepted”/“rejected”下一步是在前端构建一个管理视图,列出所有待处理的对话(这是最简单的部分),并提供action buttons 接受或拒绝谈话(这是不太容易的部分)。

这是该页面的模型:

enter image description here

当陪审团用户点击“接受”时会发生什么?

应执行类似的函数:

<?php 
wp_set_object_terms( 
  $talk_id, // ID of the selected talk, submitted by form
  $talk_status, // term: "accepted", submitted by form
  \'talk-status\', // $taxonomy, 
  false // $append 
);
它的作用:它将术语“accepted”(来自“talk status”分类法)附加到所选的对话。

Now, the question: 如何从表单按钮触发这样一个函数,以及如何将我们的两个值($talk\\u id,$talk\\u status)传递给函数?

我想讲一个最简单的工作示例。我知道there are some similar questions 已经但我认为有另一个清晰而简单的例子会很有用,从2015年底开始使用标准的WordPress方法。

1 个回复
最合适的回答,由SO网友:jgraup 整理而成

这是一个在前端呈现一些项目的简单示例,这些项目使用AJAX触发后端的更改,并根据该响应更新UI。

还有其他examples 让脚本排队,所以只需阅读AJAX,并将其视为一个高级视图。

REGISTER AJAX/SCRIPTS/CSS

add_action(\'init\', function () {

    // Register AJAX handlers

    add_action(\'wp_ajax_set_pending_item_state\', \'set_pending_item_state\');
    add_action(\'wp_ajax_nopriv_set_pending_item_state\', \'set_pending_item_state\');

    // AJAX handler (PRIV / NO PRIV)

    function set_pending_item_state()
    {
        if( empty ($_POST[\'action\']) || $_POST[\'action\'] !== \'set_pending_item_state\') {
            if (!empty ($fail_message)) {
                wp_send_json_error(array(
                    \'message\' => "Sorry!"
                )); // die
            }
        }

        $id = $_POST[\'id\'];
        $state = $_POST[\'state\'];

        wp_send_json_success(array(
            \'action\' => $_POST[\'action\'],
            \'message\' => \'State Was Set To \' . $state,
            \'state\' => $state,
            \'ID\' => $id
        )); // die
    }
});

add_action(\'wp_footer\', function () { ?>

    <style>
        .pending-form,
        .pending-list,
        .pending-list-item {
            display: block;
            color:grey;
        }

        .pending-accept {
            color:green;
        }
        .pending-reject {
            color:red;
        }

        .pending-list-item[data-state="0"] {
            color:inherit;
        }
        .pending-list-item[data-state="-1"] .title {
            color:red;
        }
        .pending-list-item[data-state="1"] .title {
            color:green;
        }
    </style>
    <script>
        (function ($) {

            var $form = $(\'.pending-form\'),
                $item = $form.find( \'.pending-list-item\');

            // Trigger to make AJAX call to set state for ID
            // ( 1:accept, -1:reject )
            function setState(id, state) {

                // item clicked
                var $item = $(\'.pending-list-item[data-id="\' + id + \'"]\'),

                // gather data
                    data = {
                        action: \'set_pending_item_state\',
                        id:      id,
                        state:   state
                    };

                // make AJAX POST call    
                $.ajax({
                    type: \'POST\',
                    url: ajaxurl,
                    data: data,
                    success: function (response) {

                        // look at the response

                        if (response.success) {

                            // update the UI to reflect the response
                            $item.attr (\'data-state\', state);

                            // succcess data
                            console.log(response.data);

                        } else {

                            // no good
                            console.log(response);
                        }
                    }
                });
            }

            // setup the items
            $item.each (function (inx, item){

                var $item = jQuery(item),
                    $acceptBtn = $item.find (\'.pending-accept\'),
                    $rejectBtn = $item.find (\'.pending-reject\');

                // setup the button click handlers

                $acceptBtn.on (\'click\', function(){
                    var id = $item.attr (\'data-id\');
                    setState( id, 1);
                });

                $rejectBtn.on (\'click\', function(){
                    var id = $item.attr (\'data-id\');
                    setState( id, -1);
                });
            });

        })(jQuery);
    </script>
<?php });

RENDER FORM

<div class="pending-form">
<?php
    $pending_talk_list = array(
        array( \'id\' => \'313\',  \'title\'=> \'This is a title1\', \'speaker\' => \'some person1\', \'abstract\' => \'12sdlfjskdfjsdfl\', \'state\' => 0),
        array( \'id\' => \'252\',  \'title\'=> \'This is a title2\', \'speaker\' => \'some person1\', \'abstract\' => \'23sdlfjskdfjsdfl\', \'state\' => 0),
        array( \'id\' => \'344\',  \'title\'=> \'This is a title3\', \'speaker\' => \'some person2\', \'abstract\' => \'45sdlfjskdfjsdfl\', \'state\' => 0),
        array( \'id\' => \'421\',  \'title\'=> \'This is a title4\', \'speaker\' => \'some person3\', \'abstract\' => \'67sdlfjskdfjsdfl\', \'state\' => 0),
    );

    echo "<h1>Pending Talks</h1>";

    echo "<ul class=\'pending-list\'>"; // pending list

    foreach($pending_talk_list as $inx => $talk ) {

        $title = $talk[\'title\'];
        $speaker = $talk[\'speaker\'];
        $abstract = $talk[\'abstract\'];
        $state = $talk[\'state\'];
        $id = $talk[\'id\'];

        echo "<li class=\'pending-list-item\' data-state=\'{$state}\' data-id=\'{$id}\'><div class=\'title\'>{$title}</div><div class=\'speaker\'>{$speaker}</div><div class=\'abstract\'>{$abstract}</div><a class=\'pending-accept\'>accept</a><a class=\'pending-reject\'>reject</a></li>";
    }

    echo "</ul>"; // end pending list
?>
</div>

相关推荐

WordPress AJAX错误400向远程站点发送数据的错误请求

我正在使用发件人。net获取电子邮件订阅列表。这个网站给了我一些信息,可以将用户的电子邮件添加到订阅列表中。我想使用WordPress ajax来实现这一点。但它返回错误400错误请求。我的代码是:文件ajax新闻脚本。js公司: jQuery(document).ready(function($){ // Perform AJAX send news on form submit $(\'form#fnews\').on(\'submit\', funct