BuddyDev

Search

[Resolved] Help with a bit of javascript

  • Participant
    Level: Enlightened
    Posts: 120
    Erich199 on #47686

    Hi,
    I am integrating mycred into my buddypress site. I’ve set up some downloads for my users that requires them to be logged in. When they click on the button, it will use javascript to give a pop up to confirm. Once they confirm, it will deduct the points. However; I can’t get it to then redirect them to the link for the download. If I add an attribute href and then use php to add it into the form post action, it doesn’t work properly.

    
    /**
     * Shortcode: Take Points
     */
    add_shortcode( 'mycred_take', 'mycred_pro_render_take_shortcode' );
    function mycred_pro_render_take_shortcode( $atts, $label = 'Download Charge' ) {
    
    	extract( shortcode_atts( array(
    		'user_id' => '',
    		'confirm' => '',
    		'amount'  => '',
    		'unique'  => 0,
    		'ref'     => 'mycred_take',
    		'entry'   => '%plural% lose',
    		'done'    => 'You have lost points',
    		'ctype'   => 'mycred_default'
    	), $atts ) );
    
    	if ( ! is_user_logged_in() || ! function_exists( 'mycred' ) ) return '';
    
    	if ( $user_id == '' )
    		$user_id = get_current_user_id();
    
    	// Load essentials
    	$user_id = absint( $user_id );
    	$mycred = mycred( $ctype );
    
    	// User is excluded = has no balance
    	if ( $mycred->exclude_user( $user_id ) ) return '';
    
    	// Unique check
    	if ( $unique == 1 && $mycred->has_entry( $ref, 0, $user_id, '', $ctype ) ) return '';
    
    	$balance = $mycred->get_users_balance( $user_id, $ctype );
    
    	$output = '';
    
    	// If button was pushed
    	if ( isset( $_POST['mycred-take-points-token'] ) && wp_verify_nonce( $_POST['mycred-take-points-token'], 'mycred-deduct-points' . $ref . $ctype ) ) {
    
    		// Deduct
    		$mycred->add_creds(
    			$ref,
    			$user_id,
    			0 - $amount,
    			$entry
    		);
    
    		// Update balance
    		$balance = $balance - $amount;
    
    		if ( $done != '' )
    			$output .= '<p>' . $done . '</p>';
    
    	}
    
    	// Too low balance
    	if ( $balance < $amount ) return '';
    
    	$onclick = '';
    	if ( $confirm != '' )
    		$onclick = '
    <script type="text/javascript">
    ( function( $ ) {
    	$( "form#mycred-take-shortcode' . $ref . $ctype . '" ).on( "submit", function(){
    		if ( ! confirm( \'' . $confirm . '\' ) ) return false;
    	});
    } )( jQuery );
    </script>';
    
    	return $output . '<form action="" method="post" id="mycred-take-shortcode' . $ref . $ctype . '"><input type="hidden" name="mycred-take-points-token" value="' . wp_create_nonce( 'mycred-deduct-points' . $ref . $ctype ) . '" /><input type="submit" class="button" value="' . $label . '" /></form>' . $onclick;
    
    }
    
  • Keymaster
    (BuddyDev Team)
    Posts: 24645
    Brajesh Singh on #47701

    Hi Erich,
    There are 2 ways to redirect:-

    1. using php to redirect.
    2. Using javascript to redirect.

    Redirect using php will only work if you do it before headers/output is sent to the browser.
    Since you are using the shortcode for handling the action(deduction) and the shortcode is inside the content, the php redirect will not work(there are ways to make it work using output buffering but let us not make it complex).

    There are 2 solutions left:-
    1. Refactor the code to add the handler(deduction of point) as some different action such at ‘bp_template_redirect’
    2. or using javascript to redirect using window.location=url

    If you need assistance with exact code, Please let me know, I will update it.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 120
    Erich199 on #47704

    Hi Brajesh,

    Thanks for the reply.

    Is it possible to pass a short code argument to the window.locationJavaScript?

  • Keymaster
    (BuddyDev Team)
    Posts: 24645
    Brajesh Singh on #47756

    Hi Erich,
    I am sorry for the delayed reply.
    I have updated the code.

    please take a look.
    https://pastebin.com/sdDZ1E2v

    Make sure to change the url value appropriately.

    Regards
    Brajesh

  • Participant
    Level: Enlightened
    Posts: 120
    Erich199 on #48257

    Hi Brajesh,
    No worries. I just got around to getting back to it. It worked perfectly. Thanks for your help.

  • Keymaster
    (BuddyDev Team)
    Posts: 24645
    Brajesh Singh on #48278

    Thank you Erich.
    I am glad it worked!

    Regards
    Brajesh

The topic ‘ [Resolved] Help with a bit of javascript’ is closed to new replies.

This topic is: resolved