The structure of WordPress theme templates, their hierarchy and the order in which the engine works with page files. How to change the WordPress file structure WordPress structure

This post will collect information about the structure of WordPress in one sense or another. The structure of the database and tables in the database - why they are and what is stored in them. The structure of the folders, what files are in them, the purpose of these files and directories. List of roles, functions, etc.

I transferred the information from the post “WordPress notes, tricks and hacks”, which has grown so much that it needs to be split up.

  • Administrator- absolute access to managing themes, users, plugins, settings, pages, posts, categories, comments, export-import of content.
  • Editor- editing, creating, deleting your own and other people’s content, moderating comments, editing categories, deleting, editing, publishing your own and other people’s pages, posts, uploading files.
  • Author- creating, editing, publishing and deleting only your content - records. Cannot create pages. Has the rights to upload images, files and any materials.
  • Contributor- can add new content - record, without publishing rights. Can edit and delete their drafts. Cannot add images to a post, only via using HTML code containing a link to the image. Participants can also see entries in the console.
  • Subscriber- you can allow subscribers to see private posts and pages without additional plugins or code.

Table structure in wordpress database:

  • wp_commentmeta – for comment metadata
  • wp_comments – comments
  • wp_links - deprecated; stores information entered into the WordPress links section
  • wp_options – everything that is in the Options section of the admin panel is stored in this table, site settings
  • wp_postmeta – post metadata
  • wp_posts – posts, pages, their revisions and navigation points
    • id - posts, pages, revisions
    • post_author - id of the user - the author.
    • post_date - post date
    • post_date_gmt - post date in GMT
    • post_content - post content
    • post_title - post title
    • post_excerpt – post description
    • post_status – post status: publish, draft, auto-draft, inherit
    • comment_status – "open" if commenting on a post is allowed and "closed" if it is prohibited.
    • ping_status
    • post_password - password for reading the post if it is password protected
    • post_name - alias of the post that will be used in CNC links.
    • to_ping
    • pinged
    • post_modified - date of last modification of the post
    • post_modified_gmt - date of the last modification of the post in GMT
    • post_content_filtered
    • post_parent - id parent record post, if there is no parent, then the value is 0
    • guid – post URL in the form http://site/?p=id for posts or http://site/category/test/name - for pages
    • menu_order – zero for the post, page serial number, used to determine the order in which pages are displayed
    • post_type - post type, can be: post - post, page - page, revision - saved version of the page or post, attachment - media, for example an image page
    • post_mime_type
    • comment_count - number of comments on the post
  • wp_terms – mainly contains information on terms/taxonomies (category, link category, label, menu)
    • term_id - id of the term (for example categories)
    • name - term name
    • slug - how the term will be written in the link
  • wp_term_relationships – relationships between posts and categories, tags and other taxonomies
    • object_id - id of post, link
    • term_taxonomy_id - id of a category or any other taxonomy term (category, link category, label)
    • term_order - used for sorting
  • wp_term_taxonomy – describes what type of term this or that term is
    • term_taxonomy_id - taxonomy id
    • term_id - term id
    • taxonomy - taxonomy type: category, link_category, post_tag, nav_menu
    • parent - parent term, if for example a category is nested within a category
    • count - number of objects (posts, links) associated with the taxonomy
  • wp_usermeta – user rights and Additional Information about registered users
  • wp_users – all users

Wordpress File Structure

