3

I have an front-end ajax login on my site. I want to set the redirect url to '/services/clientarea'; for every page that is not part of the blog. For blog pages i want the login redirect to be $_SERVER['REQUEST_URI'];

Here is the code that I'm using which isn't working for me:

function ajax_login_init(){

wp_register_script('ajax-login-script', get_template_directory_uri() . '/library/js/ajax-login-script.js', array('jquery') ); 
wp_enqueue_script('ajax-login-script');

global  $post;
$posttype = get_post_type($post);

if ($posttype == 'post') {
    $loginredirect = $_SERVER['REQUEST_URI'];
} else {
    $loginredirect = '/services/clientarea';
}

wp_localize_script( 'ajax-login-script', 'ajax_login_object', array( 
    'ajaxurl' => admin_url( 'admin-ajax.php' ),
    'redirecturl' => $loginredirect,
    'loadingmessage' => __('Signing in, please wait...')
));

// Enable the user with no privileges to run ajax_login() in AJAX
add_action( 'wp_ajax_nopriv_ajaxlogin', 'ajax_login' );
}

// Execute the action only if the user isn't logged in
    if (!is_user_logged_in()) {
        add_action('init', 'ajax_login_init');
}

It seems that the post type isn't loaded in the init hook. Is there any way to identify it?

7

OK, let's get some thing staight first.

Here is list of actions run during typical request. It goes like this:

  • ...
  • init
  • ...
  • parse_request
  • ...
  • parse_query
  • pre_get_posts
  • posts_selection
  • ...
  • wp

As you can see, posts are selected much later after init. To be more precise, even the request is parsed after init hook. So there is no easy way to get $post in init action, I'm afraid.

If you move your code from init to wp (or later), then you can get $post from $wp_query object.

But... I'm not sure if I understand, what are you trying to achieve with this code. If you hook your AJAX action based on queried post, it won't fire up at all, I guess. Why? AJAX call is another request (/wp-admin/admin-ajax.php) so there won't be any $post retrieved in this request, so your action won't get hooked, so it won't fire up.

I'm pretty sure this is what you really want:

function enqueue_ajax_login_scripts() {
    wp_register_script('ajax-login-script', get_template_directory_uri() . '/library/js/ajax-login-script.js', array('jquery') ); 
    wp_enqueue_script('ajax-login-script');

    // check if it's single post
    if ( is_singular('post') ) {
        $loginredirect = $_SERVER['REQUEST_URI'];
    } else {
        $loginredirect = '/services/clientarea';
    }

    wp_localize_script( 'ajax-login-script', 'ajax_login_object', array( 
        'ajaxurl' => admin_url( 'admin-ajax.php' ),
        'redirecturl' => $loginredirect,
        'loadingmessage' => __('Signing in, please wait...')
    ));
}
add_action('wp_enqueue_scripts', 'enqueue_ajax_login_scripts');

// it can be registered every time (it won't fire up unless there will be such AJAX request
add_action( 'wp_ajax_nopriv_ajaxlogin', 'ajax_login' );
  • Thanks for the explanation. I thought it would be something like this. I only want the 'redirect url' parameter of the ajax_login_object to differ depending on which page is being loaded. As this is parameter is set in php I thought it could be modified before the page is served. Trouble is, my ajax_login_init function doesn't seem to work unless hooked in with the init action. – Ralphonz Apr 26 '14 at 21:17
  • I added some code to my answer. I'm pretty sure this is what you really want to do. – Krzysiek Dróżdż Apr 27 '14 at 6:40
  • Yeah, pretty much except it needs to check not only if it's a single post but if its index.php, post archive or any page that displays blog posts so I'm still going to use global $post; $posttype = get_post_type($post); if ($posttype == 'post') to check. Thanks for the help! – Ralphonz Apr 27 '14 at 10:51
  • P.S. It's been educational too :-) – Ralphonz Apr 27 '14 at 10:51
2

While @krzysiek Answer is the correct method to follow when enqueing files, don't answer the original question of how to get post type at init hook so I'm posting the solution in here in case anyone needs it.

Basically you can't access the post variable at that hook so you need to get it by using the url. The url_to_postid() is the function you need to use and you can do something like this:

$actual_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$post_id = url_to_postid($actual_url);
$post_type = get_post_type( $post_id );

This is just a workaround in case you need it, but would be better to start messing at wp hook where the variables are already set.

  • The only problem with you code is that url_to_postid might not return valid post ID, but you ignore it ;) – Krzysiek Dróżdż Apr 17 '18 at 6:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.