Skip to:
Content
Pages
Categories
Search
Top
Bottom

How do I turn off the redirect on an ajax favorite click?


  • mattbru
    Participant

    @mattbru

    WordPress v5.0.3
    bbPress v2.5.14

    Im using the function bbp_user_favorites_link() on my homepage post content to show a favorite link icon in the upper righthand corner of the post image.

    When you click it, it goes to the single topic page. The “like” gets liked or unliked but I want to remain on the homepage. i dont want to go to the single topic page when clicking that icon from the homepage.

    https://forum.thecritterdepot.com/

    Note: You cannot “like” when youre not logged in. But I made a dummy icon that tells you you need to log in when clicked.

    I tried looking through all the bbpress “favorites” functions but couldnt figure out how to remove this behavior.

    Thanks!

Viewing 14 replies - 1 through 14 (of 14 total)

  • Robin W
    Moderator

    @robin-w

    Just had a look, and it should be possible to filter that function, but I really need to use the code you are using on the homepage to understand the issue.

    can you post that code?


    mattbru
    Participant

    @mattbru

    Thank you for taking a look Robin.
    Below is the entire block of code for the post block.
    The lines youll be concerned with are 76-80 and 84-93. But ultimately it is line 92 <?php bbp_user_favorites_link($fav_args); ?>

    
    <?php
    /**
     * BuddyPress topic "query"
     * Show grid of all topics inside "ul.container"
     * @link http://hookr.io/plugins/bbpress/2.5.9/functions/bbp_has_topics/
     * 
     */
    	$topic_id = bbp_get_topic_id();
    	$forum_id = bbp_get_topic_forum_id($topic_id);
    	$date = bbp_get_topic_post_date($topic_id,true);
    	$time = reformat_bbp_post_time($date);
    
    	$title = bbp_get_topic_title();
    		$content = strip_tags(bbp_get_topic_content());
    		$content = trim(preg_replace('/^[ \t]*[\r\n]+/m','',$content)); 
    		$content = preg_replace('/(Attachments:).*$/','',$content); 
    		$content = preg_replace('/(This topic was modified).*$/m','',$content); 
    		$excerpt = getSummary($content,186);
    
    	$forum_title = get_post_field( 'post_title', $forum_id, 'raw' );
    	$forum_link = bbp_get_forum_permalink( $forum_id );
    	
    	$user_id = get_post_field('post_author', $topic_id);
    	$avatar = get_avatar( $user_id, 30 );
    	$author_name = esc_html( get_the_author_meta( 'display_name', $user_id ) );
    	
    	$voice_count = bbp_get_topic_voice_count($topic_id);
    	$voice_label = $voice_count === 1 ? 'voice' : 'voices';
    	$reply_count = bbp_get_topic_reply_count($topic_id);
    	$reply_label = $reply_count === 1 ? 'comment' : 'comments';
    	$image_size = 'post-featured';
    
    	// get topic post featured image
    	$src = wp_get_attachment_image_src( get_post_thumbnail_id($topic_id), $image_size);
    	$imgsrc = get_the_post_thumbnail($topic_id) != '' ? $src[0] : '';
    	$image_type = 'img';
    	$has_image = true;
    
    	// attempt to get the file attachment
    	$attachments = d4p_get_post_attachments($topic_id);
    	$attachment_url = wp_get_attachment_image_src( $attachments[0]->ID, $image_size);
    	$attachment_url = $attachment_url[0];
    
    	$cn = d4p_topic_attachments_count($topic_id);
    	
    	// if its empty, set src as attachment url
    	if ($imgsrc == '' && !empty($attachment_url)) {
    		$imgsrc = $attachment_url;
    		$image_type = 'bg-img attachment';
    	}
    	
    	// if its still empty, set src as first image found in the post
    	if (empty($imgsrc)) {
    		/**
    		 * catch_that_image()
    		 * Gets the first image in the body of the post
    		 * defined in /library/custom-theme-functions.php
    		 */
    		$imgsrc = catch_that_image($title,true);
    		if (empty($imgsrc)) {
    			$has_image = false;
    			$image_type = 'none';
    		} else {
    			$image_type = 'bg-img catch';
    		}
    	}
    
    	$is_ss = bbp_is_topic_super_sticky() ? 'super-sticky' : 'not-super-sticky';
    	$bgcolor = get_post_meta( $topic_id, 'sticky_post_color', $single = false );
    	$bgcolor = (!empty($bgcolor)) ? $bgcolor[0] : '';
    
    	if (!empty($bgcolor)) {
    		$image_type .= ' color-'.$bgcolor;
    	}
    
    	// gold count
    	$gold_count = bbp_get_topic_favoriters($topic_id);
    	$gold_count = count($gold_count);
    	$before_html = '<span class="gold-count">'.$gold_count.'</span>';
    	$fav_args = array('before' => $before_html);
    
    ?>
    	<li class="fourth a <?php echo $image_type; ?>" data-id="<?php echo $topic_id; ?>" data-userid="<?php echo $user_id; ?>" data-sticky="<?php echo $is_ss; ?>">
    		<?php if (!is_user_logged_in()) : ?>
    			<span id="favorite-toggle">
    				<?php echo $before_html; ?>
    				<span id="favorite-<?php echo $topic_id ?>" data-tooltip="Give Critter Gold">
    					<a href="#" class="favorite-toggle" data-topic="<?php echo $topic_id ?>" rel="nofollow">Favorite</a>
    				</span>
    			</span>
    		<?php else : ?>
    			<?php bbp_user_favorites_link($fav_args); ?>
    		<?php endif; ?>
    		
    		<a class="bbp-topic-permalink" href="<?php bbp_topic_permalink(); ?>">
    			<figure <?php if($has_image) : ?> style="background-image: url('<?php echo $imgsrc; ?>');"<?php endif; ?> class="<?php if(!$has_image) { echo 'no-img'; } ?>">
    			<?php if ($has_image) : ?>
    				<img src="<?php echo $imgsrc; ?>" alt="<?php echo $title; ?>">
    			<?php else : ?>
    				<h3><?php echo $title; ?></h3>
    			<?php endif; ?>
    				<span class="meta-inside">
    					<span class="read">Read Post</span>
    					<span class="data">
    						<span class="replies"><?php echo $reply_count; ?> <?php echo $reply_label; ?></span>
    					</span>
    					<span class="author">Started By <?php echo $avatar; ?> <?php echo $author_name; ?></span>
    				</span>
    			</figure>
    			<!-- <a href="<?php echo $forum_link; ?>"><?php echo $forum_title; ?></a> |  -->
    			<figcaption>
    			<?php if ($has_image) : ?>
    				<h3><?php echo $title; ?></h3>
    			<?php endif; ?>
    				<span class="meta">
    					<span class="category"><?php echo $forum_title; ?></span><span> | </span>
    <?php /*
    					<span><?php echo $voice_count; ?> <?php echo $voice_label; ?></span> | 
    */ ?>
    					<span class="comments"><?php echo $reply_count; ?> <?php echo $reply_label; ?></span> |
    					<time><?php echo $time; ?></time>
    				</span>
    			<?php if ($has_image) : ?>
    				<p><?php echo $excerpt; ?></p>
    			<?php endif; ?>
    			</figcaption>
    		</a>
    	</li>
    

    Robin W
    Moderator

    @robin-w

    hmm… dug a bit further and the problem looks more complicated than I thought – I’ll play with it and come back


    Robin W
    Moderator

    @robin-w

    ok, this looks like it should work – add to your functions file – try it and come back

    add_action( 'bbp_favorites_handler', 'rew_redirect_to_home_page' ,10,1) ;
    
    function rew_redirect_to_home_page ($success) {
    	if ( true === $success && is_front_page()) {
    		$redirect = home_url() ;
    		wp_redirect( $redirect );
    
    		// For good measure
    		exit();
    	}		
    }

    mattbru
    Participant

    @mattbru

    Robin, Yes – I’ve dug pretty deep myself and it is all pretty complicated.

    I’ve tried the action you provided and it does not do anything different, unfortunately.

    Topics

    Earlier – I did try “hacking” (editing bbpress core plugin code) and changing the redirect as you did here with the hook. But I could not get it to stay on the homepage then either.

    Hm. Maybe its not possible? I coudlnt find the exact code where it was happening. I thought it was inside the bbp_favorites_handler() function located here: bbpress/includes/users/functions.php line 430 Looks like youre using that hook on line 481 (bbp_favorites_handler).

    I wonder if it has to do with either of these actions:
    template_redirect
    bbp_template_redirect

    I tried this modified version of your hook code:

    add_action( 'bbp_favorites_handler', 'tcdf_redirect_to_home_page' , 99, 1) ;
    function tcdf_redirect_to_home_page ($success) {
    	if ( true === $success && (is_front_page() || is_home() || is_page(2)) ) {
    		$redirect = home_url() ;
    		wp_safe_redirect( $redirect );
    		// For good measure
    		exit();
    	}		
    }

    No luck. 🙁


    Robin W
    Moderator

    @robin-w

    ok, I registered, but cant see where to favorite a topic??


    mattbru
    Participant

    @mattbru

    Go to the homepage

    Topics


    Hover over the “critter gold” icon in upper right corner of the post image, you should get a popup tooltip. Then click it.

    Or, if you are on a single topic page like this:

    Bearded Dragon Care Guide


    You just click the “critter gold” icon in the upper right. Its the normal place where the “favorite” link usually is.


    Robin W
    Moderator

    @robin-w

    ok, let’s try something more basic

    this code just replaces the bbp_get_user_favorites_link, but puts the home url as the permalink

    function rew_get_user_favorites_link( $args = '', $user_id = 0, $wrap = true ) {
    		if ( ! bbp_is_favorites_active() ) {
    			return false;
    		}
    
    		// Parse arguments against default values
    		$r = bbp_parse_args( $args, array(
    			'favorite'  => __( 'Favorite',  'bbpress' ),
    			'favorited' => __( 'Favorited', 'bbpress' ),
    			'user_id'   => 0,
    			'topic_id'  => 0,
    			'before'    => '',
    			'after'     => ''
    		), 'get_user_favorites_link' );
    
    		// Validate user and topic ID's
    		$user_id  = bbp_get_user_id( $r['user_id'], true, true );
    		$topic_id = bbp_get_topic_id( $r['topic_id'] );
    		if ( empty( $user_id ) || empty( $topic_id ) ) {
    			return false;
    		}
    
    		// No link if you can't edit yourself
    		if ( ! current_user_can( 'edit_user', (int) $user_id ) ) {
    			return false;
    		}
    
    		// Decide which link to show
    		$is_fav = bbp_is_user_favorite( $user_id, $topic_id );
    		if ( ! empty( $is_fav ) ) {
    			$text       = $r['favorited'];
    			$query_args = array( 'action' => 'bbp_favorite_remove', 'topic_id' => $topic_id );
    		} else {
    			$text       = $r['favorite'];
    			$query_args = array( 'action' => 'bbp_favorite_add',    'topic_id' => $topic_id );
    		}
    
    		$permalink = home_url() ;
    
    		$url  = esc_url( wp_nonce_url( add_query_arg( $query_args, $permalink ), 'toggle-favorite_' . $topic_id ) );
    		$sub  = $is_fav ? ' class="is-favorite"' : '';
    		$html = sprintf( '%s<span id="favorite-%d"  %s><a href="%s" class="favorite-toggle" data-topic="%d">%s</a></span>%s', $r['before'], $topic_id, $sub, $url, $topic_id, $text, $r['after'] );
    
    		// Initial output is wrapped in a span, ajax output is hooked to this
    		if ( ! empty( $wrap ) ) {
    			$html = '<span id="favorite-toggle">' . $html . '</span>';
    		}
    
    		// Return the link
    		return apply_filters( 'rew_get_user_favorites_link', $html, $r, $user_id, $topic_id );
    	}

    so remove my previous suggestion, and add the above function

    Then where you have

    bbp_user_favorites_link($fav_args); in your code

    replace with

    echo rew_get_user_favorites_link($fav_args);


    mattbru
    Participant

    @mattbru

    Thanks Robin.

    I gave it a try.
    If i click the icon – It gives me a white page, basically a 404.
    But then i go back to the homepage and refresh and I get the normal page and whatever i clicked on looks like it toggles on/off fine. So it works but theres still an issue w/ the 404 page.

    I checked the php error log, and there’s nothing in there.
    Maybe Ill have to rewrite a whole new ajax function.


    Robin W
    Moderator

    @robin-w

    try re-adding my hook code as well !!


    mattbru
    Participant

    @mattbru

    Thank you!
    It now reloads the page okay. (doesnt appear to use ajax). So its not going to a 404 page. But id prefer it not to reload the page. Hm.


    Robin W
    Moderator

    @robin-w

    we got close though !

    if you do fix, come back with the solution 🙂


    mattbru
    Participant

    @mattbru

    Ill work on it some more and definitely post here if I figure out a solution.
    Thanks for all your help.


    Robin W
    Moderator

    @robin-w

    no problem!

Viewing 14 replies - 1 through 14 (of 14 total)
  • You must be logged in to reply to this topic.
Skip to toolbar