The root directory contains the following folders and folder files:

  • wp-config.php- this php file contains the database name and password, encoding, table prefix, language, cache size, you can add many other parameters to the file.
  • File.htaccess- an additional configuration file for the Apache web server, as well as similar servers. Allows you to set a large number of additional parameters and permissions for running the web server in individual directories.
  • wp-includes- WordPress core. With each update, the folder is overwritten.
  • wp-admin- CSS, JavaScript and PHP files that provide the admin console. With each update, the folder is overwritten.
  • wp-content- contains user folders and consists of folders:
    • languages ​​- contains engine translation files in .mo and .po format
    • plugins - installed plugins
    • themes- installed templates, at least one template must be installed. May contain the following folders and files:
      • index.php - template for the main page of the site, also loads the sidebar file. Required file, in the root of the template folder
      • style.css - a required file, responsible for the CSS styles of the template, in the root of the template folder
      • header.php – file responsible for outputting data in the section and top menu
      • sidebar.php – the file is responsible for generating side (additional) columns. Basically, categories, tags, and banners are displayed here.
      • footer.php – the file is responsible for displaying the footer, bottom menu, copyrights and closes HTML tags
      • single.php – is responsible for displaying individual posts.
      • page.php – is responsible for displaying individual pages (for example, “Contact”, “About us”, etc.)
      • archive.php – is responsible for displaying the archive page of records
      • category.php – generates pages that display publications by category
      • tag.php – page template that displays a list of publications by tags
      • comments.php – the file describes how comments are displayed
      • functions.php – additional file with PHP code, thanks to which you can enable or disable, add or remove certain functionality. Custom code is often added to this file if something needs to be improved.
      • /css/ - this folder may contain additional css files
      • /js/ – folder with JavaScript files
      • /images/ - the folder contains images built into the template
      • /languages/ - the folder contains theme translation files
    • uploads - media files: images, music, documents, etc.

Template Tags in WordPress

Template tags are PHP functions in WordPress for displaying information or for setting up a blog, for example wp_list_pages() - displays a list of pages in the form of links.

In the very wordpress tags templates are described in the following files:

  • wp-includes/author-template.php - author-related template tags
  • wp-includes/bookmark-template.php - template tags associated with bookmarks
  • wp-includes/category-template.php - template tags about all conditions and taxonomy, including categories and tags
  • wp-includes/comment-template.php - file for template tags for the comment section
  • wp-includes/link-template.php - template tags for links (permalinks, attachment links, archive links, etc.)
  • wp-includes/nav-menu-template.php - template tags for the navigation menu
  • wp-includes/post-template.php - template tags associated with posts
  • wp-includes/post-thumbnail-template.php - file for template tags associated with post thumbnails
  • wp-includes/general-template.php - file for other template tags that can be used anywhere

Before we dive into the topic of caching, we need to understand how WordPress works. Not only how posts are created and edited, but how several thousand lines program code intertwine with each other and create beautiful dynamic sites.

WordPress Internals

We all know how to work with WordPress. It all starts with logging into the control panel, followed by publishing, changing or uploading content, installing or updating the necessary plugins, backup and so on. But have you ever thought about how it all works?

The basic basis of all websites is HTML (from the English. HyperText Markup Language- “hypertext markup language”).

The ultimate goal of WordPress is to generate HTML pages, which happens dynamically.

The key term to understand here is "dynamically". The terms "HTML page" and "web page" are synonymous. At the most basic level, WordPress uses PHP and a database SQL data to store all your data.

So we have two objects:

  • The PHP code that makes up the core of WordPress
  • and the database, which is the memory of WordPress.

Each CMS WordPress uses one database. No more and no less. Every bit of information you have entered or will add to your site in the future is stored in the WordPress database.

This includes:

  • user login, password (encrypted with MD5) address Email and etc.;
  • all posts, pages, tags, categories and connections between them;
  • custom post types;
  • revisions, drafts and deleted entries;
  • approved comments and those awaiting moderation, as well as any spam;
  • theme customization options;
  • plugin data and much more.

But images, documents and other uploaded files are not stored in the WordPress database. They are located in the wp_content folder. Let's look at this in more detail.

All images (and other media files) that are uploaded to the site are stored in the "uploads" directory. They are distributed by year, month and day. This folder can be considered a database for all non-text data - images, PDFs, videos, MP3s and more. Access to this folder and its subfolders should also be restricted. This can be done by changing the .htaccess file, which is located in the wp_content directory.

Therefore, when creating a backup copy, you need to copy not only folders WordPress installations. You need to copy both the database and all the contents of the root directory.

Anatomy of a WordPress Query

Or as I like to call it,

What happens when someone views your site?

When someone visits your site, WordPress dynamically generates HTML code (according to CSS and JS), which is displayed as a site page. You won't see a .html extension after the URL (as you might see on some older websites) since this content is dynamically generated.

