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.
LVL 11
Amanda WatsonWeb DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

selvolCommented:
MAybe I am wrong due to the wordpress coding but I believe the "=" in the functions should
be. "=="
   if ($logged_in && $course_id = 182 ) {
Should be    if ($logged_in && $course_id == 182 ) {

As in 
 if ($course_id= 
should be 
 if ($course_id==

Open in new window



Selvol
0
Ray PaseurCommented:
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.
0
giltjrCommented:
Even if you had "==" instead of "=" can you even do:

     if ($course_id = 532 || 491 || 585)

I would code something like:

     if ($course_id == 532 || $course_id == 491 || $course_id == 585)

At a minimum I would think it would need to be:

      if ($course_id == (532 || 491 || 585))

But I don't think that is even valid.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Amanda WatsonWeb DeveloperAuthor Commented:
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?
0
selvolCommented:
You need to use "==" not "=" in this
code

      if ($logged_in && $course_id = 182 ) {

Open in new window

should be

      if ($logged_in && $course_id == 182 ) {

Open in new window


From what I have learned with much much help from @Ray Paseur.
over the years.
comparison statements use two == or ===
assignment statements use =
 


Selvol
0
Amanda WatsonWeb DeveloperAuthor Commented:
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);
*/
0
Amanda WatsonWeb DeveloperAuthor Commented:
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.
0
giltjrCommented:
Again try:

     if ($course_id == 532 ||$course_id == 491 || $course_id ==585)

Instead of

     if ($course_id = 532 || 491 || 585)

First, $course_id = 532 is setting the value of $course_id to 532 because of the single =, as you have been told b before

Second,  even if you had "$course_id == 532 || 491 || 585"  it not doing what you want, unless you want to see if 491 and 585 are true.

What "$course_id == 532 || 491 || 585" is doing is checking to see if $course_id is equal to 532 or if 491 or 585 is "true", which they are not.  What I am assuming you want to do is check to see if $course_id is 532, 491, or 585, which is what my suggestion does.
0
Amanda WatsonWeb DeveloperAuthor Commented:
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
0
Amanda WatsonWeb DeveloperAuthor Commented:
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

0
Amanda WatsonWeb DeveloperAuthor Commented:
I am sure I am missing some if/else or I don't know what is wrong!!!!??
0
giltjrCommented:
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/';
      }
0
giltjrCommented:
Although it may be too late, instead of using a series of if/thens or if/elseif/thens you may want to use switch.  I find using switch is much easier to follow than an series of if/elseifs.  In the sample below since you want to do the same thing for 3394 and 3396, there is no code after the check for 3394, it falls through to 3396 and does the same thing.  Since I'm not sure what you wanted to do for 3392, I put what you had in the second if.  

The last check for "default" is what is executed if/when the course code returned is not in any other case.  This way if you don't have to check for every course code, just for the ones that you want to do something unique for.

The "break" means to stop checking for other matches once a match is found.

function next_course_redirect ($link, $course_id) {
      $course_id = learndash_get_course_id($id);
        switch ($id) (
         case 3392:
            return 'http://corporate.12stepsforbusiness.com.au/courses/step-1-one-on-one/';
            break;
         case 3394:
         case 3396:
            return 'http://corporate.12stepsforbusiness.com.au/courses/problem-definition/';
            break;
         default:
            "Do something when the course code is not equal to anything you are checking for";
             break;
         }
}
0
Amanda WatsonWeb DeveloperAuthor Commented:
Really fast help!
Thanks

A
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WordPress

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.