plugin development – Redirecting takes me the user back to the login page

Question

So I’m trying to set up a feature for the Events Manager plugin. The feature is a search bar that allows users to be searched from the database and then selected. This has all been accomplished using SELECT2 and AJAX.

But the problem is when a user is searched and then selected from the drop-down, I need to redirect to that user’s booking page. Which is the same page as the search was done on: “http://{$sitedomain}/staged/wp-admin/edit.php?post_type=event&page=events-manager-bookings”. But with this parameter added to the url: “&person_id={$the_id_of_the_selected_user}”.

On my localhost site, everything works fine. But when I uploaded the code onto the live site I couldn’t get it to work.

Everything works fine up until the redirect. Instead of redirecting to my URL. The site redirect lands on the wp-login.php?redirect_to= basically to the correct url above, with the correct user id of the selected user.

When I click login, I am still not redirected to the page I was looking for. In the console, something is logged when I log back in: “key: undefined wp-memory-fix-config-manager.js?ver=2.13:9”.

I’ve tried two methods for redirecting.

  1. Using jQuery:
   $('#search_bookings_users').on("change", function(e){
       // console.log(e)
       if($('#search_bookings_users').select2('data') !== null){
           window.location="http://domain/staged/wp-admin/edit.php?post_type=event&page=events-manager-bookings&person_id=" + $('#search_bookings_users').select2('data')[0].id; 
       };
   });
  1. using PHP form submit:

add_action('em_bookings_admin_page', 'render__em_bookings_search_bar_adria');

function render__em_bookings_search_bar_adria(){
    echo'<form action="" method="GET" name="bookings_search_form_adria"><label for"search_bookings_users">Search Users:</label><select name="search_bookings_users[]" id="search_bookings_users">
    <option value="search" selected="selected" disabled hidden>-- Search By Name Or Email --</option>
    </select><input type="submit" name="bookings_search_form_submit_adria" class="button-primary" value="Go" /></form>';
}

add_action( 'admin_enqueue_scripts', 'hsb_select2_enqueue' );
function hsb_select2_enqueue($hook){
    if('event_page_events-manager-bookings' === $hook){
        wp_enqueue_style('select2', plugins_url().'/woocommerce/assets/css/select2.css' );
        wp_enqueue_script('select2', plugins_url().'/woocommerce/assets/js/select2/select2.min.js', array('jquery') );
        wp_enqueue_script('events_manager_search_adria', get_stylesheet_directory_uri() . '/js/emSearchBarHSB.js', array( 'jquery', 'select2' ) ); 
        wp_localize_script('events_manager_search_adria', 'wp_ajax', array( 'url' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('********') ) );
    }

    if( isset($_GET['bookings_search_form_submit_adria']) ){
        $selected_users_id_adria=$_GET['search_bookings_users'][0];
        wp_safe_redirect("http://domain/staged/wp-admin/edit.php?post_type=event&page=events-manager-bookings&person_id=".$selected_users_id_adria);
        exit;       
    }
}

Both result in the same error. So at this point, I would guess this has nothing to do with the way I am redirecting. I’m not experienced enough with WordPress or PHP to figure out the problem. I just started working with both just around 2 months ago.

This is my first ever post, I can usually work things out by reading other people’s posts. If I missed something, or if you would like more information on anything please let me know. Any kind of feedback would be greatly appreciated. Thanks in advance.

0
harin1904 3 weeks 2021-10-01T20:09:31-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse