Pasting Snippets from the Web into WordPress

mistake-180x180
mistake-180x180

Time to time, we share tutorials on WPBeginner that involves users adding code snippets to their theme’s functions.php file. While this process is fairly easy for those who know how PHP works, it seems a bit tedious for new users. Often beginners want to accomplish the final result of the tutorial, but they lack the PHP knowledge to understand how to properly paste the code without breaking the site. There is nothing wrong with that, and if you are reading this article because you broke your site doing that, then we want to assure you that you are not the only one. All of us started at that point, and we have all made this mistake. In this article, we will show you how to avoid the most common mistakes while pasting PHP code from tutorials into your WordPress theme’s functions.php file.

Note: Theme’s functions.php file is located in /wp-content/themes/yourthemename/ folder. This is NOT the same as the core functions.php file located in the /wp-includes/ folder.

To understand how to properly paste the code, you need to understand the basic semantics of PHP.

1 //This is the opening tag of a PHP Code
2 <?php
3
4 //This is the closing tag of a PHP Code
5 ?>

The reason why you need to familiarize yourself with this is because over 95% of the issues we have fixed for our users involve pasting the code in a wrong spot. So knowing the PHP semantics are very helpful. Below are some of the most popular mistakes we see.

1st Most Common Mistake

User finds a code on the website that is wrapped around with PHP like the one below, and they paste it on a wrong spot.

1 <?php
2 add_filter( 'login_headerurl', 'custom_loginlogo_url' );
3 function custom_loginlogo_url($url) {
4     return 'http://www.wpbeginner.com';
5 }
6 ?>

Example theme’s functions.php file code (Note usually theme’s functions.php file has a lot more code, but the idea is the same):

01 <?php
02 //Tons of mumble jumble PHP code that already exists in your PHP file is here
03
04 //User would copy and paste the snippet they found on the web like this:
05
06 <?php
07 add_filter( 'login_headerurl', 'custom_loginlogo_url' );
08 function custom_loginlogo_url($url) {
09     return 'http://www.wpbeginner.com';
10 }
11 ?>
12 ?>

Now this will obviously freak WordPress out because you have a PHP tag opening without closing an existing PHP tag. There are two ways of going about and fixing this issue. The first method is that we paste the item on the right spot:

01 <?php
02 //Tons of mumble jumble PHP code that already exists in your PHP file is here
03
04 //See how we properly closed the first open PHP tag
05 ?>
06 //Now we can open the PHP tag.
07 <?php
08 add_filter( 'login_headerurl', 'custom_loginlogo_url' );
09 function custom_loginlogo_url($url) {
10     return 'http://www.wpbeginner.com';
11 }
12 ?>

Notice, how we properly pasted the PHP code after the previous one was closed.

Another method is to simply strip out the opening and closing tags from your new WordPress function. So the example would look like this:

01 <?php
02 //Tons of mumble jumble PHP code that already exists in your PHP file is here  
03
04 // New code just goes here (Notice how we stripped out the opening and closing PHP tags from the new code)
05
06 add_filter( 'login_headerurl', 'custom_loginlogo_url' );
07 function custom_loginlogo_url($url) {
08     return 'http://www.wpbeginner.com';
09 }
10 ?>

2nd Most Common Mistake

Each tutorial writer has their own style of writing. Some keep their snippets wrapped around with PHP tag which you saw in the example above. Other authors do not wrap their snippets around with PHP tags. This brings us to the second problem. If you have a code like this:

1 function custom_loginlogo() {
2 echo '<style type="text/css">
3 h1 a {background-image: url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }
4 </style>';
5 }
6 add_action('login_head', 'custom_loginlogo');

The author is assuming that you know that this code is supposed to go within the PHP tags. Some even suggest that you place their code at the bottom of your theme’s functions.php file. Example of a rookie mistake is:

01 <?php
02 //Tons of mumble jumble PHP code that already exists in your PHP file is here  
03 ?>
04
05 //Then the user paste the code here (Notice the PHP tags are already closed above):
06
07 function custom_loginlogo() {
08 echo '<style type="text/css">
09 h1 a {background-image: url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }
10 </style>';
11 }
12 add_action('login_head', 'custom_loginlogo');

If you notice the user has pasted the code outside the PHP tags. WordPress doesn’t know what the code is, thus it break. A proper code would look like this:

01 <?php
02 //Tons of mumble jumble PHP code that already exists in your PHP file is here  
03
04 //The new goes here
05 function custom_loginlogo() {
06 echo '<style type="text/css">
07 h1 a {background-image: url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }
08 </style>';
09 }
10 add_action('login_head', 'custom_loginlogo');
11
12 //PHP End Tag
13 ?>

