如何使用这个代码我的wordpress主题

时间:2016-08-01 10:00:39

标签: php jquery ajax wordpress

我想跟随系统添加我的WordPress网站但不能连接ajax。我该如何关注我的网站系统。我试了很长时间,但无法连接到Ajax。

// User Follow Function Wordpress
function bg_follow() {
   $nonce = $_POST['nonce'];

if (!wp_verify_nonce($nonce, 'ajax-nonce'))
    die();

do_action('bg_before_follow', $_POST);

global $wpdb, $user_ID, $user_identity;
$author_id = $_POST['author_id'];

if ($_POST['bg_follow'] == 'follow') {
    //update usermeta following for current user
    $usermeta_following_count = get_user_meta($user_ID, '_Following Count', true);
    $usermeta_following_user_id = get_user_meta($user_ID, '_Following User ID');
    $following_user_id = $usermeta_following_user_id[0];

    if (!is_array($following_user_id))
        $following_user_id = array();

    if (!in_array($author_id, $following_user_id)) {
        array_unshift($following_user_id, $author_id);
        update_user_meta($user_ID, '_Following User ID', $following_user_id);
        update_user_meta($user_ID, '_Following Count', ++$usermeta_following_count);
    }

} else if ($_POST['bg_follow'] == 'unfollow') {     
    //update usermeta following for current user
    $usermeta_following_count = get_user_meta($user_ID, '_Following Count', true);
    $usermeta_following_user_id = get_user_meta($user_ID, '_Following User ID');
    $following_user_id = $usermeta_following_user_id[0];

        unset($following_user_id[array_search($author_id, $following_user_id)]);
        $following_user_id = array_values($following_user_id);

        update_user_meta($user_ID, '_Following User ID', $following_user_id);
        update_user_meta($user_ID, '_Following Count', --$usermeta_following_count);

        //update usermeta followers for author
        $usermeta_followers_count = get_user_meta($author_id, '_Followers Count', true);

        $usermeta_followers_id = get_user_meta($author_id, '_Followers User ID');
        $followers_id = $usermeta_followers_id[0];
        unset($followers_id[array_search($user_ID, $followers_id)]);
        $followers_id = array_values($followers_id);

        update_user_meta($author_id, '_Followers User ID', $followers_id);
        update_user_meta($author_id, '_Followers Count', --$usermeta_followers_count);

        echo 'unfollow_all';
}

do_action('bg_after_follow', $user_ID, $author_id);

exit;
     } add_action('wp_ajax_bg-follow', 'bg_follow');

按钮功能代码

<?php if ($user_info->ID != $user_ID) { ?>
                <button class="btn btn-success btn-sm follow bg-follow" data-author_id="<?php echo $user_info->ID ?>" type="button"><?php if (!$followed) { _e('Follow', 'bg'); } else { _e('Unfollow', 'bg'); } ?></button>

        <?php } ?>

Js Code

//follow for lightbox, posts, author
$(document).on('click', '.ipin-follow', function() {
    if (obj_ipin.u != '0') {
        var follow = $(this);
        var board_parent_id = follow.data('board_parent_id');
        var board_id = follow.data('board_id');
        var author_id = follow.data('author_id');
        var disable_others = follow.data('disable_others');
        follow.attr('disabled', 'disabled');

        if (!follow.hasClass('disabled')) {
            var data = {
                action: 'ipin-follow',
                nonce: obj_ipin.nonce,
                ipin_follow: 'follow',
                board_parent_id: board_parent_id,
                board_id: board_id,
                author_id: author_id,
                disable_others: disable_others
            };

            $.ajax({
                type: 'post',
                url: obj_ipin.ajaxurl,
                data: data,
                success: function() {
                    if (follow.data('board_parent_id') != 0) {
                        follow.addClass('disabled').text(obj_ipin.__UnfollowBoard).removeAttr('disabled');
                    } else {
                        follow.addClass('disabled').text(obj_ipin.__Unfollow).removeAttr('disabled');
                    }

                    //increase followers count in author.php
                    if ($('#ajax-follower-count') && follow.parent().parent().parent().parent().attr('id') == 'userbar') {
                        $('#ajax-follower-count').html(parseInt($('#ajax-follower-count').html(), 10)+1);
                    }

                    //disable other follow button
                    if (board_parent_id == '0' && (disable_others != 'no' || $('#userbar .nav li:first').hasClass('active'))) {
                        $('.ipin-follow').each(function() {
                            if ($(this).data('board_parent_id') != 0) {
                                $(this).addClass('disabled').text(obj_ipin.__UnfollowBoard);
                            }
                        });
                    }
                }
            });
        } else {                        
            var data = {
                action: 'ipin-follow',
                nonce: obj_ipin.nonce,
                ipin_follow: 'unfollow',
                board_parent_id: board_parent_id,
                board_id: board_id,
                author_id: author_id
            };

            $.ajax({
                type: 'post',
                url: obj_ipin.ajaxurl,
                data: data,
                success: function(data) {
                    if (follow.data('board_parent_id') != 0) {      
                        follow.removeClass('disabled').text(obj_ipin.__FollowBoard).removeAttr('disabled');
                    } else {
                        follow.removeClass('disabled').text(obj_ipin.__Follow).removeAttr('disabled');
                    }

                    //decrease followers count in author.php
                    if ($('#ajax-follower-count') && follow.parent().parent().parent().parent().attr('id') == 'userbar') {
                        $('#ajax-follower-count').html(parseInt($('#ajax-follower-count').html(), 10)-1);
                    }

                    //enable other follow button
                    if (data == 'unfollow_all' && (disable_others != 'no' || $('#userbar .nav li:first').hasClass('active'))) {
                        $('.ipin-follow').each(function() {
                            if ($(this).data('board_parent_id') != 0) {
                                $(this).removeClass('disabled').text(obj_ipin.__FollowBoard);
                            }
                        });
                    }
                }
            });
        }
        return false;
    } else {
        loginPopup();
        return false;
    }
});

我想用它作者:author.php和single.php

1 个答案:

答案 0 :(得分:0)

我在你的代码中错过了AJAX调用。 像这样:

request = $ .ajax({         url:&#34; /sendmail.php",         输入:&#34; post&#34;,         data:serializedData     });