WordPress structure. Understanding the inner workings of WordPress

In this post we will talk about what names WordPress theme files have and what page on the site each of them is responsible for displaying. 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 checking, indicates that several file names are suitable for displaying one page on the site. 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 page, WordPress will check for the presence of the following files one by one (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 of 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, the MIME type of a text file is “text/plain”, which 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.

Websites 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 multi-level menu system also corresponds to this system of placing materials. 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, social network buttons.

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 feed, the old ones fall below. You can only see a limited number of entries on a page, for example ten. Other entries can be accessed using the appropriate navigation buttons. Another feature of the post feed is that on the main page most often we see not all posts, but only the first paragraphs. This makes it easier to select the article you need. But this already applies to the internal structure of the 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, site map, contact information, etc.

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, used on this site, allows you to use the following sidebar layout options:

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, it also depends on the choice of template, but there are basic, required files.

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

  • Archives
    (archive.php)

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 through the use of HTML code that contains 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 of the post's parent 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 (records, 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 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 – an 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 WordPress itself, template tags 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

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 details, read the guide on how to use FTP to upload WordPress files (working in progress). An easier alternative to FTP is File Manager (a web application with a 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. The correct operation of the site depends on these files; under no circumstances should you 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 configuration files. 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 an index file that basically loads and initializes all 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 make backup copies of 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 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 of WordPress core, themes, and plugins from their sources, but if you lose your downloads folder, it will be difficult to recover 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.

Good evening, friends and comrades!

I was preparing an article for publication on a different topic, but I was faced with the fact that the changes described again affected a file in the wp-includes folder. Let me remind you that in the article we have already edited the file from this folder - /wp_includes/taxonomy.php. Then I was confused by this moment and, again, I came to this. Therefore, before moving on, I decided to finally figure out whether it is possible to make changes to the files in the wp-includes folder?

The search gave a clear answer: You CANNOT edit files in the wp-includes folder!

WordPress core files are located in wp-includes and during an update they can be updated or deleted, depending on changes, in the next version of WP.

Of course, you can make changes to system files (the code is open), but then you have two options:

1. You are permanently denied any WordPress updates. Not a very good option, since the system is constantly evolving, security is being improved, errors from previous versions are eliminated, functionality is expanded, etc.

2. You commit all the changes you made to the kernel files, and after the update, repeat these changes again. Not good either. If you don’t immediately get rid of this bad habit of editing system files, then a lot of changes can accumulate, and repeating them each time is an additional and not very interesting activity that can lead you to the first option - refusing updates. And then, even if you are not too lazy to make your changes to the kernel every time, after the next update it may happen that your code simply will not fit into the new kernel, due to some global changes.

Conclusion - you need to quit this bad habit once and for all!

How? Increase your knowledge of WordPress. The system is designed in such a way that you can always find a change option without interfering with the kernel.

The easiest option to solve an existing problem is to find a plugin with the required functionality. And since there is a plugin, the same thing can be done without a plugin.

function my_category_order($orderby, $args) ( if($args["orderby"] == "sort") return "t.sort"; else return $orderby; ) add_filter("get_terms_orderby", "my_category_order", 10, 2);

function my_category_order($orderby, $args)

if ($ args [ "orderby" ] == "sort" )

return "t.sort" ;

else

return $orderby ;

add_filter ("get_terms_orderby" , "my_category_order" , 10 , 2 ) ;

The get_terms_orderby function passes the field for the ORDER BY condition of an sql query. And we added additional code to this function, which includes our sorting field – sort .

In points 1 and 3, everything remains unchanged, and thus, we have implemented custom sorting of categories without using a plugin. But now our code is not afraid of WordPress updates. Sorting will work for the current template; if you want to move it to another template, you need to repeat steps 2 and 3 for a new theme.

Thus, in most cases, you can avoid making changes to the files in the wp-includes folder.

I hope you will follow my example and stop editing WordPress core files. Indeed, this is a very bad practice, especially when you are not doing it for yourself, but fulfilling someone else’s order. An unsuspecting person will update WordPress and lose something useful that they paid for, and may not even realize such a “bullshit”. He will return to the old version, refuse updates, and pass on his negative experience to others.

Good luck learning WordPress! Protect yourself and your clients from stress! =)