3rd Most Common Mistake

Now that we have covered the opening and closing PHP tags issue, the last most common mistake that we see involves user pasting new snippets within an existing function.

Anatomy of a function usually looks like this:

1 function custom_function_name() { // This is the opening tag of a function
2
3 // code specific to the function goes here
4
5 } // This is the closing tag of a function

If you paste another function inside a function, then it will cause your site to break. Example of this mistake would be like:

01 function custom_function_name() { // This is the opening tag of a function
02
03 // code specific to the function goes here
04
05 function custom_loginlogo() {
06 echo '<style type="text/css">
07 h1 a {background-image: url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }
08 </style>';
09 }
10 add_action('login_head', 'custom_loginlogo');
11
12 } // This is the closing tag of a function

You cannot do this. Each functions are independent, so you cannot put the two together. Proper way of pasting would be like this:

01 function custom_function_name() { // This is the opening tag of a function
02
03 // code specific to the function goes here
04
05
06 } // This is the closing tag of a function
07
08
09 function custom_loginlogo() {
10 echo '<style type="text/css">
11 h1 a {background-image: url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }
12 </style>';
13 }
14 add_action('login_head', 'custom_loginlogo');

The above three mistakes is probably the reason why your theme broke when you copied and pasted a snippet from web into your WordPress theme’s functions.php

Other Tips

There are times when we ran into issues just to find out that the pasted code had errors. Some are really careless errors like the developer not putting ; or closing a quote. Whereas others were functional errors with the code itself or user not following the instructions properly.

It is crucial that you have the plugin activated if it is being used in the tutorial. Some tutorial authors just call the plugin function directly in their snippet without having the conditional statement to check if the plugin exists. You can blame both the user and the author for this. The author should have had the conditional statement in place, but the user should also have read the article properly and installed the required the plugin.

There are times when you encounter long snippets that you really want. For example our Gravity Forms snippet. Authors have a tendency of explaining the code to their users (which is a good thing), but it also causes errors in the code. So one thing that we do is scroll down or up to get the final code snippet rather than putting the pieces together by ourselves.

Have you encountered these mistakes? How did you dealt with them? Did you encounter an issue that we didn’t mention above? If so please share it in the comments with us.

Article from http://www.wpbeginner.com/

Beginner’s Guide to Pasting Snippets from the Web into WordPress

Time to time, we share tutorials on WPBeginner that involves users adding code snippets to their theme’s functions.php file. While this process is fairly easy for those who know how PHP works, it seems a bit tedious for new users. Often beginners want to accomplish the final result of the tutorial, but they lack the PHP knowledge to understand how to properly paste the code without breaking the site. There is nothing wrong with that, and if you are reading this article because you broke your site doing that, then we want to assure you that you are not the only one. All of us started at that point, and we have all made this mistake. In this article, we will show you how to avoid the most common mistakes while pasting PHP code from tutorials into your WordPress theme’s functions.php file.

Note: Theme’s functions.php file is located in /wp-content/themes/yourthemename/ folder. This is NOT the same as the core functions.php file located in the /wp-includes/ folder.

To understand how to properly paste the code, you need to understand the basic semantics of PHP.

1 //This is the opening tag of a PHP Code
2 <?php
3
4 //This is the closing tag of a PHP Code
5 ?>

The reason why you need to familiarize yourself with this is because over 95% of the issues we have fixed for our users involve pasting the code in a wrong spot. So knowing the PHP semantics are very helpful. Below are some of the most popular mistakes we see.

1st Most Common Mistake

User finds a code on the website that is wrapped around with PHP like the one below, and they paste it on a wrong spot.

1 <?php
2 add_filter( 'login_headerurl', 'custom_loginlogo_url' );
3 function custom_loginlogo_url($url) {
4     return 'http://www.wpbeginner.com';
5 }
6 ?>

Example theme’s functions.php file code (Note usually theme’s functions.php file has a lot more code, but the idea is the same):

01 <?php
02 //Tons of mumble jumble PHP code that already exists in your PHP file is here
03
04 //User would copy and paste the snippet they found on the web like this:
05
06 <?php
07 add_filter( 'login_headerurl', 'custom_loginlogo_url' );
08 function custom_loginlogo_url($url) {
09     return 'http://www.wpbeginner.com';
10 }
11 ?>
12 ?>

Now this will obviously freak WordPress out because you have a PHP tag opening without closing an existing PHP tag. There are two ways of going about and fixing this issue. The first method is that we paste the item on the right spot:

