learndash-quiz-essay-upload-link
Filters the uploaded file data for LearnDash quiz essay uploads.
add_filter( 'learndash-quiz-essay-upload-link', $callback, 10, 1 );
Description
Allows modification of the uploaded essay file URL before it's processed. Developers can alter the `$uploaded_file` parameter to change the URL of a user's essay submission, for example, to point to a different storage location or apply custom processing. This hook fires during the retrieval and processing of user essay submissions within LearnDash.
Usage
add_filter( 'learndash-quiz-essay-upload-link', 'your_function_name', 10, 1 );
Parameters
-
$uploaded_file(mixed) - This parameter contains the file path or URL of the uploaded essay.
Return Value
The filtered value.
Examples
/**
* Filters the uploaded file link for LearnDash quiz essays.
*
* This example demonstrates how to modify or replace the default uploaded file link
* for essay questions in LearnDash quizzes. It could be used to add extra attributes
* to the link, change the displayed text, or even replace the link entirely if needed.
*
* @param string $uploaded_file The HTML string for the uploaded file link.
* @return string The modified or original HTML string for the uploaded file link.
*/
add_filter(
'learndash-quiz-essay-upload-link',
function( $uploaded_file ) {
// Example: Add a custom class to the link for styling purposes.
// We'll assume the $uploaded_file is an anchor tag (<a>).
// A more robust solution might involve DOM parsing for complex scenarios.
if ( strpos( $uploaded_file, '<a' ) !== false && strpos( $uploaded_file, '</a>' ) !== false ) {
$uploaded_file = str_replace( '<a', '<a class="custom-essay-upload-link"', $uploaded_file );
}
// Example: Change the displayed text for the link.
// This is a simple string replacement and assumes a specific link text.
// A more flexible approach would require parsing the HTML or having more context.
$uploaded_file = str_replace( __( 'View uploaded file', 'learndash' ), __( 'Download Essay Attachment', 'your-text-domain' ), $uploaded_file );
// Return the potentially modified uploaded file link HTML.
return $uploaded_file;
},
10, // Priority
1 // Accepted arguments count
);
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/reporting/learndash/frontend-reports/quiz-module-reports.php:147
public static function get_user_essay() {
$return_object = array();
$return_object['success'] = false;
$essay_url = esc_url( ultc_get_filter_var( 'essayUrl', '', INPUT_POST ) );
$essay_user_id = absint( ultc_get_filter_var( 'userId', '', INPUT_POST ) );
// Make sure we receive data
if ( empty( $essay_url ) ) {
$return_object['message'] = __( 'Essay Link Not Found', 'uncanny-learndash-reporting' );
$return_object['essayUrl'] = $essay_url;
return $return_object;
}
// Make sure we receive data
if ( empty( $essay_user_id ) ) {
$return_object['message'] = __( 'User ID Not Found', 'uncanny-learndash-reporting' );
$return_object['essayUrl'] = $essay_url;
return $return_object;
}
$user_id = get_current_user_id();
$can_view_file = false;
$slug = explode( 'essay/', $essay_url );//substr( $essay_url, strrpos( $essay_url, '/' ) + 1 );
$slug = str_replace( '/', '', $slug[1] );
global $wpdb;
$essay_post = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM {$wpdb->posts} WHERE post_author = %d AND post_name = %s",
$essay_user_id,
$slug
)
);
if ( empty( $essay_post ) ) {
return __( 'Essay answer not found.', 'uncanny-learndash-reporting' );
}
if ( ( learndash_is_admin_user( $user_id ) ) || ( absint( $essay_post->post_author ) === absint( $essay_user_id ) ) ) {
$can_view_file = true;
} elseif ( ( learndash_is_group_leader_user( $user_id ) ) && ( learndash_is_group_leader_of_user( $user_id, $essay_user_id ) ) ) {
$can_view_file = true;
}
if ( true === $can_view_file ) {
$uploaded_file = get_post_meta( $essay_post->ID, 'upload', true );
if ( ( ! empty( $uploaded_file ) ) && ( ! strstr( $essay_post->post_content, $uploaded_file ) ) ) {
$essay_post->post_content .= apply_filters( 'learndash-quiz-essay-upload-link', '<p><a target="_blank" href="' . $uploaded_file . '">' . __( 'View uploaded file', 'learndash' ) . '</a></p>' ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores
}
return wpautop( $essay_post->post_content );
} else {
return __( 'Sorry, Access Denied', 'uncanny-learndash-reporting' );
}
}