LoginSignup
4
0

More than 5 years have passed since last update.

WordPress で is_rest()

Posted at

WordPress には is_home() とか is_singular() とかリクエストされたページがなにかを判別する関数がいろいろありますが、それが REST API であるかどうかを判別するための関数がないので書いた。

/**
 * Returns the true if the requested URL is REST API.
 *
 * @return bool
 */
function is_rest() {
    return ( defined( 'REST_REQUEST' ) && REST_REQUEST );
}

ただし、これは parse_request というアクションフックが発火したあとじゃないと常に false を返すので以下のように使ってください。

add_action( 'parse_request', function() {
    if ( ! is_rest() ) {
        // do something.
    }
}, 11 );
4
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0