Solved

correction to php code please?

Posted on 2014-11-20
14
443 Views
Last Modified: 2014-12-02
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.
0
Comment
Question by:Amanda Watson
  • 7
  • 4
  • 2
  • +1
14 Comments
 
LVL 17

Assisted Solution

by:selvol
selvol earned 200 total points
ID: 40457096
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
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40457111
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
 
LVL 57

Accepted Solution

by:
giltjr earned 300 total points
ID: 40457728
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
 
LVL 11

Author Comment

by:Amanda Watson
ID: 40461143
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
 
LVL 17

Assisted Solution

by:selvol
selvol earned 200 total points
ID: 40461254
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
 
LVL 11

Author Comment

by:Amanda Watson
ID: 40465929
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
 
LVL 11

Author Comment

by:Amanda Watson
ID: 40465936
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 57

Assisted Solution

by:giltjr
giltjr earned 300 total points
ID: 40467218
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
 
LVL 11

Author Comment

by:Amanda Watson
ID: 40468178
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
 
LVL 11

Author Comment

by:Amanda Watson
ID: 40468215
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
 
LVL 11

Author Comment

by:Amanda Watson
ID: 40468216
I am sure I am missing some if/else or I don't know what is wrong!!!!??
0
 
LVL 57

Expert Comment

by:giltjr
ID: 40469107
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
 
LVL 57

Assisted Solution

by:giltjr
giltjr earned 300 total points
ID: 40469158
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
 
LVL 11

Author Closing Comment

by:Amanda Watson
ID: 40477497
Really fast help!
Thanks

A
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now