$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>url rewriting - Detect page type by url (Archive, single, page, author,...)|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

url rewriting - Detect page type by url (Archive, single, page, author,...)

matteradmin10PV0评论

Update:Really short version

How do I get $wp_query for a given url which can be anything (single, page archive, custom post type, author, year, month,...)?

TL;DR

I need a function which can return this info (as a REST API endpoint) based on a given url:

request:'category/Uncategorized'

response:
{
type:"category",
category_name:"uncategorized"
}

request:'books/harry-potter'

response:
{
type:"single",
post_type:"books",
name:"harry-potter"
}

So my app will know which template to use and what sort of data to expect.

The long story

I'm writing a Wordpress theme with ReactJS, I have my (known) routes set up with react-router, But I need to detect what sort of content I should expect if the requested url is not known at the present.

In case url is GET ed by browser(not as ajax call), I have my wordpress template file inject all the required data in the page (with $wp_query->query_vars), So with a simple url check I'll know if data is meant for current url and if so, I'll use it.

The problem comes in when user navigates through and next pages load by calling respective API endpoints.

It becomes worse if I want to support all permalink structures, So I think I'll definitely need to resolve URLs in the back-end to check all the possibilities.

So I have a detector js which tries to see if it can detect what kind of resource is requested (by checking if url params contain category,author,...) and make the API call, in case it couldn't, it then will call the detector endpoint in which I need to tell my app what should be rendered in this page.

For example, I've currently registered a custom post type named "Team", so I already know how to handle mywebsite/team & mywebsite/team/:slug, but in case user registers a new post type, say "Books", my app will have no idea whether books is a page's slug, post's slug or a post type archive name.

Update:Really short version

How do I get $wp_query for a given url which can be anything (single, page archive, custom post type, author, year, month,...)?

TL;DR

I need a function which can return this info (as a REST API endpoint) based on a given url:

request:'category/Uncategorized'

response:
{
type:"category",
category_name:"uncategorized"
}

request:'books/harry-potter'

response:
{
type:"single",
post_type:"books",
name:"harry-potter"
}

So my app will know which template to use and what sort of data to expect.

The long story

I'm writing a Wordpress theme with ReactJS, I have my (known) routes set up with react-router, But I need to detect what sort of content I should expect if the requested url is not known at the present.

In case url is GET ed by browser(not as ajax call), I have my wordpress template file inject all the required data in the page (with $wp_query->query_vars), So with a simple url check I'll know if data is meant for current url and if so, I'll use it.

The problem comes in when user navigates through and next pages load by calling respective API endpoints.

It becomes worse if I want to support all permalink structures, So I think I'll definitely need to resolve URLs in the back-end to check all the possibilities.

So I have a detector js which tries to see if it can detect what kind of resource is requested (by checking if url params contain category,author,...) and make the API call, in case it couldn't, it then will call the detector endpoint in which I need to tell my app what should be rendered in this page.

For example, I've currently registered a custom post type named "Team", so I already know how to handle mywebsite/team & mywebsite/team/:slug, but in case user registers a new post type, say "Books", my app will have no idea whether books is a page's slug, post's slug or a post type archive name.

Share Improve this question edited Jan 18, 2017 at 16:33 Ramtin Gh asked Jan 18, 2017 at 16:21 Ramtin GhRamtin Gh 1727 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You have rewrite_rules in wp_options table. From there your app can have idea of what the wordpress is going to rewrite in future and how it is going to rewrite after index.php. You can use regex in JS with wp_options data to customise your app response.

UPDATE

Here is block of code I needed to determine the nature of current page/post to dynamically use different template based on the return-

if(!function_exists('get_nature_of_post')):
function get_nature_of_post() { 
  global $wp_query;
  $wpq = json_decode(json_encode($wp_query),true);
  $check = array("is_single","is_page","is_archive","is_author","is_category","is_tax","is_search","is_home","is_404","is_post_type_archive");

  $page_identifiers = array_intersect_key($wpq,array_flip($check));
  $page_identifiers = array_filter($page_identifiers);
  $keys = array_flip(array_keys($page_identifiers));

  $case['home'] = array_flip(array('is_home'));
  $case['search'] = array_flip(array('is_search'));
  $case['archive'] = array_flip(array('is_archive','is_post_type_archive'));
  $case['taxonomy'] = array_flip(array('is_archive','is_tax'));
  $case['single'] = array_flip(array('is_single'));
  $case['page'] = array_flip(array('is_page'));

  $home = !array_diff_key($case['home'], $keys) && !array_diff_key($keys, $case['home']);
  $archive = !array_diff_key($case['archive'], $keys) && !array_diff_key($keys, $case['archive']);
  $search = !array_diff_key($case['search'], $keys) && !array_diff_key($keys, $case['search']);
  // var_dump($archive);
  $taxonomy = !array_diff_key($case['taxonomy'], $keys) && !array_diff_key($keys, $case['taxonomy']);
  // var_dump($taxonomy);
  $single = !array_diff_key($case['single'], $keys) && !array_diff_key($keys, $case['single']);
  // var_dump($single);
  $page = !array_diff_key($case['page'], $keys) && !array_diff_key($keys, $case['page']);
  // var_dump($page);

  switch (!false) {
    case $archive: return 'archive'; break;
    case $taxonomy: return 'taxonomy'; break;
    case $single: return 'single'; break;
    case $page: return 'page'; break;
    case $search: return 'search'; break;
    case $home: return 'home'; break;
    default: return false;
  }
}
endif;
Post a comment

comment list (0)

  1. No comments so far