2016-08-01 9 views
-5

Ich möchte folgen System hinzufügen meine WordPress-Website, aber nicht verbinden Ajax. Wie kann ich meinem Standortsystem folgen? Ich versuche es lange, kann mich aber nicht mit Ajax verbinden.Wie benutze ich diesen Code mein Wordpress-Theme

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

Taste Funktionscode

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

ich möchte es author.php und single.php

+2

Wo ist Ihr JS-Code für Ajax? – Mickey

+0

Fügen Sie meinen Js-Code hinzu, sehen Sie es bitte –

+0

Wenn Aktion 'ipin-follow' ist, dann müssen Sie mit do_action umgehen ('wp_ajax_ipin-follow', 'your_callback'); – Mickey

Antwort

0

Ich bin in der AJAX-Aufruf fehlen Ihre Code. Etwas wie folgt aus:

Anfrage = $ Schnipsel ({ url: "/sendmail.php", Typ: "post", Daten: serializedData });