2016-08-01 4 views
-5

私のシステムに私のワードプレスサイトを追加したいが、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コード

//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使うと

+2

あなたのJSコードはどこですか? – Mickey

+0

私のjsコードを追加してください。 –

+0

アクションが 'ipin-follow'であれば、do_action( 'wp_ajax_ipin-follow'、 'your_callback')を処理する必要があります。 – Mickey

答えて

0

single.php私はあなたにAJAX呼び出しを欠けているしたいですコード。 このような何か:

要求= $アヤックス({ URL: "/sendmail.php"、 タイプ: "POST"、 データ:serializedData })。

関連する問題