Understanding Hooks and Filters
WordPress and LearnPress use a system of hooks and filters to allow developers to modify functionality and appearance:
- Action Hooks – Allow you to add custom content or functionality at specific points in the code
- Filter Hooks – Allow you to modify data or content before it’s displayed or processed
Action Hooks
Common Action Hooks
Hook Name | Description | Location |
---|---|---|
learn-press/before-main-content |
Executes before the main content area | templates/single-course.php, templates/archive-course.php |
learn-press/after-main-content |
Executes after the main content area | templates/single-course.php, templates/archive-course.php |
learn-press/course-content-summary |
Executes within the course content summary | templates/single-course/content.php |
learn-press/before-checkout-form |
Executes before the checkout form | templates/checkout/form.php |
learn-press/after-checkout-form |
Executes after the checkout form | templates/checkout/form.php |
learn-press/before-course-item-content |
Executes before course item content | templates/single-course/content-item.php |
learn-press/after-course-item-content |
Executes after course item content | templates/single-course/content-item.php |
learn-press/before-profile-content |
Executes before profile content | templates/profile/content.php |
learn-press/after-profile-content |
Executes after profile content | templates/profile/content.php |
Filter Hooks
Hook Name | Description | Parameters |
---|---|---|
learn-press/course-content-summary |
Modifies the course content summary | $content, $course |
learn_press_get_template |
Modifies the template file path | $template_name, $args, $template_path, $default_path |
learn-press/course-tabs |
Modifies the course tabs | $tabs, $course |
learn-press/profile-tabs |
Modifies the profile tabs | $tabs, $user |
learn-press/course-item-class |
Modifies the CSS classes for course items | $classes, $item_id, $course_id |
learn-press/filter-courses/type/fields |
Modifies the course type filter options | $filter_types |
learn-press/course-object-query-args |
Modifies the course query arguments | $args |
Examples
Example 1: Modifying Course Query
// Add to functions.php
function modify_course_query_args($args) {
// Only show courses from a specific category
$args['tax_query'] = array(
array(
'taxonomy' => 'course_category',
'field' => 'term_id',
'terms' => array(123),
'operator' => 'IN',
'include_children' => false
)
);
// Change the number of courses per page
$args['posts_per_page'] = 12;
return $args;
}
add_filter('learn-press/course-object-query-args', 'modify_course_query_args');
Example 2: Adding Custom Course Tabs
// Add to functions.php
function add_custom_course_tab($tabs, $course) {
// Add a new "Resources" tab
$tabs['resources'] = array(
'title' => __('Resources', 'your-text-domain'),
'priority' => 40,
'callback' => 'display_course_resources_tab'
);
return $tabs;
}
add_filter('learn-press/course-tabs', 'add_custom_course_tab', 10, 2);
// Callback function to display tab content
function display_course_resources_tab() {
$course = LP_Global::course();
echo 'Course Resources
';
echo '';
echo '- Resource 1
';
echo '- Resource 2
';
echo '- Resource 3
';
echo '
';
}
Example 3: Customizing Course Type Labels
// Add to functions.php
function modify_course_type_labels($filter_types) {
if (isset($filter_types['online'])) {
$filter_types['online'] = __('Virtual Learning', 'your-text-domain');
}
if (isset($filter_types['offline'])) {
$filter_types['offline'] = __('In-Person Classes', 'your-text-domain');
}
return $filter_types;
}
add_filter('learn-press/filter-courses/type/fields', 'modify_course_type_labels');
Best Practices
- Use a child theme for your customizations
- Prefix your function names to avoid conflicts
- Check hook parameters carefully
- Test thoroughly after updates
- Use conditional logic when needed
Warning: Always use hooks and filters instead of modifying plugin files directly.