01 <?php
02 //Tons of mumble jumble PHP code that already exists in your PHP file is here
03
04 //See how we properly closed the first open PHP tag
05 ?>
06 //Now we can open the PHP tag.
07 <?php
08 add_filter( 'login_headerurl', 'custom_loginlogo_url' );
09 function custom_loginlogo_url($url) {
10     return 'http://www.wpbeginner.com';
11 }
12 ?>

Notice, how we properly pasted the PHP code after the previous one was closed.

Another method is to simply strip out the opening and closing tags from your new WordPress function. So the example would look like this:

01 <?php
02 //Tons of mumble jumble PHP code that already exists in your PHP file is here  
03
04 // New code just goes here (Notice how we stripped out the opening and closing PHP tags from the new code)
05
06 add_filter( 'login_headerurl', 'custom_loginlogo_url' );
07 function custom_loginlogo_url($url) {
08     return 'http://www.wpbeginner.com';
09 }
10 ?>

2nd Most Common Mistake

Each tutorial writer has their own style of writing. Some keep their snippets wrapped around with PHP tag which you saw in the example above. Other authors do not wrap their snippets around with PHP tags. This brings us to the second problem. If you have a code like this:

1 function custom_loginlogo() {
2 echo '<style type="text/css">
3 h1 a {background-image: url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }
4 </style>';
5 }
6 add_action('login_head', 'custom_loginlogo');

The author is assuming that you know that this code is supposed to go within the PHP tags. Some even suggest that you place their code at the bottom of your theme’s functions.php file. Example of a rookie mistake is:

01 <?php
02 //Tons of mumble jumble PHP code that already exists in your PHP file is here  
03 ?>
04
05 //Then the user paste the code here (Notice the PHP tags are already closed above):
06
07 function custom_loginlogo() {
08 echo '<style type="text/css">
09 h1 a {background-image: url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }
10 </style>';
11 }
12 add_action('login_head', 'custom_loginlogo');

If you notice the user has pasted the code outside the PHP tags. WordPress doesn’t know what the code is, thus it break. A proper code would look like this:

01 <?php
02 //Tons of mumble jumble PHP code that already exists in your PHP file is here  
03
04 //The new goes here
05 function custom_loginlogo() {
06 echo '<style type="text/css">
07 h1 a {background-image: url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }
08 </style>';
09 }
10 add_action('login_head', 'custom_loginlogo');
11
12 //PHP End Tag
13 ?>

3rd Most Common Mistake

Now that we have covered the opening and closing PHP tags issue, the last most common mistake that we see involves user pasting new snippets within an existing function.

Anatomy of a function usually looks like this:

1 function custom_function_name() { // This is the opening tag of a function
2
3 // code specific to the function goes here
4
5 } // This is the closing tag of a function

If you paste another function inside a function, then it will cause your site to break. Example of this mistake would be like:

01 function custom_function_name() { // This is the opening tag of a function
02
03 // code specific to the function goes here
04
05 function custom_loginlogo() {
06 echo '<style type="text/css">
07 h1 a {background-image: url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }
08 </style>';
09 }
10 add_action('login_head', 'custom_loginlogo');
11
12 } // This is the closing tag of a function

You cannot do this. Each functions are independent, so you cannot put the two together. Proper way of pasting would be like this:

01 function custom_function_name() { // This is the opening tag of a function
02
03 // code specific to the function goes here
04
05
06 } // This is the closing tag of a function
07
08
09 function custom_loginlogo() {
10 echo '<style type="text/css">
11 h1 a {background-image: url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }
12 </style>';
13 }
14 add_action('login_head', 'custom_loginlogo');

The above three mistakes is probably the reason why your theme broke when you copied and pasted a snippet from web into your WordPress theme’s functions.php

Other Tips

There are times when we ran into issues just to find out that the pasted code had errors. Some are really careless errors like the developer not putting ; or closing a quote. Whereas others were functional errors with the code itself or user not following the instructions properly.

It is crucial that you have the plugin activated if it is being used in the tutorial. Some tutorial authors just call the plugin function directly in their snippet without having the conditional statement to check if the plugin exists. You can blame both the user and the author for this. The author should have had the conditional statement in place, but the user should also have read the article properly and installed the required the plugin.

There are times when you encounter long snippets that you really want. For example our Gravity Forms snippet. Authors have a tendency of explaining the code to their users (which is a good thing), but it also causes errors in the code. So one thing that we do is scroll down or up to get the final code snippet rather than putting the pieces together by ourselves.

Have you encountered these mistakes? How did you dealt with them? Did you encounter an issue that we didn’t mention above? If so please share it in the comments with us.