0

So what I wanna do is when a user pushes the submit button, a php query is run. In that php query if an "If statement" condition is fulfilled, I want to execute an operation (which in my case is a page redirection) and return false.

[EDIT] The whole query code is something like this:

add_filter( ‘mycred_add’, ‘mycred_pro_restrict_negative_balances’, 1, 3 );
function mycred_pro_restrict_negative_balances( $reply, $request, $mycred ) {

  if ( $reply === false || $request[‘amount’] > 0 ) return $reply;

  extract( $request );

  // Users current balance
  $current_balance = $mycred->get_users_balance( $user_id, $type );

  // If balance is zero – decline OR If we are deducting points, make sure the amount will not take us below zero
  if (( $current_balance <= 0 )||(($current_balance – abs( $amount )) < 0 )) {
    $redirect_to_page = 22;
    return false;
  }

  return $reply;

}

I wanna know if the query will work?? And if there's something wrong with it, please do mention the correction. Thanks in advance for helping / trying to help me.

Patrice Gahide
  • 3,644
  • 1
  • 27
  • 37
Joe Wilkinson
  • 111
  • 1
  • 1
  • 8

2 Answers2

1

In your calling function, test for a FALSE return value. If it's false, then call your redirection after processing any other actions you need to do upon this function returning FALSE.

NathanR
  • 196
  • 10
  • Ya that's exactly what I need to learn, how exactly do I test for a FALSE. – Joe Wilkinson Mar 11 '15 at 08:31
  • Can it be something like /* if (return false == true) { $redirect_to_page = 10;} – Joe Wilkinson Mar 11 '15 at 08:32
  • @JoeWilkinson If your function that this code is in is called say `testForX()`, you would do: `if(testForX() == FALSE) { $redirect_to_page = 10; }` or simply using the `!` (not operator) like `if(!testForX()) { $redirect_to_page = 10; }` – NathanR Mar 11 '15 at 08:43
  • Thanks man I'll try this and let u know ASAP. BTW I've updated the whole query above. Could you please see to that once and confirm with the answer you provided. Thanks for your help man. – Joe Wilkinson Mar 11 '15 at 08:44
  • Can't believe I missed it before, but there is another issue in that the variable that you're setting (`$redirect_to_page`) isn't actually being used anywhere. When you return `false`, that's the only thing that's returned. The value of that variable is never used, unless it's a global. – NathanR Mar 11 '15 at 08:55
0

You probably nead something like that:

if ( $current_balance <= 0 ) {
 /* Redirection */
 header("Location: http://www.example.com/page10.php"); 
 exit();
}

Sending header 'Location' will tell browser to go to specified URL. That's the simplest way of redirection. You don't have to return false.