Last active
June 6, 2024 08:46
-
-
Save hazratbilal0079/2754c7f91dce3e8240d960d971c50922 to your computer and use it in GitHub Desktop.
WordPress Wpnonce Generation Shortcode and Handling in Bricks Builder
This file contains 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
-- Shortcode | |
[wpnonce action='bricks-nonce-admin'] | |
-- php Snippet or Code Snippet | |
// Function to generate the _wpnonce value | |
function generate_wpnonce_shortcode($atts) { | |
// Extract shortcode attributes, if any (default to 'bricks-nonce-admin' action) | |
$atts = shortcode_atts( | |
array( | |
'action' => 'bricks-nonce-admin', | |
), | |
$atts | |
); | |
// Generate the nonce for the specified action | |
$nonce = wp_create_nonce($atts['action']); | |
// Return the nonce value | |
return $nonce; | |
} | |
// Register the shortcode [wpnonce] | |
add_shortcode('wpnonce', 'generate_wpnonce_shortcode'); | |
// Function to handle the action (example) | |
function handle_bricks_duplicate_content() { | |
if (isset($_GET['_wpnonce']) && wp_verify_nonce($_GET['_wpnonce'], 'bricks-nonce-admin')) { | |
// Nonce is valid, perform the action | |
echo "Nonce is valid. Proceeding with the action."; | |
} else { | |
// Nonce is invalid, handle the error | |
echo "Nonce is invalid. Action aborted."; | |
} | |
} | |
// Hook into an appropriate action to handle the request | |
add_action('admin_post_bricks_duplicate_content', 'handle_bricks_duplicate_content'); | |
add_action('admin_post_nopriv_bricks_duplicate_content', 'handle_bricks_duplicate_content'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment