learndash-course-expand-before
Fires before the course expand. Fires before a LearnDash course is expanded, passing the course and user IDs.
add_action( 'learndash-course-expand-before', $callback, 10, 2 );
Description
Fires just before the LearnDash course expand button is displayed, passing the course and user IDs. This hook allows developers to add custom content or modify the course expansion logic right before it renders. Use it to influence the appearance or behavior of the course content reveal.
Usage
add_action( 'learndash-course-expand-before', 'your_function_name', 10, 2 );
Parameters
-
$course_id(int) - Course ID.
-
$user_id(int) - User ID.
Examples
/**
* Example function to perform an action before the LearnDash course expand.
* This function logs the course and user IDs when the course expand is triggered.
*
* @param int $course_id The ID of the LearnDash course.
* @param int $user_id The ID of the logged-in user.
*/
function my_learndash_course_expand_before_log( $course_id, $user_id ) {
// Log a message to the WordPress debug log when the hook fires.
// This can be useful for debugging or tracking specific user interactions.
if ( WP_DEBUG === true ) {
error_log( sprintf( 'LearnDash Course Expand Triggered: Course ID %d for User ID %d', $course_id, $user_id ) );
}
// You could also perform other actions here, such as:
// - Updating user meta related to course progress.
// - Triggering notifications.
// - Making API calls based on the course and user.
}
// Add the function to the 'learndash-course-expand-before' action hook.
// The '3' indicates that this function accepts 3 arguments (though we only use 2 in this example).
// LearnDash 3.0.0 and later versions pass $course_id, $user_id, and potentially others.
// It's best practice to specify the correct number of arguments expected by the hook.
// Based on the provided source, it expects $course_id and $user_id.
add_action( 'learndash-course-expand-before', 'my_learndash_course_expand_before_log', 10, 2 );
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/templates/single-ld30-course.php:205
* Fires before the course expand.
*
* @since 3.0.0
*
* @param int $course_id Course ID.
* @param int $user_id User ID.
*/
do_action( 'learndash-course-expand-before', $course_id, $user_id );
?>
<?php
/**
* Fires after the course content expand button.
*
* @since 3.0.0