Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ Please include the following header format when submitting a snippet
```
/**
* Describe what the snippet does in one sentence. (i.e. Add a checkbox to the checkout page.)
*
*
* Learn more at: https://lifterlms.com/link-to-content-if-available-or-remove-this-line/
*
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion documentation for step-by-step directions on either method.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Add custom columns and output data on the Courses > Students reporting for course export.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion documentation for step-by-step directions on either method.
* https://lifterlms.com/docs/adding-custom-code/
*/
/**
* Output data for a custom column on the student reporting for course export.
*
* @param string $value Default value being output
* @param string $key Name of the custom field being output
* @param obj $student LLMS_Student for the row
* @param string $context Output context "display" or "export"
* @return mixed
*/
function course_students_table_column_data( $value, $key, $data, $context ) {

if ( 'test' === $key ) {
$value = 'Some Test';
}

return $value;

}
add_filter( 'llms_table_get_data_course-students', 'course_students_table_column_data', 10, 4 );

/**
* Add custom columns to the course students reporting table.
*
* @param array $cols Columns.
* @return array $cols Columns.
*/
function course_students_table_columns( $cols ) {

$cols['test'] = array(
'title' => __( 'Test Test', 'lifterlms' ),
'export_only' => false, // set to false to show display screens but include in exports
'exportable' => true, // set to false to exclude from exports
);

return $cols;
}
add_filter( 'llms_table_get_course-students_columns', 'course_students_table_columns', 10 );
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Get all the course IDs ordered by enrolled students in descending order.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion documentation for step-by-step directions on either method.
* https://lifterlms.com/docs/adding-custom-code/
*/
function llms_get_courses_ids_order_by_enrollments() {
// Get all the course IDs.
$course_ids = get_posts(
array(
'post_type' => 'course',
'posts_per_page' => -1,
'fields' => 'ids',
)
);

// Get the enrolled students count for each course.
$enrollment_counts = array();
foreach ( $course_ids as $course_id ) {
$course = new LLMS_Course( $course_id );
$enrollment_counts[ $course_id ] = $course->get_student_count();
}

// Sort the array in descending order.
arsort( $enrollment_counts );

return $enrollment_counts;
}
$desc_sorted_course_ids = llms_get_courses_ids_order_by_enrollments();
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Hiding paid access plans for students enrolled in particular memberships.
*
* You can add this recipe to your site by creating a custom plugin
* or using the Code Snippets plugin available for free in the WordPress repository.
* Read this companion documentation for step-by-step directions on either method.
* https://lifterlms.com/docs/adding-custom-code/
*/
function llms_product_is_purchasable( $purchasable, $product ) {

$user_id = get_current_user_id();
$free_access_plans = $product->get_access_plans( true );

// Get membership restrictions.
$membership_restrictions = array();
foreach ( $free_access_plans as $plan ) {
$membership_restrictions[] = $plan->get( 'availability_restrictions' );
}

// Merging the array.
$membership_restrictions = array_merge( ...$membership_restrictions );

// Check if a user is enrolled in the memberships.
foreach ( $membership_restrictions as $membership_id ) {
if ( llms_is_user_enrolled( $user_id, $membership_id ) ) {
$purchasable = false;
}
}

return $purchasable;
}

add_filter( 'llms_product_is_purchasable', 'llms_product_is_purchasable', 10, 2 );