Gutenberg Editor deaktiveren

Where the code in the functions.php really belongs

12. February 2020

A white screen full of nothing appears, where before the homepage, which was developed in many hours, could be viewed. How did it come to this? In many cases by the wrong integration of a PHP snippet into the functions.php of the WordPress theme. In this article you will learn where PHP code really belongs.

This tutorial assumes that you already know how to access the functions.php. If this is not the case, you should take a closer look at the following tutorial before you continue reading: Edit functions.php correctly without paralyzing the website.

 

If you are already looking at a white screen instead of your WordPress website, the following article will help you: WordPress white screen – Help, what to do?

 

Php code belongs to functions.php

If not all, most functions.php files are written in the PHP scripting language. That is, this is not the place to include user-defined CSS or HTML (unless you know exactly what you are doing).

To understand this better, let’s look at some of the files that make up a WordPress theme:

functions.php: It is the heart of the theme. This file contains the functions of the theme. Here, for example, new widget areas can be created, post formats can be edited, or menus and their position can be determined.

style.css: A stylesheet contains CSS code and defines the design of a website. Here you can find commands for colors, spacing, fonts etc.

Templates: Template Files end like the functions.php with the suffix .php (e.g. sidebar.php, single.php, search.php etc.) These files contain both HTML and PHP code and are responsible for the display of a certain area like the sidebar, the header or the footer.

PHP code comes between the PHP tags

All PHP code must be placed between the PHP tags. This immediately tells us one important thing: if you want to insert code at the beginning of the file, it must follow after the opening PHP tag and before the closing end. The tags look like this:

 

 

//

The closing PHP tag is optional for many themes, by the way. Some themes – such as the Divi theme – do without the closing tag at the end of the file. If it should be like this with you, it’s OK! You don’t have to add the tag additionally.

comments in PHP blocks

It is not uncommon to find comments between the code blocks. These do not fulfill a primary function. Rather, they serve developers so that they can immediately see what the code block is about. The following code is from our SVG Tutorial and serves as an example:

// SVG Upload erlauben
function allow_svgimg_types($mimes) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter('upload_mimes', 'allow_svgimg_types');
  • The comment starts with two slashes. These must not be removed! If the slash is removed, the comment interrupts and can damage the entire file.

Where PHP code can be inserted

The best choice is to find an empty room. You shouldn’t necessarily look for it in the middle of functions.php, because there is a higher chance to get into conflict with another snippet. Instead, we recommend to go directly to the end of functions.php.

    • Insert your code at the end of the file after the last piece of code – but before the closing tag.

 

  • It is best if you place a few blank lines before inserting your code so that your code is clearly separated from the rest.

 

Example:

Let us assume you want to insert the following code snippet into functions.php:

function no_wordpress_errors(){
return 'Something is wrong!';
}
add_filter( 'login_errors', 'no_wordpress_errors' );

Here is an example using the codes for correct insertion presented so far:

// SVG Upload erlauben
function allow_svgimg_types($mimes) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter('upload_mimes', 'allow_svgimg_types');
function no_wordpress_errors(){
return 'Something is wrong!';
}
add_filter( 'login_errors', 'no_wordpress_errors' );

The first code snippet is followed by two blank lines and then the second snippet. Both snippets can also be inserted one after the other, without blank lines. This way, however, it is easier to distinguish them.

Here, the snippet was placed at the end of the file, but after the closing (?>) tag. PHP code must be placed inside the PHP tags, so this example might cause errors.

?>
function no_wordpress_errors(){
return 'Something is wrong!';
}
add_filter( 'login_errors', 'no_wordpress_errors' );

So it would be right at the end of the functions.php file. The code was placed before the closing PHP tag.

function no_wordpress_errors(){
return 'Something is wrong!';
}
add_filter( 'login_errors', 'no_wordpress_errors' );
?>

Avoid curly apostrophe and quotation marks

This error happens frequently and is totally annoying. Because even if the code has been correctly integrated, the change is not accepted. What causes the curved apostrophe and quotation marks?  Code should never be inserted and edited in Word or similar. It should also not be sent by email or messenger. Why not? The problem is that unwanted formatting can occur, like the curved quotation marks. To edit or redistribute code, TextEdit on the Mac or Notepad on the Windows computer can be used. If you don’t have both programs, you can download Atom – a free program especially for coding.

Here the difference is shown again:

Correct

function no_wordpress_errors(){
return 'Something is wrong!';
}
add_filter( 'login_errors', 'no_wordpress_errors' );
Falsch
function no_wordpress_errors(){
return ‘Something is wrong!‘;
}
add_filter( ‘login_errors‘, ‘no_wordpress_errors‘ );

Include code in functions.php correctly – Conclusion

 

The functions.php is a little sensitive. It is recommended to make a back-up before making changes. It is not necessary to back up the whole website. Just load the functions.php on your computer to make the changes separately from the original. We have described this procedure in the following article: functions.php edit without shutting down the website

Any questions? Suggestions for improvement or suggestions for further articles? Then write it in the comments.

SIE KÖNNEN SICH BEI UNS BEDANKEN!

Sofern Ihnen der Artikel geholfen hat, können Sie sich mit wenig Aufwand revanchieren!

 

Bitte hinterlassen Sie uns eine positive Bewertung.

 

Falls Sie Fehler entdecken oder ein Artikel unvollständig ist, freuen wir uns über einen Kommentar.

Bewertung erstellen

Comments

Tell us what you think! Reply now

Your email address will not be published.