Skip to content

Instantly share code, notes, and snippets.

@adeel-raza
Last active June 6, 2024 21:42
Show Gist options
  • Save adeel-raza/1c225cd6fdec840206296cdeff00b214 to your computer and use it in GitHub Desktop.
Save adeel-raza/1c225cd6fdec840206296cdeff00b214 to your computer and use it in GitHub Desktop.
When using Uncanny automator sending a certificate link to user does not open the certificate due to security reasons, this snippet provides a solution for that
<?php
/*
Plugin Name: Custom Code By eLearning evolve
Description: This plugin holds the custom code implemented for this site by eLearning evolve
Version: 1.0
Author: Adeel Raza
Author URI: https://elearningevolve.com/learndash-developer
*/
/**
* Redirects users from an email link to view a certificate with the proper nonce.
*
* This function checks if the user is redirected from an email link and is logged in
* properly to view the certificate. It ensures that only administrators can view
* certificates of other users. If the conditions are met, it generates a nonce and
* redirects to the certificate view URL with the necessary query arguments.
*
* @return void
*/
function custom_redirect_to_cert_view() {
// If link already set don't come here
if ( isset( $_GET['no-redirect'] ) ) {
return;
}
// Check if the user is redirected from email and is logged in properly to view certificate
if ( ! isset( $_GET['course_id'], $_GET['cert-nonce'], $_GET['user'] ) ) {
return;
}
// Allow only admin to view the cert of other users
if ( ! current_user_can( 'administrator' ) && $_GET['user'] != get_current_user_id() ) {
return;
}
// Sanitize user input
$cert_user_id = intval( $_GET['user'] );
$course_id = intval( $_GET['course_id'] );
$view_user_id = intval( get_current_user_id() );
// Create cert_query_args
$cert_query_args = array(
'course_id' => $course_id,
'user' => $cert_user_id,
'cert-nonce' => wp_create_nonce( $course_id . $cert_user_id . $view_user_id ),
'no-redirect' => 1,
);
// Get the permalink and add query args to site URL
$redirect_url = add_query_arg( $cert_query_args, get_permalink() );
// Redirect to the new URL
wp_redirect( $redirect_url );
exit();
}
add_action( 'init', 'custom_redirect_to_cert_view' );
@adeel-raza
Copy link
Author

Place this file in your WordPress site /wp-contents/plugins folder after that activate this plugin from your list of plugins on the site "Custom Code By eLearning evolve"

Alternatively, you can place this snippet in your child theme's functions.php file if you don't need to create a plugin to place this snippet.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment