Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andrewlimaza/fc111e8e3b97ebe4f4fb03ef19174893 to your computer and use it in GitHub Desktop.
Save andrewlimaza/fc111e8e3b97ebe4f4fb03ef19174893 to your computer and use it in GitHub Desktop.
If we 404, and the slug matches a pmpro discount code, redirect
/*
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