Make WordPress Core

Opened 15 years ago

Closed 15 years ago

Last modified 15 years ago

#11871 closed defect (bug) (invalid)

$pagenow is wrong in frontend processing (always set to "index.php")

Reported by: codestyling's profile codestyling Owned by:
Milestone: Priority: high
Severity: critical Version: 2.9.1
Component: Template Keywords: template pagenow defect
Focuses: Cc:

Description

The global var $pagenow will be evaluated at all versions using the vars.php else path (non admin pages).
But this is the wrong place. It's only possible to set the correct processed page (template file name) at the template-loader.php after evaluation of correct template file!

In my opinion there should be a function inside the template-loader.php like:

function redefine_pagenow($template) {
	global $pagenow;
	if ( preg_match('#([^/]+\.php)([?/].*?)?$#i', $template, $self_matches) )
		$pagenow = strtolower($self_matches[1]);
	else
		$pagenow = 'index.php';
}

and the if/else cascades should use the function for redefine the correct template file been processed currently as frontend view (example excerpt):

...
	} else if ( is_single() && $template = get_single_template() ) {
		redefine_pagenow($template);
		include($template);
		return;
	} else if ( is_page() && $template = get_page_template() ) {
		redefine_pagenow($template);
		include($template);
		return;
	} else if ( is_category() && $template = get_category_template()) {
		redefine_pagenow($template);
		include($template);
		return;
...

This will permit theme/template designer to detect using the correct $pagenow var what template is been currently processed instead of always index.php

This problem is also related to older versions of WordPress.

Change History (6)

#1 @ryan
15 years ago

pagenow is not intended to show the template. Changing it to do so would break current usages.

What are you doing that requires more accuracy than the is_* functions provide?

#2 @codestyling
15 years ago

The main confusion is this code inside vars.php :

// On which page are we ?
if ( is_admin() ) {
	// wp-admin pages are checked more carefully
	preg_match('#/wp-admin/?(.*?)$#i', $PHP_SELF, $self_matches);
	$pagenow = $self_matches[1];
	$pagenow = trim($pagenow, '/');
	$pagenow = preg_replace('#\?.*?$#', '', $pagenow);
	if ( '' === $pagenow || 'index' === $pagenow || 'index.php' === $pagenow ) {
		$pagenow = 'index.php';
	} else {
		preg_match('#(.*?)(/|$)#', $pagenow, $self_matches);
		$pagenow = strtolower($self_matches[1]);
		if ( '.php' !== substr($pagenow, -4, 4) )
			$pagenow .= '.php'; // for Options +Multiviews: /wp-admin/themes/index.php (themes.php is queried)
	}
} else {
	if ( preg_match('#([^/]+\.php)([?/].*?)?$#i', $PHP_SELF, $self_matches) )
		$pagenow = strtolower($self_matches[1]);
	else
		$pagenow = 'index.php';
}

The code makes a difference between admin area and non admin area (equal to frontend visitor view only). At admin areas the $pagenow will be set properly but the else (frontend part) make no sence. At this point of time you will always get "index.php" at $pagenow so this could also be hard coded there!

If this is not intended to be so, than this place is not the correct one.

Sometimes is may be necessary to know the actual running "base" template, if you have as example includes inside your templates file fetching snippet files, that need to know exactly, what is going to be produced to change there behavior explicitly.
Furthermore why we can obtain the correct running "base" file as admin area but not at frontend ?

#3 @dd32
15 years ago

At this point of time you will always get "index.php" at $pagenow so this could also be hard coded there!

No.

index.php is the entry point for all "Pretty-Permalink" accesses.

Access it via wp-feed.php and it should be set to that.

$pagenow refers to the file that the access started with, Not the themes template file or something else..

#4 @nacin
15 years ago

Changing how this works will affect backward compatibility. There are other ways to hook into what you'd like to do, so I suggest we close this as worksforme.

If there isn't another way to do what you'd like to do (aside from using the template_redirect hook to rewrite the template-loader.php file to your liking), then this ticket should be closed as a duplicate of #11242, unless I'm reading this incorrectly.

#5 @ryan
15 years ago

  • Resolution set to invalid
  • Status changed from new to closed

As explained, index.php is correct and expected. Closing this as invalid.

#6 @ryan
15 years ago

  • Milestone 2.9.2 deleted
Note: See TracTickets for help on using tickets.