Forked from strangerstudios/my_pmpro_pre_handle_404.php
Created
September 4, 2018 14:34
-
-
Save andrewlimaza/fc111e8e3b97ebe4f4fb03ef19174893 to your computer and use it in GitHub Desktop.
If we 404, and the slug matches a pmpro discount code, redirect
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
/* | |
If we 404, and the slug matches a discount code, redirect | |
Add this code to a custom plugin | |
*/ | |
function my_pmpro_pre_handle_404($preempt, $wp_query) { | |
global $wpdb; | |
//bail if PMPro is not installed | |
if(empty($wpdb->pmpro_discount_codes_levels)) | |
return $preempt; | |
//look for a code | |
$possible_code = sanitize_text_field(str_replace('/', '', strtolower($_SERVER['REQUEST_URI']))); | |
$code = $wpdb->get_row("SELECT * FROM $wpdb->pmpro_discount_codes WHERE code = '" . esc_sql($possible_code) . "' LIMIT 1"); | |
//redirect to checkout for the first level found | |
if(!empty($code)) { | |
$code_level = $wpdb->get_row("SELECT * FROM $wpdb->pmpro_discount_codes_levels WHERE code_id = '" . $code->id . "' ORDER BY initial_payment DESC"); | |
//found one, redirect | |
if($code_level) { | |
wp_redirect(pmpro_url('checkout', '?level=' . $code_level->level_id . '&discount_code=' . $code->code)); | |
exit; | |
} | |
} | |
return $preempt; | |
} | |
add_filter('pre_handle_404', 'my_pmpro_pre_handle_404', 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment