Skip to content

Instantly share code, notes, and snippets.

@ideadude
Forked from strangerstudios/my_pmpro_pre_handle_404.php
Last active November 22, 2017 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ideadude/19909df1a14c63c2880da79cb531945a to your computer and use it in GitHub Desktop.
Save ideadude/19909df1a14c63c2880da79cb531945a 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;
//make sure we're 404ing
if(!is_404())
return;
//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