Link to home
Start Free TrialLog in
Avatar of Amanda Watson
Amanda WatsonFlag for Australia

asked on

correction to php code please?

I have some code that is supposed to help with a wordpress plugin - called Learn dash.
Its to help you go through courses.
The problem is, once you complete the course it doesn't push you on to the next course which is important.

Learn Dash provided support, but so far no code
 http://support.learndash.com/forums/topic/redirect-to-page-on-course-completion/

This is the code I tried but it doesn't work
/*--Redirect on Step 1 Completion--

add_action('learndash_course_completed', 'complete_taster_redirect');

function complete_taster_redirect() {
      $user_id = get_current_user_id();
      $id = get_the_ID();
      $course_id = learndash_get_course_id($id);
      $pmpro_level = pmpro_has_membership_level($user_id);
      if ($logged_in && $course_id = 182 ) {
            wp_redirect( 'http://corporate.12stepsforbusiness.com.au/step-1-completed/ ', 301 );
      }
}*/

/*--REDIRECT TO NEXT COURSE ON COMPETION---
//***PROBABLY NEED TO PUT IN SOME IF_USER_HAS_ACCESS  - ELSE GO TO????? CLAUSE
function next_course_redirect ($link, $course_id) {

      $course_id = learndash_get_course_id($id);
      // MENTOR/MENTEE/CHAMPION SETUP --> PROBLEM DEFINITION
      if ($course_id = 532 || 491 || 585) {
      return 'http://corporate.12stepsforbusiness.com.au/courses/problem-definition/';
      }
      // PROBLEM DEFINITION --> STEP 1: 1:1      
      if ($course_id = 365) {
      return 'http://corporate.12stepsforbusiness.com.au/courses/step-1-one-on-one/';
      }
      // STEP 1: 1:1      --> STEP 1: GROUP
      if ($course_id = 14) {
      return 'http://corporate.12stepsforbusiness.com.au/courses/step-1-group/';
      }
}
add_filter('learndash_course_completion_url', 'next_course_redirect', 5, 2);
*/

I wonder if anyone can help with this.
SOLUTION
Avatar of James Williams
James Williams
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think @selvol is right about the difference between assignment statements (one equal sign) and comparison statements (two or three equals signs).  It also appears that the entire code block is commented out by the use of /* ... */ so none of that code is getting executed at all.  If you paid any money for that code, you should ask for a refund because it doesn't look like it meets the minimum standards for merchantability or fitness.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Amanda Watson

ASKER

Ok, This code IS valid  

For course completion redirection:

add_filter("learndash_course_completion_url", function($link, $course_id) {
//You can change the link here
return $link;
}, 5, 2);
now my code needs to upgrade because we are connected to PMPro. The redirection needs to be according to membership level.

So I tried this code
add_action('learndash_course_completed', 'complete_taster_redirect');

function complete_taster_redirect() {
      $user_id = get_current_user_id();
      $id = get_the_ID();
      $course_id = learndash_get_course_id($id);
      $pmpro_level = pmpro_has_membership_level($user_id);
      if ($logged_in && $course_id = 182 ) {
            wp_redirect( 'http://ourdomain.com/redirect-page/ ', 301 );
      }
}


but doesn't seem to be working?  Can anyone help?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, so I tried that code
add_action('learndash_course_completed', 'complete_taster_redirect');

function complete_taster_redirect() {
      $user_id = get_current_user_id();
      $id = get_the_ID();
      $course_id = learndash_get_course_id($id);
      $pmpro_level = pmpro_has_membership_level($user_id);
      if ($logged_in && $course_id == 182 ) {
            wp_redirect( 'http://corporate.12stepsforbusiness.com.au/step-1-completed/', 301 );
      }
}

and did my first course and hit complete and was redirected to http://corporate.12stepsforbusiness.com.au/lessons/test-lesson-1/ which was blank but after I came back to the site it showed the correct page.

Perhaps I need to progress further...
so I have done
1.  Redirect after step one and then next is
2. REDIRECT TO NEXT COURSE ON COMPLETION...

This is my attempt at that code??  Please can someone help me use this code?

//***PROBABLY NEED TO PUT IN SOME IF_USER_HAS_ACCESS  - ELSE GO TO????? CLAUSE
function next_course_redirect ($link, $course_id) {

      $course_id = learndash_get_course_id($id);
      // MENTOR/MENTEE/CHAMPION SETUP --> PROBLEM DEFINITION
      if ($course_id = 532 || 491 || 585) {
      return 'http://corporate.12stepsforbusiness.com.au/courses/problem-definition/';
      }
      // PROBLEM DEFINITION --> STEP 1: 1:1      
      if ($course_id = 365) {
      return 'http://corporate.12stepsforbusiness.com.au/courses/step-1-one-on-one/';
      }
      // STEP 1: 1:1      --> STEP 1: GROUP
      if ($course_id = 14) {
      return 'http://corporate.12stepsforbusiness.com.au/courses/step-1-group/';
      }
}
add_filter('learndash_course_completion_url', 'next_course_redirect', 5, 2);
*/
I think it needs to be a series of if/elses (I think) to check what the course ID that is completing is, then to direct to the next course. I thought I had it working but it kept directing all  courses upon completion to the same Problem Definition course.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes, that makes perfect sense thank you.

How about the rest of the code, is there more that needs to be added to test against the group they are from?
A
This seems to be the only code I need to fix now because I added this code to the top
add_action('learndash_course_completed', 'complete_taster_redirect');

function complete_taster_redirect() {
	$user_id = get_current_user_id();
	$id = get_the_ID();
	$course_id = learndash_get_course_id($id);
	$pmpro_level = pmpro_has_membership_level($user_id);
	if ($logged_in && $course_id == 3392 ) {
		wp_redirect( 'http://corporate.12stepsforbusiness.com.au/step-1-completed/ ', 301 );
	}
}

Open in new window

and the functions page was completely BLANK, so I quickly commented it out and it got happy again.
I wish I knew how to tidy this up.  What does this damn code need to simply move on to next course after completion??!!

/*--REDIRECT TO NEXT COURSE ON COMPLETION---
//***PROBABLY NEED TO PUT IN SOME IF_USER_HAS_ACCESS  - ELSE GO TO????? CLAUSE*/
function next_course_redirect ($link, $course_id) {

	$course_id = learndash_get_course_id($id);
	// MENTOR/MENTEE/CHAMPION SETUP --> PROBLEM DEFINITION
	if ($course_id == 3392 || $course_id == 3394 || $course_id == 3396) {
	return 'http://corporate.12stepsforbusiness.com.au/courses/problem-definition/';
	}
	// PROBLEM DEFINITION --> STEP 1: 1:1	
	if ($course_id == 3392) {
	return 'http://corporate.12stepsforbusiness.com.au/courses/step-1-one-on-one/';
	}
}
	
add_filter('learndash_course_completion_url', 'next_course_redirect', 5, 2);

Open in new window

I am sure I am missing some if/else or I don't know what is wrong!!!!??
In the last code you posted you have you are checking for course 3392 twice and doing 2 different things.  You can only do one of those two.  So you need to remove one of the checks for 3392.  Not sure which one you want.

You have:

      if ($course_id == 3392 || $course_id == 3394 || $course_id == 3396) {
      return 'http://corporate.12stepsforbusiness.com.au/courses/problem-definition/';
      }

followed by:

      if ($course_id == 3392) {
      return 'http://corporate.12stepsforbusiness.com.au/courses/step-1-one-on-one/';
      }
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Really fast help!
Thanks

A