The best way to allow plugins for a PHP application. Inserting PHP code into WordPress via widgets Retreat plugin php

The 7th version of PHP was released in recent 2015. A new round in the development of this programming language has brought many opportunities for all those who use PHP. Among the advantages of the new version, speed should be highlighted. Thus, according to the developers, the speed of scripts in PHP 7, compared to previous versions, has almost doubled. In this regard, many website owners who have older versions of PHP installed want to switch to the new one.

Why check for compatibility?

It should be noted that before changing the PHP version to a newer and faster one in the control panel of your hosting, you need to check the WordPress site for compatibility with it, namely themes and plugins. This need arises because usually in new versions of software some functions are added, and some cease to exist altogether. If a plugin or theme uses functions or methods that do not exist in the new version, then this is a sign of an error, which can disrupt the operation of the site as a whole.

How to check for compatibility with PHP 7.0? PHP Compatibility Checker Plugin

The PHP Compatibility Checker plugin allows you to scan the entire site and indicate which of its components (themes, plugins) are not compatible with the selected version of PHP. First you need to install and activate it, then go to the admin panel Tools -> PHP Compatibility.

As you can see, the plugin offers very clear settings. To start checking, you need to select the PHP version with which you want to check the site for compatibility (PHP Version block). A convenient function of the plugin is to select the status of the components being checked. There are two options to choose from: either check among active themes and plugins, or in inactive ones (field Plugin / Theme Status). To start the scan, click the Scan site again button.

The test result can also be downloaded in a text file by clicking the Download Report button.

Everyone familiar with WordPress has heard about the theme (template) functions.php file. However, not everyone understands its purpose well, seeing it only as a file that stores various PHP functions. On the Internet, like on my site, it is often suggested to add PHP code to this file. However, not every code will work for this file. Not because it won't work, but because it doesn't fit the logic of use.

Also, when editing functions.php, beginners make mistakes due to which the site stops working.

In this article I will try to consider all these points: when to use functions.php and when it is better not to do this, what errors may occur when editing functions.php.

Features functions.php

functions.php is located in the theme folder and is loaded every time, while viewing the external part of the site, in the admin panel and even during AJAX requests. There is no case when functions.php will not be included and this opens up wide opportunities for developers.

For example, the following code inserted into the theme's functions.php file will expand the theme's capabilities by enabling support for post thumbnails:

Add_action("after_setup_theme", "wp_kama_theme_setup"); function wp_kama_theme_setup())( // Thumbnail support add_theme_support("post-thumbnails"); )

Another example, the code will replace the text in the footer of the WordPress admin panel with data about the number of database requests, page generation time and memory usage:

## Data on the number of requests to the database in the admin panel add_filter("admin_footer_text", "wp_usage"); // in the admin panel add_filter("wp_footer", "wp_usage"); // on the website function wp_usage())( echo sprintf(__("SQL: %d in %s sec. %s MB", "km"), get_num_queries(), timer_stop(0, 3), round(memory_get_peak_usage() /1024/1024, 2)); )

functions.php vs plugins

- “Plugins work slower than the code in the functions.php file,” the ignorant say - this is not so!

In theory, inserting code into functions.php is the same as installing a plugin, but it's not the same thing. After all, when we change the theme, we will get a completely different functions.php and all the changes made will be lost, but the plugin will remain, no matter how much the theme is changed. For these reasons, we need to refer to the functions.php file. If the added functionality relates not only to the theme, but also to the site as a whole, then you should think about connecting it as a plugin.

The second example above is “Data on the number of queries to the database in the admin panel footer.” Logically it is not suitable for use in the functions.php file. Because if we change the template, we will lose this functionality, but it is used in the admin panel and is needed no matter what theme is used.

So let's remove it from functions.php and make it a plugin - it's easy!

To create a plugin, you need to create a file with the code below (the file name can be anything), add it to the plugin directory wp-content/plugins/ and activate the plugin in the admin panel:

. Typically, if the code you are inserting has these tags at the beginning and end, then they need to be removed. Also, the functions.php file should not display any text (HTML code or other content) on the screen. Text output is only allowed inside functions that will later be used in the template or that are attached to hooks (see below for more details).

You cannot allow any characters before , including invisible characters (line breaks), because functions.php is connected before setting http headers (such headers convey various data, for example, that this is an html document; that the utf-8 encoding is different). According to PHP rules, the content should be displayed on the screen after the headers are sent. And everything outside is the content - the text displayed on the screen, even the invisible \n character. Therefore, this text causes an error.

To avoid errors, consider 4 points: #1 Correct nesting

For example, we had this structure:

.......here is the code......... ?>

Correctly like this:

#2 No line breaks, spaces, text before

This code will cause an error:

But this one doesn't:

It’s more logical to write it like this:

It happens that a line break is placed at the very end of functions.php, and then this becomes a real problem, because everything seems to be correct, but the site does not work. Actually after?> or before there is an empty line

For this reason, many developers remove the closing ?> tag altogether; this is acceptable in PHP. I recommend always doing this:

inside PHP functions

If there is a function in functions.php, then tags can be used inside this function, for example, in order to visually highlight the HTML code inside the function:

this is the html code

The fact is that in this case the function is only registered and does not perform any actions. Everything inside a function (between ( )) does not work until this function is called, and such functions are usually called from a template or through filters, after the HTTP headers have been sent. So in this example, we can ignore line breaks and use ?> and