Tribe Libs provides a Request helper object in order to allow easy access to common request (or server) related values, such as headers, input values, and URL/path information.
This object can be made available to your classes via direct injection in your constructor
class My_Cool_Class {
protected $request;
public function __construct( Request $request ) {
$this->request = $request;
}
}Once injected, the Request object will automatically contain the various values relating to the current request. For instance,
to access the Content-Type header, you could use:
$content_type = $this->request->header('Content-Type');The Request Object is also available via a Facade for usage in classes in which you don't have control over the Constructor (such as a Controller) in order to inject the class as you normally would.
@return \WP_QueryGet the current \WP_Query global object. Note that this always grabs the global $wp_query at the time of calling in
order to provide the most-up-to-date version of the object, so it can be used just like global $wp_query can within hooks.
$query = $this->request->query();
$posts = $query->found_posts; @return arrayGet all of the headers for this request.
$headers = $this->request->headers();
echo $headers['Content-Type'];@param string $key
@return stringGet the header value by key.
$content_type = $this->request->header('Content-Type');@param string $key
@return mixed Get the input from the Request by key. Automatically detects the method (GET, POST, JSON body) and returns the value from the correct method values.
$foobar = $this->request->input('foobar');@return arrayGet all input values from the Request. Automatically detects method and pulls in the values from there. If method is not GET and the request also has query parameters, returns both the method input and the query parameters.
$all_input = $this->request->all();@param array $keys
@return arrayReturn only input values matching the provided keys. Returns an array containing only those keys which existed (including empty values). Will not return values for non-existent keys.
$keys = [ 'foo', 'bar', 'bash' ];
$values = $this->request->only( $keys );@param array $keys
@return arrayReturn all input values except those matching the provided keys. Returns an empty array if no other values exist beyond the provided keys.
$keys = [ 'foo', 'bar' ];
$values = $this->request->except( $keys );@param string $key
@return boolDetermine whether an input matching the provided key exists in the Request. Will return true if input key exists even if it is empty.
$has_foobar = $this->request->has('foobar');
if ( $has_foobar ) {
// do thing.
}@param string $key
@return boolDetermine whether an input matching the provided key exists and is non-empty. Will return true if value is bool or 0, but
false for any empty string or null value.
$foobar_filled = $this->request->filled('foobar');
if ( $foobar_filled ) {
// do thing.
}@param bool $include_params
@return stringGet the current Request path. If $include_params is set to true, also include any Query Params in the path.
// URL is http://foobar.com/page/here?foo=bar
$path = $this->request->path();
echo $path;
// /page/here
$path_with_params = $this->request->path( true );
echo $path_with_params;
// /page/here?foo=bar@return stringGet the current Request URL. Does not contain path or any Query Parameters.
// URL is http://foobar.com/page/here?foo=bar
$url = $this->request->url();
echo $url;
// http://foobar.com@return stringGet the full current Request URL. Includes both the path and any Query Parameters.
// is http://foobar.com/page/here?foo=bar
$full_url = $this->request->full_url();
echo $full_url;
// http://foobar.com/page/here?foo=bar@param string $path
@return boolDetermine if the current Request Path matches the given pattern. Wildcards (*) can be used.
// URL is http://foobar.com/page/here?foo=bar
$is_page = $this->request->is('page/here');
// true
$is_page = $this->request->is('page/*');
// true
$is_page = $this->request->is('page');
// false