0
  1. I installed latest XAMPP & extracted latest WordPress to this path: C:\xampp\htdocs

  2. I activated default theme: Active: Twenty Twenty

  3. I created these:

  • 1 Page called test: http://localhost:82/test/

  • 1 Post called test: http://localhost:82/2022/04/11/test/

  1. I accessed & edited the theme file called singular.php & wrote the following to see what the filename gets printed as:
<?php
echo $_SERVER["PHP_SELF"], PHP_EOL;
?>

I refreshed above 2 URLs & this php function prints /index.php & not /singular.php. WTH!

But when I insert the above code (or just ANYTHING) inside "index.php, & reload the above 2 URLs (page & post), then nothing I wrote in index.php shows up.


My direct 2 Questions are:

  1. What's going on? Why is singular.php the file being accessed while PHP prints that the current filename is /index.php?

  2. How does WordPress know that it should load singular.php (& not any other php file)? I looked at page & I even don't read this line at all:

<?php /* Template Name: Example Template */ ?>

Doesn't WordPress count on PHP filename to know which file is for Page & which for Posts?

  • Here's the theme structure:

enter image description here

  • Here's C:\xampp\htdocs.htaccess content:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

1 Answer 1

2

This is because you did not put singular.php in the URL, and if you did it wouldn't make any sense. Remember, WordPress loads the template, not the other way round, your mental model of how this works is upside down.

What's going on? Why is singular.php the file being accessed while PHP prints that the current filename is /index.php?

Because the entry point to WordPress is index.php in the root. index.php then loads WordPress, and starts up the CMS for that request. It'll look at your URL, convert it into arguments for fetching stuff from the database, then it grabs them. Then, it goes through a list of rules to figure out how to display that content, and picks a template file from your theme. So by the time it goes to load singular.php it already knows which posts there are. singular.php doesn't make it display a single post, it's the other way around, displaying a single post makes it load singular.php.

Since $_SERVER["PHP_SELF"] refers to the PHP file that was loaded by the server to handle the request, not the current file it is being run in, it will return index.php.

How does WordPress know that it should load singular.php (& not any other php file)? I looked at page & I even don't read this line at all:

Your server does not load singular.php, WordPress does. singular.php is just a PHP file that's meant to display a singular post. Take a look at the template hierarchy for the full flow diagram of WordPress decides which template to load to display what it fetched from the database.

Another way to think of the misunderstanding is this:

When somebody writes on a piece of paper, how does the ink know to put the words in the persons head? And why does it say the person not the paper when I ask who the author is?

Clearly that doesn't make sense when put in more familiar terms

2
  • I found out that singular.php is being used for both Page & Post. But inside singular.php we have this line get_template_part( 'template-parts/content', get_post_type() ); But by looking inside template-parts folder the file is just content.php. There's still so much to learn especially regarding template hierarchy, so thanks a lot to mention it. You're a great guy Tom. Thanks for taking the time to write. Do you think you can help here? wordpress.stackexchange.com/questions/404681/…
    – compliance
    Apr 11, 2022 at 13:50
  • take a look at developer.wordpress.org/themes/basics/template-hierarchy
    – Tom J Nowell
    Apr 11, 2022 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.