uo_time_topic
Filters the time topic for a user, allowing modification of the topic based on raw time, post, and user ID.
add_filter( 'uo_time_topic', $callback, 10, 4 );
Description
Filters the topic ID associated with a user's course timer. Developers can modify the topic post or user ID before the timer data is retrieved or processed, allowing for custom topic logic or user context. Use with caution to avoid breaking timer functionality.
Usage
add_filter( 'uo_time_topic', 'your_function_name', 10, 4 );
Parameters
-
$uo_time_topic(mixed) - This parameter contains the time topic, which can be filtered by other plugins or themes.
-
$raw_time(mixed) - This parameter contains the calculated time for a topic for a specific user, which can be modified by filters.
-
$topic_post(mixed) - This parameter holds the raw, unformatted time data related to the topic.
-
$user_ID(mixed) - This parameter contains the WordPress post object for the current topic being processed.
Return Value
The filtered value.
Examples
add_filter( 'uo_time_topic', 'my_custom_topic_time_display', 10, 4 );
/**
* Example filter to customize the display of a topic's time.
*
* This function modifies the output of the 'uo_time_topic' filter to append
* a custom string to the time if the topic is related to a specific course
* or has a certain meta value.
*
* @param string $uo_time_topic The formatted time string (e.g., "1 hour 30 minutes").
* @param int $raw_time The raw time in seconds spent on the topic.
* @param WP_Post $topic_post The WP_Post object for the topic.
* @param int $user_ID The ID of the user.
* @return string The potentially modified time string.
*/
function my_custom_topic_time_display( $uo_time_topic, $raw_time, $topic_post, $user_ID ) {
// Check if we have a valid topic post object
if ( ! is_a( $topic_post, 'WP_Post' ) ) {
return $uo_time_topic; // Return original if topic post is invalid
}
// Example: Only apply customization if the topic belongs to a specific course ID
// Assuming there's a way to get the parent course ID for a topic.
// This might involve checking post meta or using a plugin's API.
// For this example, let's assume a meta key 'course_id' exists on the topic.
$parent_course_id = get_post_meta( $topic_post->ID, 'course_id', true );
if ( $parent_course_id && absint( $parent_course_id ) === 123 ) { // Replace 123 with your actual course ID
$uo_time_topic .= ' (Course Specific)';
}
// Example: Add a suffix if the user has spent more than a certain amount of time
if ( $raw_time > 3600 ) { // If more than 1 hour spent
$uo_time_topic .= ' - Significant Time!';
}
return $uo_time_topic;
}
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/classes/course-timer.php:1882
public static function shortcode_uo_time_topic( $attributes ) {
$request = shortcode_atts( array(
'user-id' => '',
'topic-id' => '',
), $attributes );
if ( '' === $request['user-id'] ) {
$request['user-id'] = get_current_user_id();
}
$user_ID = absint( $request['user-id'] );
$post_ID = absint( $request['topic-id'] );
$topic_post = null;
if ( '' == $request['topic-id'] ) {
global $post;
if ( ( ! is_404() && ! is_archive() && is_a( $post, 'WP_Post' ) ) ) {
if ( 'sfwd-topic' == $post->post_type ) {
$topic_post = $post;
}
}
} elseif ( $post_ID ) {
$requested_post = get_post( $post_ID );
if ( is_a( $requested_post, 'WP_Post' ) ) {
if ( 'sfwd-topic' == $requested_post->post_type ) {
$topic_post = $requested_post;
}
}
}
if ( null !== $topic_post && $user_ID > 0 ) {
$raw_time = self::get_topic_time( $topic_post, $user_ID );
$uo_time_topic = self::convert_second_to_time( $raw_time );
return apply_filters( 'uo_time_topic', $uo_time_topic, $raw_time, $topic_post, $user_ID );
}
return '';
}