Skip to content

Instantly share code, notes, and snippets.

@victormattosvm
Created February 23, 2022 04:53
Show Gist options
  • Select an option

  • Save victormattosvm/5ad95b733bd16108b772f070f77baad5 to your computer and use it in GitHub Desktop.

Select an option

Save victormattosvm/5ad95b733bd16108b772f070f77baad5 to your computer and use it in GitHub Desktop.
How to bypass X-WP-Nonce in WP Rest API
<?php
add_filter(
'determine_current_user',
function( $user_id ) {
$expression = '/wordpress_logged_in/';
$matches = preg_grep( $expression, array_keys( $_COOKIE ) );
foreach ( $_COOKIE as $cookie_key => $cookie_value ) {
if ( in_array( $cookie_key, $matches, true ) ) {
$logged_in_cookie = $cookie_key;
}
}
if( ! $logged_in_cookie || ! $_COOKIE[ $logged_in_cookie ] ){
return $user_id;
}
remove_action( 'auth_cookie_valid', 'rest_cookie_collect_status' );
$user_id = wp_validate_auth_cookie( $_COOKIE[ $logged_in_cookie ], 'logged_in' );
return $user_id;
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment