Title: [Plugin: teachPress] PHP conditional test for TeachPress access
Last modified: August 20, 2016

---

# [Plugin: teachPress] PHP conditional test for TeachPress access

 *  Resolved [Elder_Richmond](https://wordpress.org/support/users/elder_richmond/)
 * (@elder_richmond)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/plugin-teachpress-php-conditional-test-for-teachpress-access/)
 * Can anyone help write a simple conditional test **in PHP** that will be true 
   or false based upon whether the actively logged in WordPress user is successfully**
   enrolled in a particular course or not**.
 * In other words, Boolean true or false based upon if the user is enrolled in TeachPress’s
   Course ID=1.
 * **NOTE, THE TPPOST SHORTCUT YIELDS THE WRONG RESULTS:**
 * > `[tppost id=”1″]
   >  You can only see this text if registered for Course ID 1 [/
   > tppost]`
 * I am trying to display different executable buttons (embeded as shortcode from
   another plugin) based upon php conditional tests. I am unable to utilize the 
   TPPOST shortcut for this scenario because the TPPOST simply outputs the text 
   string of the said embedded button shortcode to the screen. So, instead of seeing
   a functional button, I see the shortcode text string. Hence, TPPOST shortcode
   is functioning successfully as it is designed, but doesn’t allow me to process
   the code within the embedded shortcode for the said buttons.
 * Hence, I need assistance finding a PHP test parameter that I can use to test 
   if a user is registered for a course (e.g. Course ID=1). Perhaps, someone can
   specify an appropriate parameter to go with the current_user_can scope, such 
   as:
 * > `<?php if (!current_user_can('teachpress_user')) ...`
 * However, ‘TEACHPRESS_USER’ is the wrong parameter.
 * Thanks for any assistance.
 * [http://wordpress.org/extend/plugins/teachpress/](http://wordpress.org/extend/plugins/teachpress/)

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Michael](https://wordpress.org/support/users/donsalierie/)
 * (@donsalierie)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/plugin-teachpress-php-conditional-test-for-teachpress-access/#post-3027863)
 * The following function will be a part of the next major version and implements
   this test:
 *     ```
       /**
        * Return true if the user is subscribed in the course or false of not
        * @param integer course_id
        * @param boolean consider_subcourses   -->
        * @return boolean
        */
       function tp_is_user_subscribed ($course_id, $consider_subcourses = false) {
           global $wpdb;
           global $teachpress_signup;
           global $teachpress_courses;
           global $user_ID;
           get_currentuserinfo();
           $course_id = intval($course_id);
           if ( $course_id == 0 ) {
               return false;
           }
           // simple case
           if ( $consider_subcourses == false ) {
               $test = $wpdb->query("SELECT con_id FROM $teachpress_signup WHERE course_id = '$course_id' AND wp_id = '$user_ID' AND waitinglist = '0'");
           }
           // consider subcourses
           if ( $consider_subcourses == true ) {
               $where = "";
               $courses = $wpdb->get_results("SELECT course_id FROM $teachpress_courses WHERE parent = '$course_id'");
               foreach ( $courses as $row ) {
                   $where = $where == "" ? "course_id = '$row->course_id'" : $where . " OR course_id = '$row->course_id'";
               }
               if ( $where != "" ) {
                   $where = " WHERE wp_id = '$user_ID' AND waitinglist = '0' AND ( $where OR course_id = '$course_id' )";
                   $test = $wpdb->query("SELECT con_id FROM $teachpress_signup $where");
               }
               // Fallback if there are no subcourses
               else {
                   $test = $wpdb->query("SELECT con_id FROM $teachpress_signup WHERE course_id = '$course_id' AND wp_id = '$user_ID' AND waitinglist = '0'");
               }
           }
   
           if ( $test >= 1 ) {
               return true;
           }
           return false;
       }
       ```
   
 *  Thread Starter [Elder_Richmond](https://wordpress.org/support/users/elder_richmond/)
 * (@elder_richmond)
 * [13 years, 8 months ago](https://wordpress.org/support/topic/plugin-teachpress-php-conditional-test-for-teachpress-access/#post-3027864)
 * Thanks, Michael.
 * You are a true godsend. Your solution is very empowering because it gives us 
   the ability to perform critical conditional testing with your plugin.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘[Plugin: teachPress] PHP conditional test for TeachPress access’ is closed
to new replies.

 * ![](https://ps.w.org/teachpress/assets/icon-256x256.png?rev=1110789)
 * [teachPress](https://wordpress.org/plugins/teachpress/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/teachpress/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/teachpress/)
 * [Active Topics](https://wordpress.org/support/plugin/teachpress/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/teachpress/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/teachpress/reviews/)

## Tags

 * [conditional](https://wordpress.org/support/topic-tag/conditional/)
 * [current_user_can](https://wordpress.org/support/topic-tag/current_user_can/)
 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [post-type](https://wordpress.org/support/topic-tag/post-type/)
 * [shortcode](https://wordpress.org/support/topic-tag/shortcode/)
 * [user](https://wordpress.org/support/topic-tag/user/)

 * 2 replies
 * 2 participants
 * Last reply from: [Elder_Richmond](https://wordpress.org/support/users/elder_richmond/)
 * Last activity: [13 years, 8 months ago](https://wordpress.org/support/topic/plugin-teachpress-php-conditional-test-for-teachpress-access/#post-3027864)
 * Status: resolved