Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

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

Last updated on by
Elegant Themes
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.

//This is the opening tag of a PHP Code
<?php

//This is the closing tag of a PHP Code
?>

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.

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

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):

<?php
//Tons of mumble jumble PHP code that already exists in your PHP file is here

//User would copy and paste the snippet they found on the web like this:

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

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:

<?php
//Tons of mumble jumble PHP code that already exists in your PHP file is here

//See how we properly closed the first open PHP tag
?>
//Now we can open the PHP tag.
<?php
add_filter( 'login_headerurl', 'custom_loginlogo_url' );
function custom_loginlogo_url($url) {
	return 'http://www.wpbeginner.com';
}
?>

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:

<?php
//Tons of mumble jumble PHP code that already exists in your PHP file is here	

// New code just goes here (Notice how we stripped out the opening and closing PHP tags from the new code)

add_filter( 'login_headerurl', 'custom_loginlogo_url' );
function custom_loginlogo_url($url) {
	return 'http://www.wpbeginner.com';
}
?>

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:

function custom_loginlogo() {
echo '<style type="text/css">
h1 a {background-image: url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }
</style>';
}
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:

<?php
//Tons of mumble jumble PHP code that already exists in your PHP file is here	
?>

//Then the user paste the code here (Notice the PHP tags are already closed above):

function custom_loginlogo() {
echo '<style type="text/css">
h1 a {background-image: url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }
</style>';
}
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:

<?php
//Tons of mumble jumble PHP code that already exists in your PHP file is here	

//The new goes here
function custom_loginlogo() {
echo '<style type="text/css">
h1 a {background-image: url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }
</style>';
}
add_action('login_head', 'custom_loginlogo');

//PHP End Tag
?>

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:

function custom_function_name() { // This is the opening tag of a function

// code specific to the function goes here

} // 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:

function custom_function_name() { // This is the opening tag of a function

// code specific to the function goes here

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

} // 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:

function custom_function_name() { // This is the opening tag of a function

// code specific to the function goes here


} // This is the closing tag of a function


