Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save andrewlimaza/fc111e8e3b97ebe4f4fb03ef19174893 to your computer and use it in GitHub Desktop.

Select an option

Save andrewlimaza/fc111e8e3b97ebe4f4fb03ef19174893 to your computer and use it in GitHub Desktop.

Revisions

  1. @ideadude ideadude revised this gist Oct 26, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions my_pmpro_pre_handle_404.php
    Original 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");
  2. @ideadude ideadude created this gist Oct 25, 2017.
    26 changes: 26 additions & 0 deletions my_pmpro_pre_handle_404.php
    Original 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);