WP / Angular : Nonce incorrect on first load
-
I need any visitors to be logged in to wordpress without them knowing it, so they can further post ads.
functions.php :
function wp_auto_login() { if (!is_user_logged_in()) { $creds = array( 'user_login' => 'visitor', 'user_password' => 'xxx', 'remember' => true ); $user = wp_signon( $creds, false ); wp_set_current_user($user->ID); } } add_action('init', 'wp_auto_login');
header.php
<script> appInfo = { nonce: "<?php echo wp_create_nonce( 'wp_rest' ) ?>" } </script>
app.config.js
$httpProvider.interceptors.push([function() { return { request: function(config) { config.headers = config.headers || {}; config.headers['X-WP-Nonce'] = appInfo.nonce; return config } }; }]);
My problem : on first load, the interceptor function breaks all ajax requests (403 Forbidden). It works fine if i reload the page. Obviously it also works fine when i put header(“Refresh:0”) in the wp_auto_login() php function, but the result is very ugly (lazy reload);
I’m stuck… The only difference i see between load1 & load2, is the different nonce number. Load3 gives me the same nonce as load2. So it looks like the first generated nonce is problematic…
Any tip ?
Thank you!
Viewing 10 replies - 1 through 10 (of 10 total)
Viewing 10 replies - 1 through 10 (of 10 total)
- The topic ‘WP / Angular : Nonce incorrect on first load’ is closed to new replies.