Must-Use plugins in WordPress. Creating a Simple WordPress Plugin

This main file in your WordPress theme. Located in /wp-content/themes/(name of your theme here)/functions.php.
It defines important properties of the theme, customizes hooks, appearance and its functionality, and also adds some of the functions you need. This file is downloaded every time you open any WordPress pages, so you can use it to change any element of the site. In this regard, many tips a la “ how to change something in WordPress without plugins"often relate specifically to making changes to functions.php, instead of creating a separate plugin for this functionality or using ready-made solution. This often leads to information overload this file, the code becomes difficult to parse, and making corrections even more difficult. But this is not the most dangerous thing. The most dangerous thing is that when you change the active theme, some or all of the necessary functionality of the site will disappear.

What is the difference between functions.php and a plugin?

Nothing. At its core, functions.php is a kind of global non-disabled plugin that is tied to the current theme. You can see how it is connected in WordPress in wp-settings.php. As can be seen from source code, it is loaded after all plugins, however, this does not provide any disadvantages or advantages, except perhaps the ability to override something in the connected plugins. This will also not affect the speed of code execution. Only the content of plugins and functions.php is affected. Therefore, be careful when choosing active plugins for your theme and discard unnecessary ones that are of little use to you, then you can make your site easier and speed up its operation.

When to use functions.php

Follow this rule: if the functionality is directly related to the current theme, but not to the operation of the site, write it in functions.php.

For example, it could be

  • Setting up thumbnails
  • Setting sidebar sizes
  • Setting up places for widgets
  • Declaring places under the navigation menu
  • Theme settings
  • Additional features of your theme
When to Avoid Using Functions.php

If the functionality must work even when the active theme is changed, you should take care of moving it into a separate plugin.
This may include:

  • Determination of traffic counters (Google Analytics, Yandex.Metrika, Liveinternet)
  • Setting up additional admin functionality (for example)
  • Source code configuration()
  • Defining Shortcodes
  • Registration

The lists are incomplete, you can determine their contents for yourself.

Where to deposit this code if not in functions.php? You can write special plugins for them, however, there is a more interesting and simpler way.

mu-plugins as an alternative to functions.php

Visit us at modern versions WordPress from WordPress MU(Multi-User) has come with an interesting functionality called MU Plugins. Its essence was as follows. WordPress Administrator MU sometimes needed to define plugins for an entire network of sites. This could not be achieved with the usual functionality, so a special section was introduced: /wp-content/mu-plugins/, where they were defined. Another interesting thing is that plugin files from this directory are loaded before all others, which makes it possible to predefine some constants or settings.
Later, WPMU was abolished, its code was integrated with the main blog, and now any WordPress can use the MU-plugins functionality, which now stands for Must Use, that is, mandatory for use.

How to use mu-plugins

First you need to create a special section /wp-content/mu-plugins/
We place the necessary plugin files in it. Unlike regular plugins, there is no need to maintain special syntax, and functionality can be declared directly

Here, as an example, a file with attendance counter code has been created.
Inside this file looks like this

// ...Instead of this line, insert the counter code...