Here's what happens when you request a web page:

  1. The visitor's browser requests a web page.
  2. The WordPress core (can be considered the brain of WordPress) calls the required PHP scripts, starting with index.php.
  3. WP Core then connects to its database and retrieves data (posts, pages, comments and other information).
  4. It then combines the extracted data, data from currently active plugins and the currently active theme and generates HTML code on the fly i.e. dynamically.
  5. It then serves this dynamically generated HTML code to the visitor's browser.

Sites created on the WordPress engine have a special structure that differs from the structure

When first getting acquainted with website development, they usually talk about the hierarchical structure of the site. Its essence is that the entire site consists of many pages, the pages are combined into sections, which, in turn, into larger sections, etc. The system multi-level menu. This structure of sites is similar to the file structure of a computer, therefore it is familiar and understandable.

The structure of posting materials on the WordPress engine, used to create most of them, is not hierarchical, but network-based, so it is not clear at first glance. This may, to some extent, discourage a novice webmaster from using the WordPress CMS.

That’s why there is a need to understand how a WordPress site works.

By the way, if you want to learn how to make websites and blogs using the CMS WordPress, and with a unique design, it’s best for you to take the course "A unique site from scratch." You can get acquainted with him using the link below.

Unique website from scratch

There are three main areas in the structure of any website:

  1. External structure. It shows us the appearance of the page, the placement of individual elements and blocks relative to each other.
  2. Internal structure, that is, the structure of connections between the individual materials that form the content.
  3. A file structure showing the relationship of the files that make up the entire site.

So, what does a WordPress site look like?

It should be noted right away that it depends on the choice of template. There are a huge number of such templates and, of course, sites can differ greatly from each other. But there are also common features that we will pay attention to. This site can be taken as an example.

At the top of the page there is usually header. It may contain the site name, logo, slogan, sometimes a search bar, buttons social networks.

Below the header, and sometimes below it, is most often the main menu.

At the bottom of the page we see basement. It may contain service information. For example, about the authors of the site, copyrights. Sometimes you can find social networking buttons, some navigation elements, etc. there.

In the middle part of the page there is a feed of posts and side column (sidebar).

Posts (post)– the main part of a WordPress site. They contain the main content, what the creator of the site wanted to tell his visitors about.

Each new entry appears at the top of the tape, the old ones go down below. You can only see a limited number of entries on a page, for example ten. You can navigate to the remaining entries using the appropriate navigation buttons. Another feature of the post feed is that on the main page most often we don’t see the entire post, but only the first paragraphs. This makes it easier to select the article you need. But this already applies to internal structure site.

In addition to the post feed, the content is also located on permanent pages that do not change or move. Such pages usually contain information about the author, about the site, a site map, contact information and so on.

To the side of the tape there is side column (sidebar). There may also be various options in its location. The sidebar can be located on the left or right, there can be one or two.

For example, template Admired, applied on this site, allows you to use the following options sidebar locations:

The sidebar consists of separate widget blocks (widget). Their number and location are determined by the site administrator and are quite easily modified.

Now let's move on to the internal structure of a WordPress site. This is where the features of this engine lie. The navigation system consists of several parts.

First, the main menu. This menu takes us to permanent pages. There can be many such pages, but the menu can also be complex and multi-level. In this case we have a hierarchical structure. But the menu items do not lead to the posts, with the exception of one item that opens the main feed of posts.

To navigate through records, a different system is used, based on the concepts of “category” and “label”.

When writing another article, it is tied to some category, or even not one, but two or three. The sidebar has a widget with a list of categories, and we can select articles related to one category from the entire feed of posts.

In addition, each article is assigned tags - words that characterize this entry. There can also be several of these tags, and you can select records by tag. Tags are usually visible at the end of each article. In addition, a “Tag Cloud” widget is often placed in the sidebar, which shows all tags and allows you to select articles by clicking on these keywords.

The external structure of the page and the structure of the placement of site materials are important for them, but the file structure is not visible to them. It is mainly of interest to site developers.

