By using Elementorforum.com’s services you agree to our Cookies Use and Data Transfer outside the EU.
We and our partners operate globally and use cookies, including for analytics, personalisation, ads and Newsletters.

  • Join the Best Unofficial Elementor Support forum

    Provide or get advice on everything Elementor and Wordpress, ask questions, gain confirmation or just become apart of a friendly, like minded community who love Wordpress and Elementor


    Join us!
elementor official

Question How to create custom display condition

A

athanasius

New Member
I am trying to figure out how I can create a custom display condition for templates. So for example, I am using LearnDash LMS and would like to specifically add a condition for users that are enrolled in a course. I dug into the ElementorPro Conditions classes to see how things are being done for the default conditions but I am not sure if I am on the right track. I've included the start of a class I have written to hopefully do the job, however I am not sure how/where to register this. Any help would be appreciated.

Thanks!

PHP:
class Course_By_Enrolled extends ElementorPro\Modules\ThemeBuilder\Conditions\Condition_Base {
    private $post_type;
    public static function get_type(){return 'singular';
    }
    public static function get_priority(){return 40;
    }
    public function __construct( $post_type ){parent::__construct();
        $this->post_type = $post_type;
    }
    public function get_name(){return 'course_by_enrolled';
    }
    public function get_label(){return sprintf( __( 'Course By Enrolled', 'elementor-pro' ),$this->post_type->label );
    }
    public function check( $args = null ){if (is_singular('sfwd-courses')){$course_id = get_the_ID();
            $user_id = get_current_user_id();
            return is_user_logged_in() && sfwd_lms_has_access( $course_id, $user_id );
        }
        return false;
    }
}
 
elementor official

Attachments

A

athanasius

New Member
I figured out how to do it. For anyone else trying to achieve something similar here is what I added to my child theme functions.php file:

Code:
add_action( 'elementor/theme/register_conditions', function( $conditions_manager ){class Course_By_Enrolled extends ElementorPro\Modules\ThemeBuilder\Conditions\Condition_Base {
        public static function get_type(){return 'singular';
        }
        public static function get_priority(){return 40;
        }
        public function get_name(){return 'course_by_enrolled';
        }
        public function get_label(){return sprintf( __( 'Courses By Enrolled', 'elementor-pro' ),$this->post_type->label );
        }
        public function check( $args = null ){if (is_singular('sfwd-courses')){$course_id = get_the_ID();
                $user_id = get_current_user_id();
                return is_user_logged_in() && sfwd_lms_has_access( $course_id, $user_id );
            }
            return false;
        }
    }
    $conditions_manager->get_condition( 'sfwd-courses' )->register_sub_condition( new Course_By_Enrolled() );
}, 100 );
The $conditions_manager->get_condition( 'sfwd-courses' ) will specify where in the menu the condition will reside. So if you just want to add a new top level condition you would probably use 'singular' instead of sfwd-courses which is specific to LearnDash.

Hope this helps someone else.

Pax,
Athanasius
 
elementor official

Latest posts

Other Elementor Resources

elementor official
elementor official
Top