Examine Worpdress url to check if it has an endpoint

Viewed 304

Is it possible to find out if current Wordpress url is an endpoint page?

  • Pretty permalinks are in use
  • Can be asked late, after wp_head, but earlier is better

Only using:

global $wp_query;
isset( $wp_query->query_vars['articles'] )

is not working correct. I cant accept that as an answer. No links to that please.

It seems like its more into this issue. The example above returns true on archive pages where the slug is 'articles' or when custom post type using this slug or single page named 'articles' or another unique slug using 'articles' that is not a rewrite rule endpoint "pointing objective".

NOTE: It also returns true if someone has set_query_var('articles', $articles) to pass it into a template scope. (Otherwise, the provided function below did work until I stumbled upon template file plugin(s)).

The problem persists on other examples, as 'locations' or 'address' if variables are set within set_query_var().

Traversing db for all post_name only returns post and pages slugs to "remove" from the examine part. The rewrite rules for custom archives are not in the db.

As of AMP pages, the endpoint could be in the beginning or at the end of the url, so positioning conditional check is not working either.

The only help so far is this function found somewhere (cutted):

function is_endpoint(){

    if(!get_option('permalink_structure')) return false;

    global $wp;
    if(!$wp->request) return false;
    $parts = explode('/', $wp->request);
    if(empty($parts)) return false;

    // https://codex.wordpress.org/Reserved_Terms
    $reserved = array(
        'revision',
        'nav_menu_item',
        'custom_css',
        'customize_changeset',
        'action',
        'attachment',
        'attachment_id',
        .. AND ALL OTHER FROM CODEX DOCS ...
        'year'
    );

    //global $wp_query (clashes with added stuff);
    global $wp_the_query;
    foreach($parts as $maybe_endpoint){
        if(!$maybe_endpoint || !trim($maybe_endpoint)) continue;
        $maybe_endpoint = trim($maybe_endpoint);
        if(post_type_exists($maybe_endpoint)) continue;
        if(in_array($maybe_endpoint, $reserved)) continue;
        /** if(is_slug($maybe_endpoint)) continue;**/ // MAYBE SOMETHING LIKE THIS?

        /* Final conditional, all other possibilities removed: */
        if(isset($wp_the_query->query_vars[$maybe_endpoint])) {
            return true;
            break;
        }
    }
    return false;
}

But it seems it has to examine rewrite rules as well to exclude archive/ registered taxonomies slugs.

ANY possible input to this?

1 Answers

Maybe the function needs to be extended with get_post_type_archive_link logic. Without knowing the post type ahead. Examine if ( $post_type_obj->rewrite['with_front'] ) $struct = $wp_rewrite->front . $struct else $struct = $wp_rewrite->root . $struct; But please, someone else provide a more concrete answer. This is just a hint. Sorry this was ment to be a comment, not an answer. Do not Wote.

Related