Briefly about this structure. Like any website, a WordPress website consists of many files. Let's look at the most important things.

Firstly, all content is stored separately on the server in a MySQL database.

Secondly, the image files are located in separate folders.

And thirdly, the pages themselves are formed from separate PHP files. The number of these files may vary, this also depends on the choice of template, but there are basic, required files.

To see these files, you need to go to the console WordPress management choose Appearance? Edited R. WITH right side A list of all WP files will open. For example:

  • Archives
    (archive.php)

In this post we will talk about what file names are WordPress themes and each of them is responsible for displaying which page on the site. This is very important, necessary, and at the same time very simple to understand knowledge. Everyone who works with WordPress should have them. Below is a complete description of the structure of WordPress theme files and the order in which they are connected (hierarchy).

I mentioned the hierarchy of theme files in the article “Conditional Tags in WordPress”. And below is the same thing, only more detailed and clearer.

Theme file connections (theory)

Hierarchy, in this case sequential check, indicates that several file names are suitable for displaying one page on a website. Checking which file will be used is done one by one. Those. there is a list of files, each of them is checked in turn for physical existence, as soon as an existing file is found, the check stops and the found file is used as a template.

For example, we go to the page of the “Plugins” category with the plugins label and id 25 - http://example.com/category/plugins. Then to generate the code for this WordPress pages will check for the presence of the following files in turn (the check will stop at the first existing file):

  • category-plugins.php
  • category-25.php
  • category.php
  • archive.php
  • index.php

The complete scheme for all types of pages and their files looks like this:

Another diagram, perhaps it is clearer (old):

Page types and file names

Below is a list of pages and the theme files responsible for them. This section describes a picture of the theme file hierarchy, which is slightly higher.

Below is the site page and the corresponding list PHP files. Such files should be located in the theme's root folder.

Posts

Page (record page)

  • (any_name).php (when using a page template)
  • page-(post_label).php
  • page-(post_ID).php
  • page.php
  • singular.php
  • index.php

Record (post record)

  • single-post-(post_label).php
  • single-post.php
  • single.php
  • singular.php
  • index.php

Custom post type

  • (any_name).php (for tree type with template support. Since WP 4.7)
  • single-(post_type)-(post_label).php
  • single-(post_type).php
  • single.php
  • singular.php
  • index.php

The attachment

  • (start_MIME_type).php
  • (end_MIME_type).php
  • (start_MIME_type)-(end_MIME_type).php
  • attachment.php
  • single-attachment-(attachment_label).php (allows you to specify a template for a single image)
  • single-attachment.php (same as attachment.php)
  • single.php
  • singular.php
  • index.php

By start and end of a MIME type, we mean the first and last part of the MIME type, separated by / . For example, MIME type text file: "text/plain" and this means the file will be checked for text.php , then plain.php , then text-plain.php .

Archives

404 page

  • 404.php
  • index.php

Search page

  • search.php
  • index.php

Home page

  • front-page.php
  • (logic of persistent pages if the main page is selected)
  • home.php
  • index.php

Blog page

The blog page appears when the main page is set to a permanent page

  • home.php
  • index.php

Embeds

Embedding templates are used when a record is requested via the REST API. Embeddings appeared in version 4.5 and allow you to embed your posts into other people's sites. See get_post_embed_url()

  • embed-(post-type)-(post_format).php
  • embed-(post-type).php
  • embed.php

To change only the embedding content, you can create a file embed-content.php in the theme and describe the HTML there. The original HTML is in the engine file /wp-includes/theme-compat/embed-content.php

How it works

The core file wp-includes/template-loader.php is responsible for all the logic: which file to include. If you look at it, everything is described in it. But this activity is not particularly interesting, so I’ll write it down.

First of all. template-loader.php is connected after the entire WordPress environment has loaded. After the wp-load.php file has been processed and the main request has been processed, the wp() function. Those. template-loader.php is connected at the very, very end of the PHP script...

At first The template_redirect hook is triggered. In this hook you can perform some checks and, if necessary, redirect to another URL. In it you need to terminate the script using die() . Those. if this hook changes something, then the work of the template-loader.php file should end and we “fly away” to some other page.