function custom_loginlogo() {
echo '<style type="text/css">
h1 a {background-image: url('.get_bloginfo('template_directory').'/images/login_logo.png) !important; }
</style>';
}
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.


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. Sinan İŞLER says:

    Haha noob friendly article nice :D

  2. Barbara says:

    Am I the only one wondering if my google analitics information can be pasted into my WordPress.com site??? In the past I’ve come across things suggested to be added to WordPress only to find out it does not work for my .com. So tell me. Did I go through all of the analitics set up for nothing? I hope not. Where could you paste the information in WordPress.com? There no place to be found.

    • WPBeginner Support says:

      Barbara, this guide is for self hosted WordPress.org sites. As for WordPress.com blogs, you can not add Google Analytics code or try to pass any unauthorized code into a free WordPress.com blog. Please see our guide on WordPress.com vs WordPress.org

  3. Louis Sahagun says:

    I was informed that I have to add script to the beggining of: /google_analytics_auto.js’/script/head> in order to change this from relative url to absolute url. This will fix many broken links since I recently changed the IDX to my site. After reading the above instructions, it sounds like I need to use a FTP client like Filezilla to perform this. Is this correct?

  4. Anthony says:

    Hello,

    I found your tutorial unfortunately after I broke my site. I am getting this error:

    Parse error: syntax error, unexpected T_IF in /home/content/45/7334345/html/wp-content/themes/Magazine110613/functions.php on line 31

    But I cannot access the admin panel to fix the code. Any suggestions?

    Thanks!

    • WPBeginner Support says:

      You can fix this by accessing your site using an FTP client like Filezilla. Download a fresh copy of your theme and extract it on your computer, then you need to upload the new functions.php file from your computer to to your theme directory using the FTP client.

  5. Dwight B says:

    Hi I was referred to your article from this link:

    http://cyberchimps.com/guide/make-responsive-unresponsive/

    I have a question, since I have my child theme installed and activated, is adding the PHP code as simple as going to ‘Appearance> Editor> Theme Functions (functions.php) [which is empty]‘ and pasting it?

    Or are there other files that need to be edited?

    Thanks, hope you can help.

    • Editorial Staff says:

      It is that simple, but you have to be very careful with the proper syntax that we have highlighted in the article above. If you paste the code incorrectly, then you can potentially lock yourself out of your WordPress admin. This is why we highly recommend that you know how to use FTP before modifying code through WordPress admin.

  6. Sean says:

    Wow, this is the 1st ever article I have seen on the web which acknowledges the fact that newbies who want to get things done with adding code do not necessarily know the assumptions that tutorial writers often have.

    This is a very thoughtful and great gesture on your part. Thank You.

    I do not blame the experts since they are writing form another level, but seriously, it wouldn’t hurt them or take any extra time to just add the 1 or 2 lines to make sure their code is understood by everyone.

    Right now I am looking around for the explanation of another basic thing that must be obvious to all the PHP pros but is totally lost on me, which is, what do they mean when they say “Paste the code below within the single.php within the loop” . . .

    What’s “within the loop” here and where should I paste the code snippet in my single.php file ?

    Any ideas please ?

    • WPBeginner Support says:

      Sean, in WordPress terminology, the loop refers to the WordPress Loop. It is the code that runs a loop, generating HTML for posts fetched by the SQL query run by WordPress. It starts like this:

      <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

      and ends like this:

      <?php endwhile; else: ?>
      <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
      <?php endif; ?>

      When you are asked to paste a snippet inside the loop you need to paste it between the start and end of the loop.

  7. Michael says:

    Nice, thoughtful article. There are many little things a beginner must know, like these simple errors, that are quickly taken for granted once we learn them. It’s these little things that often cause people to just give up. But that keeps some of us in business :-).

  8. Greg says:

    Thanks for this – the one questions that I have is that often I have seen that there are ‘code snippets’ that are offered, but I have NO idea which file they are to be pasted into!

    Others, like the one that talks about Displaying Custom Fields here:
    http://codex.wordpress.org/Using_Custom_Fields#Displaying_Custom_Fields

    The links to “The Loop” quickly get very unclear when they make statements like:
    The Loop should be placed in index.php
    and
    ……..in any other Templates used to display post information.<=== they must be kidding, right?

    This is where I get very frustrated. How am I supposed to know what "Any of the OTHER Templates used to display post information" is supposed to mean.

    Any guidance you could give on this would be greatly appreciated.

    • Editorial Staff says:

      Hey Greg,

      It is definitely a tough question as to where to paste the code because the answer varies based on the situation. If it is a theme based function, then you would use functions.php file (or another theme file). If it is a site based function, then you probably want to use a site-specific plugin.

      The Loop is used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags. Any HTML or PHP code contained in the Loop will be processed on each post. You read more about it here: http://codex.wordpress.org/The_Loop

      As for template files, you can get a better overview here: http://codex.wordpress.org/Template_Hierarchy#Visual_Overview

      Not all themes have all these template files. The most basic theme only needs style.css file and an index.php file. But the more customization you want, the more options you have available for you. The visual overview in the link above should explain that.

      “The loop should be placed in index.php” is the very bare bone example. Some themes have separate loop files altogether (i.e loop.php or loop-content.php, loop-quote.php etc). Then the theme author would call those loop files into other template files like single.php where they can say get_template_part( ‘loop’, ‘single’ ); which will include the loop-single.php file. Theme authors have different practices to make things work. It also depends on whether you are using a parent theme or a child theme. Because in child themes, you are mainly just working with the functions.php file and hooks available in the theme framework. Yes it gets pretty tricky when it comes to the child themes.

  9. Felipe says:

    In your very first example:

    //This is the opening tag of a PHP Code
    <?php

    the comment *before* the opening tag of a PHP Code won't run very well ;) Not a big deal as few people will paste that anywhere but as the whole guide is about learning how to mix PHP code correctly with HTML and other PHP code …

  10. scottwyden says:

    I used to make these mistakes and there was never an article like this to help me learn. Thanks for writing this up for newbies!

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.