• I am trying to understand wordpress. I looked at the code for index.php under the twentynineteen theme. This is the home page of the site.

    bash-5.0# cat wp-content/themes/twentynineteen/index.php
    <?php
    get_header();
    ?>
    
    	<div id="primary" class="content-area">
    		<main id="main" class="site-main">
    
    		<?php
    		if ( have_posts() ) {
    
    			// Load posts loop.
    			while ( have_posts() ) {
    				the_post();
    				get_template_part( 'template-parts/content/content' );
    			}
    
    			// Previous/next page navigation.
    			twentynineteen_the_posts_navigation();
    
    		} else {
    
    			// If no content, include the "No posts found" template.
    			get_template_part( 'template-parts/content/content', 'none' );
    
    		}
    		?>
    
    		</main><!-- .site-main -->
    	</div><!-- .content-area -->
    
    <?php
    get_footer();

    – where is the have_posts defined? how does PHP know about it? I don’t see any imports or require statements?

    – where is the WP engine loaded? I am specifically trying to understand when someone makes a request for index.php, as far as web server is concerned, its a request for a PHP resource. How does the wordpress engine/platform kick-in?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Theme template files are loaded by WordPress, depending on the URL requested. WordPress loads all of its files first, then plugins, then the theme. So themes and plugins can use WordPress functions without having to load any of them. You can find all of the PHP functions in the Code Reference.

    The .htaccess file on the server has a section for WordPress which says if the file or folder exists, serve it, otherwise load the index.php in the WordPress folder (not the one in the theme folder). This one loads all of the settings and config file and all of WordPress, which then figures out what to show for that URL and loads the appropriate template file of the theme.

    Moderator bcworkz

    (@bcworkz)

    The page here used to be part of the WP Codex. You might find it informative. The resource linked at humanshell.net sadly does not exist anymore. It was an even more detailed run down of WP queries.

    The page I linked above starts when the non-theme index.php file Joy referred to is loaded, after .htaccess rules determined the request does not exist on the server.

    Thread Starter siddjain

    (@siddjain)

    Thanks Joy and @bcworkz.

    When I look inside the wordpress:php7.4-fpm-alpine docker image which is what I am using to run wordpress, I see that it runs the php-fpm Fast CGI server.

    Now php-fpm is a general Fast CGI server that has no relation to wordpress. So when a page like index.php is loaded, where is this server loading the wordpress core which is then processing index.php?

    Next question I have is that is the loading of wordpress core happening on every request? That would be a performance bottleneck.

    • This reply was modified 4 years, 10 months ago by siddjain.
    Moderator bcworkz

    (@bcworkz)

    A large part of the PHP files in /wp-includes/ are loaded on every request. They are all chained together starting with index.php, which loads wp-blog-header.php, which eventually loads a whole slew of files. Yes, it’s a bit of a bottle neck, but it’s not as significant as you might think. Modern servers are pretty damn fast.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Where is have_posts defined?’ is closed to new replies.