Want to learn more about the WordPress file structure? And also about the core WordPress software, themes, plugins and all the user uploads that are stored on the site? I will talk about all this in this article.

Why do you need to know the WordPress file structure?

Many users start working with WordPress without any idea about its files and directories, which is very bad. After all, knowledge about WordPress files and directories, where what is stored and why, can help resolve many common problems without outside help.

In this article you will learn:

  • Which files and directories are root.
  • Where WordPress stores images and media uploads.
  • Where does WordPress store themes and plugins?
  • Where are the configuration files stored?

Now let's move on to exploring the WordPress file structure.

Accessing WordPress Files and Directories

To get started, log into your WordPress server using an FTP client. For more detailed information Read the instructions on how to use FTP to upload WordPress files (while writing). A simpler alternative to FTP is File Manager(web application with built-in cPanel administration panel). Once you log into WordPress via FTP or File Manager, you will see a file and directory structure something like this:

The WordPress root files are highlighted in red. It depends on these files correct work website, under no circumstances change anything in them yourself.

Here is a list of these files and folders in the root directory:

  • wp-admin
  • wp-includes
  • wp-activate.php
  • wp-blog-header.php
  • wp-comments-post.php
  • wp-config-sample.php
  • wp-cron.php
  • wp-links-opml.php
  • wp-load.php
  • wp-login.php
  • wp-mail.php
  • wp-settings.php
  • wp-signup.php
  • wp-trackback.php

Configuration files

The WordPress root directory stores some special files configurations. These files contain important settings specific to your WordPress site.

  • .htaccess is a server configuration file, WordPress uses it to manage permalinks and .
  • wp-config.php – Tells WordPress how to connect to the database. It also sets some important settings.
  • index.php is the index file that basically loads and initializes everything WordPress files when a user requests a page.

Sometimes there is a need to edit wp-config.php or .htaccess files. Be extremely careful and careful when doing this. Any minor error can make your website unavailable. That is why, before changing anything here, be sure to do backups these files. If you do not see the .htaccess file in the root directory, then read the instructions on why the .htaccess file is not visible in the root directory (being written).

Depending on your WordPress installation, you may or may not have the following files in your root directory:

  • robots.txt – contains all the instructions for crawling search bots
  • Favicon.ico – the favicon file is sometimes generated by the hosters themselves.

WordPress stores all downloads, plugins and themes in the wp-content folder.

Let's take a look inside the wp-content folder to understand how it works and what you can do here.

  • themes
  • plugins
  • uploads

WordPress stores site themes in the /wp-content/themes/ folder. You can edit the theme file, however this is generally not recommended. Once you update your website theme version, all your changes will be applied during this update. This is why it is recommended to customize the main theme.

WordPress stores all downloaded and installed plugins in the folder /wp-content/plugins/. It is not recommended to edit plugin files unless you have created the plugin yourself for the needs of the site.

In many WordPress instructions you can see the codes that are inserted into the site. It's best to add them to your child theme's functions.php file or to a site-specific plugin.

WordPress stores all images and media uploads in a folder /wp-content/uploads/. By default they are stored in folders like /year/month/. Every time you backup your site, don’t forget about this folder.

You can download copies WordPress core, themes and plugins from their sources, but if you lose your downloads folder, it will be difficult to restore it without a backup.

The wp-content directory also stores other standard folders, such as:

  • languages ​​– this folder stores all language files of non-English-language sites.
  • upgrade is a temporary folder created by WordPress when updating a version of the site.

wp-content also stores folders that are created by plugins. For example, the screenshot above shows the gallery folder created by the plugin. Some of these folders may contain very important files. For example, the “gallery” folder stores all the images. Always make backup copies of such folders to avoid losing important data.

Other folders contain files that you can safely delete. For example, W3 Total Cache or WP Super Cache can store cached files in their folders.

That's all. I hope this article helped you understand the WordPress file structure.

Don't forget to subscribe to my YouTube channel, find me on VKontakte and Twitter.

Choice