Created
May 17, 2018 15:02
-
-
Save Garconis/d4c9b63a55f0495c4fb4d41c4af14920 to your computer and use it in GitHub Desktop.
WordPress | Create a shortcode of a link based on hashed date and user role
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php //remove this line if using in Snippets | |
// Usage: [auth_app_link] | |
add_shortcode( 'auth_app_link', 'generate_auth_app_link_shortcode' ); | |
function generate_auth_app_link_shortcode( $atts, $content ) { | |
// Set variables to create an authentication token that will be passed to some other pages | |
// use a salt "key" to further hash the token | |
$salt = 'sdfj8sad9f8jasf9jsd'; | |
// grab the current date in UTC timezone with YYYYMMDD format | |
$date = date('Ymd', strtotime('Now')); | |
// add the salt in front of the date and then hash it | |
$token = md5($salt.$date); | |
// Set variables to describe the role ("translated" string) of the logged in user | |
// set string for Subscriber role | |
if( current_user_can('subscriber') ) { | |
$role = 'HgDeA01kzy'; | |
} | |
// set string for Custom role | |
if( current_user_can('custom_role') ) { | |
$role = '8ASJHd48sf'; | |
} | |
// set string for Leadership role | |
if( current_user_can('leadserhip') ) { | |
$role = 'Sddhd875d'; | |
} | |
// set string for Administrator role | |
if( current_user_can('administrator') ) { | |
$role = 'jfdsss4w3y3'; | |
} | |
// set a variable with the URL | |
$auth_link = 'https://example.com/test.php?token='. $token .'&role='. $role; | |
// return the URL | |
return '<a href="'. $auth_link .'" target="_blank" class="auth-app-link">Access the Mobile App »</a>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment