Wordpress - JS to select only the current user i clicked to follow
I'm using "User Follow System" plugin, and i would like to know how to make this js select only one user of the current user i clicked to follow.
I am showing a list of followers and following using get_users in foreach loop.
The problem I am facing is when i click on the follow link for one user, the loading images for all users show up and all the follow links get toggled. $(‘.follow-links a’).toggle();
When i asked the plugin owner he said: Sounds like you may just need to adjust the selector used in the JS to target only the one you clicked on. This was originally built with only one user’s links displayed on the page.
But sorry, I failed to figure it out!
jQuery(document).ready(function($) {
/*******************************
follow / unfollow a user
*******************************/
$( '.follow-links a' ).on('click', function(e) {
e.preventDefault();
var $this = $(this);
if( pwuf_vars.logged_in != 'undefined' && pwuf_vars.logged_in != 'true' ) {
alert( pwuf_vars.login_required );
return;
}
var data = {
action: $this.hasClass('follow') ? 'follow' : 'unfollow',
user_id: $this.data('user-id'),
follow_id: $this.data('follow-id'),
nonce: pwuf_vars.nonce
};
$('img.pwuf-ajax').show();
$.post( pwuf_vars.ajaxurl, data, function(response) {
if( response == 'success' ) {
$('.follow-links a').toggle();
} else {
alert( pwuf_vars.processing_error );
}
$('img.pwuf-ajax').hide();
} );
});
});
display-function.php
<?php
/**
* Retrieves the follow / unfollow links
*
* @access public
* @since 1.0
* @param int $user_id - the ID of the user to display follow / unfollow links for
* @return string
*/
function pwuf_get_follow_unfollow_links( $follow_id = null ) {
global $user_ID;
if( empty( $follow_id ) )
return;
if( ! is_user_logged_in() )
return;
if ( $follow_id == $user_ID )
return;
ob_start(); ?>
<div class="follow-links">
<?php if ( pwuf_is_following( $user_ID, $follow_id ) ) { ?>
<span><a href="#" class="unfollow followed" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>"><span>Following</a></span>
<a href="#" class="follow" style="display:none;" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>">Follow</a>
<?php } else { ?>
<a href="#" class="follow" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>">Follow</a>
<span><a href="#" class="followed unfollow" style="display:none;" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>"><span>Following</a></span>
<?php } ?>
<img src="<?php echo PWUF_FOLLOW_URL; ?>/images/loading.svg" class="pwuf-ajax" style="display:none;"/>
</div>
<?php
return ob_get_clean();
}
javascript wordpress
add a comment |
I'm using "User Follow System" plugin, and i would like to know how to make this js select only one user of the current user i clicked to follow.
I am showing a list of followers and following using get_users in foreach loop.
The problem I am facing is when i click on the follow link for one user, the loading images for all users show up and all the follow links get toggled. $(‘.follow-links a’).toggle();
When i asked the plugin owner he said: Sounds like you may just need to adjust the selector used in the JS to target only the one you clicked on. This was originally built with only one user’s links displayed on the page.
But sorry, I failed to figure it out!
jQuery(document).ready(function($) {
/*******************************
follow / unfollow a user
*******************************/
$( '.follow-links a' ).on('click', function(e) {
e.preventDefault();
var $this = $(this);
if( pwuf_vars.logged_in != 'undefined' && pwuf_vars.logged_in != 'true' ) {
alert( pwuf_vars.login_required );
return;
}
var data = {
action: $this.hasClass('follow') ? 'follow' : 'unfollow',
user_id: $this.data('user-id'),
follow_id: $this.data('follow-id'),
nonce: pwuf_vars.nonce
};
$('img.pwuf-ajax').show();
$.post( pwuf_vars.ajaxurl, data, function(response) {
if( response == 'success' ) {
$('.follow-links a').toggle();
} else {
alert( pwuf_vars.processing_error );
}
$('img.pwuf-ajax').hide();
} );
});
});
display-function.php
<?php
/**
* Retrieves the follow / unfollow links
*
* @access public
* @since 1.0
* @param int $user_id - the ID of the user to display follow / unfollow links for
* @return string
*/
function pwuf_get_follow_unfollow_links( $follow_id = null ) {
global $user_ID;
if( empty( $follow_id ) )
return;
if( ! is_user_logged_in() )
return;
if ( $follow_id == $user_ID )
return;
ob_start(); ?>
<div class="follow-links">
<?php if ( pwuf_is_following( $user_ID, $follow_id ) ) { ?>
<span><a href="#" class="unfollow followed" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>"><span>Following</a></span>
<a href="#" class="follow" style="display:none;" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>">Follow</a>
<?php } else { ?>
<a href="#" class="follow" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>">Follow</a>
<span><a href="#" class="followed unfollow" style="display:none;" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>"><span>Following</a></span>
<?php } ?>
<img src="<?php echo PWUF_FOLLOW_URL; ?>/images/loading.svg" class="pwuf-ajax" style="display:none;"/>
</div>
<?php
return ob_get_clean();
}
javascript wordpress
Just replace$('.follow-links a').toggle()with$this.toggle()
– Callam
Jan 19 at 18:28
add a comment |
I'm using "User Follow System" plugin, and i would like to know how to make this js select only one user of the current user i clicked to follow.
I am showing a list of followers and following using get_users in foreach loop.
The problem I am facing is when i click on the follow link for one user, the loading images for all users show up and all the follow links get toggled. $(‘.follow-links a’).toggle();
When i asked the plugin owner he said: Sounds like you may just need to adjust the selector used in the JS to target only the one you clicked on. This was originally built with only one user’s links displayed on the page.
But sorry, I failed to figure it out!
jQuery(document).ready(function($) {
/*******************************
follow / unfollow a user
*******************************/
$( '.follow-links a' ).on('click', function(e) {
e.preventDefault();
var $this = $(this);
if( pwuf_vars.logged_in != 'undefined' && pwuf_vars.logged_in != 'true' ) {
alert( pwuf_vars.login_required );
return;
}
var data = {
action: $this.hasClass('follow') ? 'follow' : 'unfollow',
user_id: $this.data('user-id'),
follow_id: $this.data('follow-id'),
nonce: pwuf_vars.nonce
};
$('img.pwuf-ajax').show();
$.post( pwuf_vars.ajaxurl, data, function(response) {
if( response == 'success' ) {
$('.follow-links a').toggle();
} else {
alert( pwuf_vars.processing_error );
}
$('img.pwuf-ajax').hide();
} );
});
});
display-function.php
<?php
/**
* Retrieves the follow / unfollow links
*
* @access public
* @since 1.0
* @param int $user_id - the ID of the user to display follow / unfollow links for
* @return string
*/
function pwuf_get_follow_unfollow_links( $follow_id = null ) {
global $user_ID;
if( empty( $follow_id ) )
return;
if( ! is_user_logged_in() )
return;
if ( $follow_id == $user_ID )
return;
ob_start(); ?>
<div class="follow-links">
<?php if ( pwuf_is_following( $user_ID, $follow_id ) ) { ?>
<span><a href="#" class="unfollow followed" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>"><span>Following</a></span>
<a href="#" class="follow" style="display:none;" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>">Follow</a>
<?php } else { ?>
<a href="#" class="follow" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>">Follow</a>
<span><a href="#" class="followed unfollow" style="display:none;" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>"><span>Following</a></span>
<?php } ?>
<img src="<?php echo PWUF_FOLLOW_URL; ?>/images/loading.svg" class="pwuf-ajax" style="display:none;"/>
</div>
<?php
return ob_get_clean();
}
javascript wordpress
I'm using "User Follow System" plugin, and i would like to know how to make this js select only one user of the current user i clicked to follow.
I am showing a list of followers and following using get_users in foreach loop.
The problem I am facing is when i click on the follow link for one user, the loading images for all users show up and all the follow links get toggled. $(‘.follow-links a’).toggle();
When i asked the plugin owner he said: Sounds like you may just need to adjust the selector used in the JS to target only the one you clicked on. This was originally built with only one user’s links displayed on the page.
But sorry, I failed to figure it out!
jQuery(document).ready(function($) {
/*******************************
follow / unfollow a user
*******************************/
$( '.follow-links a' ).on('click', function(e) {
e.preventDefault();
var $this = $(this);
if( pwuf_vars.logged_in != 'undefined' && pwuf_vars.logged_in != 'true' ) {
alert( pwuf_vars.login_required );
return;
}
var data = {
action: $this.hasClass('follow') ? 'follow' : 'unfollow',
user_id: $this.data('user-id'),
follow_id: $this.data('follow-id'),
nonce: pwuf_vars.nonce
};
$('img.pwuf-ajax').show();
$.post( pwuf_vars.ajaxurl, data, function(response) {
if( response == 'success' ) {
$('.follow-links a').toggle();
} else {
alert( pwuf_vars.processing_error );
}
$('img.pwuf-ajax').hide();
} );
});
});
display-function.php
<?php
/**
* Retrieves the follow / unfollow links
*
* @access public
* @since 1.0
* @param int $user_id - the ID of the user to display follow / unfollow links for
* @return string
*/
function pwuf_get_follow_unfollow_links( $follow_id = null ) {
global $user_ID;
if( empty( $follow_id ) )
return;
if( ! is_user_logged_in() )
return;
if ( $follow_id == $user_ID )
return;
ob_start(); ?>
<div class="follow-links">
<?php if ( pwuf_is_following( $user_ID, $follow_id ) ) { ?>
<span><a href="#" class="unfollow followed" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>"><span>Following</a></span>
<a href="#" class="follow" style="display:none;" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>">Follow</a>
<?php } else { ?>
<a href="#" class="follow" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>">Follow</a>
<span><a href="#" class="followed unfollow" style="display:none;" data-user-id="<?php echo $user_ID; ?>" data-follow-id="<?php echo $follow_id; ?>"><span>Following</a></span>
<?php } ?>
<img src="<?php echo PWUF_FOLLOW_URL; ?>/images/loading.svg" class="pwuf-ajax" style="display:none;"/>
</div>
<?php
return ob_get_clean();
}
javascript wordpress
javascript wordpress
edited Jan 19 at 18:44
Adham Mohamed
asked Jan 19 at 18:18
Adham MohamedAdham Mohamed
688
688
Just replace$('.follow-links a').toggle()with$this.toggle()
– Callam
Jan 19 at 18:28
add a comment |
Just replace$('.follow-links a').toggle()with$this.toggle()
– Callam
Jan 19 at 18:28
Just replace
$('.follow-links a').toggle() with $this.toggle()– Callam
Jan 19 at 18:28
Just replace
$('.follow-links a').toggle() with $this.toggle()– Callam
Jan 19 at 18:28
add a comment |
1 Answer
1
active
oldest
votes
The line $('.follow-links a').toggle() is toggling every follow link because the query selector doesn't specify a single child. The call of the toggle method is applied to every child found.
Just replace $('.follow-links a').toggle() with $this.toggle().
Thanks for your answer, I tested it but still toggle all of them!
– Adham Mohamed
Jan 19 at 18:36
Is theimg.pwuf-ajaxinside theatag? You will have to do the same thing for that since your query selector will show/hide all elements matchingimg.pwuf-ajax
– Callam
Jan 19 at 18:41
I updated my question, kindly check,Thanks!
– Adham Mohamed
Jan 19 at 18:45
If you have time, can you help me please @Callam
– Adham Mohamed
Jan 19 at 19:24
1
Replace$('img.pwuf-ajax')with$this.closest('.follow-links').find('img.pwuf-ajax')when you show and hide it.
– Callam
Jan 19 at 19:44
|
show 3 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54270046%2fwordpress-js-to-select-only-the-current-user-i-clicked-to-follow%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The line $('.follow-links a').toggle() is toggling every follow link because the query selector doesn't specify a single child. The call of the toggle method is applied to every child found.
Just replace $('.follow-links a').toggle() with $this.toggle().
Thanks for your answer, I tested it but still toggle all of them!
– Adham Mohamed
Jan 19 at 18:36
Is theimg.pwuf-ajaxinside theatag? You will have to do the same thing for that since your query selector will show/hide all elements matchingimg.pwuf-ajax
– Callam
Jan 19 at 18:41
I updated my question, kindly check,Thanks!
– Adham Mohamed
Jan 19 at 18:45
If you have time, can you help me please @Callam
– Adham Mohamed
Jan 19 at 19:24
1
Replace$('img.pwuf-ajax')with$this.closest('.follow-links').find('img.pwuf-ajax')when you show and hide it.
– Callam
Jan 19 at 19:44
|
show 3 more comments
The line $('.follow-links a').toggle() is toggling every follow link because the query selector doesn't specify a single child. The call of the toggle method is applied to every child found.
Just replace $('.follow-links a').toggle() with $this.toggle().
Thanks for your answer, I tested it but still toggle all of them!
– Adham Mohamed
Jan 19 at 18:36
Is theimg.pwuf-ajaxinside theatag? You will have to do the same thing for that since your query selector will show/hide all elements matchingimg.pwuf-ajax
– Callam
Jan 19 at 18:41
I updated my question, kindly check,Thanks!
– Adham Mohamed
Jan 19 at 18:45
If you have time, can you help me please @Callam
– Adham Mohamed
Jan 19 at 19:24
1
Replace$('img.pwuf-ajax')with$this.closest('.follow-links').find('img.pwuf-ajax')when you show and hide it.
– Callam
Jan 19 at 19:44
|
show 3 more comments
The line $('.follow-links a').toggle() is toggling every follow link because the query selector doesn't specify a single child. The call of the toggle method is applied to every child found.
Just replace $('.follow-links a').toggle() with $this.toggle().
The line $('.follow-links a').toggle() is toggling every follow link because the query selector doesn't specify a single child. The call of the toggle method is applied to every child found.
Just replace $('.follow-links a').toggle() with $this.toggle().
answered Jan 19 at 18:34
CallamCallam
8,33821824
8,33821824
Thanks for your answer, I tested it but still toggle all of them!
– Adham Mohamed
Jan 19 at 18:36
Is theimg.pwuf-ajaxinside theatag? You will have to do the same thing for that since your query selector will show/hide all elements matchingimg.pwuf-ajax
– Callam
Jan 19 at 18:41
I updated my question, kindly check,Thanks!
– Adham Mohamed
Jan 19 at 18:45
If you have time, can you help me please @Callam
– Adham Mohamed
Jan 19 at 19:24
1
Replace$('img.pwuf-ajax')with$this.closest('.follow-links').find('img.pwuf-ajax')when you show and hide it.
– Callam
Jan 19 at 19:44
|
show 3 more comments
Thanks for your answer, I tested it but still toggle all of them!
– Adham Mohamed
Jan 19 at 18:36
Is theimg.pwuf-ajaxinside theatag? You will have to do the same thing for that since your query selector will show/hide all elements matchingimg.pwuf-ajax
– Callam
Jan 19 at 18:41
I updated my question, kindly check,Thanks!
– Adham Mohamed
Jan 19 at 18:45
If you have time, can you help me please @Callam
– Adham Mohamed
Jan 19 at 19:24
1
Replace$('img.pwuf-ajax')with$this.closest('.follow-links').find('img.pwuf-ajax')when you show and hide it.
– Callam
Jan 19 at 19:44
Thanks for your answer, I tested it but still toggle all of them!
– Adham Mohamed
Jan 19 at 18:36
Thanks for your answer, I tested it but still toggle all of them!
– Adham Mohamed
Jan 19 at 18:36
Is the
img.pwuf-ajax inside the a tag? You will have to do the same thing for that since your query selector will show/hide all elements matching img.pwuf-ajax– Callam
Jan 19 at 18:41
Is the
img.pwuf-ajax inside the a tag? You will have to do the same thing for that since your query selector will show/hide all elements matching img.pwuf-ajax– Callam
Jan 19 at 18:41
I updated my question, kindly check,Thanks!
– Adham Mohamed
Jan 19 at 18:45
I updated my question, kindly check,Thanks!
– Adham Mohamed
Jan 19 at 18:45
If you have time, can you help me please @Callam
– Adham Mohamed
Jan 19 at 19:24
If you have time, can you help me please @Callam
– Adham Mohamed
Jan 19 at 19:24
1
1
Replace
$('img.pwuf-ajax') with $this.closest('.follow-links').find('img.pwuf-ajax') when you show and hide it.– Callam
Jan 19 at 19:44
Replace
$('img.pwuf-ajax') with $this.closest('.follow-links').find('img.pwuf-ajax') when you show and hide it.– Callam
Jan 19 at 19:44
|
show 3 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54270046%2fwordpress-js-to-select-only-the-current-user-i-clicked-to-follow%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Just replace
$('.follow-links a').toggle()with$this.toggle()– Callam
Jan 19 at 18:28