Advertisement
Guest User

Untitled

a guest
Sep 16th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. <?php
  2.  
  3. if ( !function_exists( 'is_rest' ) ) {
  4. /**
  5. * Checks if the current request is a WP REST API request.
  6. *
  7. * Case #1: After WP_REST_Request initialisation
  8. * Case #2: Support "plain" permalink settings
  9. * Case #3: It can happen that WP_Rewrite is not yet initialized,
  10. * so do this (wp-settings.php)
  11. * Case #4: URL Path begins with wp-json/ (your REST prefix)
  12. * Also supports WP installations in subfolders
  13. *
  14. * @returns boolean
  15. * @author matzeeable
  16. */
  17. function is_rest() {
  18. $prefix = rest_get_url_prefix( );
  19. if (defined('REST_REQUEST') && REST_REQUEST // (#1)
  20. || isset($_GET['rest_route']) // (#2)
  21. && strpos( trim( $_GET['rest_route'], '\\/' ), $prefix , 0 ) === 0)
  22. return true;
  23.  
  24. // (#3)
  25. global $wp_rewrite;
  26. if ($wp_rewrite === null) $wp_rewrite = new WP_Rewrite();
  27.  
  28. // (#4)
  29. $rest_url = wp_parse_url( trailingslashit( rest_url( ) ) );
  30. $current_url = wp_parse_url( add_query_arg( array( ) ) );
  31. return strpos( $current_url['path'], $rest_url['path'], 0 ) === 0;
  32. }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement