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.
Revisions
-
ideadude revised this gist
Oct 26, 2017 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -6,6 +6,10 @@ 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"); -
ideadude created this gist
Oct 25, 2017 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ /* 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; //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);