How do i edit functions php in wordpress?

  • You want to add a cool new feature to your WordPress site so you look up a tutorial on how to make it happen.
  • The tutorial gives you a chunk of code and tells you to paste it inside functions.php.
  • You do that.
  • Your site breaks. Maybe you get a scary error message, or just an empty white page.

I’m going to help you avoid the white screen of death.

I guess I should start this by saying that I can’t guarantee you won’t break your site. But my goal in this post is to help you understand what’s going on in functions.php so you can better navigate around the code and have a smaller chance of breaking your site!

So let’s talk a bit about how the functions.php file works.

functions.php is for PHP code

Most (if not all) of the functions.php file is in a language called PHP. That means:

  • This is not the place to put custom CSS code.
  • This is not the place for HTML (unless you know what you’re doing!).

This file is for PHP snippets and functions.

These functions change the behaviour and functionality of your site. So let’s look at some of the files that make up a WordPress theme:

  • style.css — Contains CSS code. This file changes the appearance of your site, including fonts, colours, spacing, and layouts.
  • Template Files (like index.php, archive.php, single.php, etc.) — Contain HTML and bits of PHP. These files insert content into your pages. They’re responsible for inserting the post titles in the right place, the post content in the right place, etc. Template files make text and HTML appear on your site.
  • functions.php — Adds features and functionality to your site. For example: creating new widget areas, adding support for featured images, adding extra settings to the Customizer, etc.

PHP code goes inside PHP tags

All of the PHP in the file needs to be inside PHP tags. Those look like this:

<?php

// PHP code can go in here!

?>

This immediately tells us one important thing: if you want to insert code at the top of the file, it needs to be after the opening <?php tag.

Note that the closing PHP tag is optional at the end of the file. So if you don’t see ?> at the bottom of the file, that’s okay. It’s optional.

An example chunk of code

Here’s an example of something you might find in functions.php:

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function _s_widgets_init() {
	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar', '_s' ),
		'id'            => 'sidebar-1',
		'description'   => esc_html__( 'Add widgets here.', '_s' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
}
add_action( 'widgets_init', '_s_widgets_init' );

This particular piece of code is for creating a new widget area (sidebar). Let’s dissect it a little:

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */

This is a comment. It doesn’t do anything—it’s just for explaining what the code does. Comments are a bit like footnotes.

  • Don’t insert code in the middle of a comment—it won’t work. No code inside a comment gets executed.
  • Don’t delete anything at the beginning or end of a comment. If you remove the slash at the beginning, the comment will break, and that will break your whole functions.php file. Same if you accidentally remove the slash from the end.
function _s_widgets_init() {
	// 'register_sidebar' code stuff was in here
}

This is the declaration of a new function. A function is basically a piece of code that does something. That’s vague, I know.

  • Unless you know what you’re doing, don’t mess with existing functions.
  • Don’t try to place your code inside an existing function (again, unless you know what you’re doing).
  • Make sure you wait until a function is ‘over’ before you insert your code. Look for the closing curly brace: }. That designates the end of a function.

Where to place your code

It’s a good idea to find a nice empty space. Don’t aim for the middle of your functions.php file, since you’re more likely to accidentally interfere with another piece of code there. Instead, I recommend heading straight to the end of functions.php.

  • Insert your code at the bottom of the file, after all other code. Add a few empty lines between the last bit of code on the site and your new snippet.
  • If there’s a closing PHP tag like thsi: ?> then place your code before that. Leave the > tag on the line below your code.
  • If there’s no closing PHP tag, then that’s okay. You don’t need to add one.

Examples:

Let’s say you want to add this code to your functions.php file:

add_filter( 'widget_text', 'do_shortcode' );

Here are some different examples (some wrong, some right):

WRONG

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function _s_widgets_init() {
	register_sidebar( array(

add_filter( 'widget_text', 'do_shortcode' );

		'name'          => esc_html__( 'Sidebar', '_s' ),
		'id'            => 'sidebar-1',
		'description'   => esc_html__( 'Add widgets here.', '_s' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
}
add_action( 'widgets_init', '_s_widgets_init' );

Can you see where I inserted the snippet? Smack dab in the middle of a function! This would break the whole function and cause big errors on your site.

CORRECT

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function _s_widgets_init() {
	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar', '_s' ),
		'id'            => 'sidebar-1',
		'description'   => esc_html__( 'Add widgets here.', '_s' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
}
add_action( 'widgets_init', '_s_widgets_init' );

add_filter( 'widget_text', 'do_shortcode' );

This time I found the end of the function and then inserted my code. Technically this would be okay too:

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function _s_widgets_init() {
	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar', '_s' ),
		'id'            => 'sidebar-1',
		'description'   => esc_html__( 'Add widgets here.', '_s' ),
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );
}
add_filter( 'widget_text', 'do_shortcode' );
add_action( 'widgets_init', '_s_widgets_init' );

It’s a little less organized, but the important thing is that your code is outside of the curly braces and isn’t interfering with other functions.

WRONG

?>

add_filter( 'widget_text', 'do_shortcode' );

Here, I placed the snippet at the end of the file, but after the ?> tag. PHP code needs to be inside PHP tags, so this would cause errors.

CORRECT

add_filter( 'widget_text', 'do_shortcode' );

?>

Again, our snippet is placed at at the end of the file, but this time it’s before the ?> tag. This means it’s being placed inside “PHP mode”, which is what we want!

Watch out for curly quotes!

I’ve come across people who have added the correct code to their site, but it still broke. The problem was that their perfectly valid code had curly quotes in it instead of plain quotes. Here’s the difference (it’s subtle!):

  • Curly Quote (double | single): “ ‘
  • Plain Quote (double | single): " '

Curly quotes are not valid code characters. Sometimes if you copy code from a tutorial website, the quotes may have been accidentally converted into curly quotes inside the blog post. Always double check the code you’re pasting in before you add it to your site.

WRONG

add_filter( ‘widget_text’, ‘do_shortcode’ );

CORRECT

add_filter( 'widget_text', 'do_shortcode' );

Remind yourself what your code does

When putting something into functions.php, you should leave a note to yourself to remind you what that code was for. So if you look at it two weeks from now, you’ll still remember what it does.

You can leave notes in the form of comments. There are a few different kinds:

Single line comments

You’ll most commonly see single line comments with two slashes in front.

// Enables shortcodes in widgets.
add_filter( 'widget_text', 'do_shortcode' );

Simply prefix your text with two slashes. That will turn that one line into a comment, allowing you to insert plain text. But remember, it works for one line only so this is NOT correct:

WRONG

// Enables shortcodes in widgets.
This lets me add shortcodes in text widgets.
add_filter( 'widget_text', 'do_shortcode' );

In that incorrect example, we tried adding more text on a second line, but we didn’t include the two slashes again. Text that isn’t inside a comment is now just being inserted in “PHP mode”, which will break our site, since it isn’t recognized as being valid PHP.

Multi line comments

If you want to create a comment that spans multiple lines, you can do it like this:

/* Comment starts here and keeps going...
...until we close it */
  • Begin the comment with /*
  • End the comment with */

Don’t forget to actually end it, otherwise the comment will keep going on and on forever and will probably break your site.

WRONG

/* Enables shortcodes in widgets.
This lets me add shortcodes in text widgets.
add_filter( 'widget_text', 'do_shortcode' );

This is incorrect because we’ve forgotten to end the comment!

CORRECT

/* Enables shortcodes in widgets.
This lets me add shortcodes in text widgets.*/
add_filter( 'widget_text', 'do_shortcode' );

We’ve ended the comment correctly.

Always take a backup before changing your functions.php file

You don’t need to do an entire site backup—just copy or download the functions.php file, save it on your computer, then make your changes separately. That way you always have the original file to revert back to if you mess something up!

Use FTP — not Appearance > Editor!

You should also use FTP for making your site changes—don’t use the built-in editor (Appearance > Editor). It’s super easy to undo mistakes via FTP, but if you do make a mistake while editing in Appearance > Editor, you’ll get the white screen of death and then you’ll have to figure out FTP in order to fix it!

How do I edit php files in WordPress?

Simply right click on the file and then select download from the menu. Your FTP client will now download wp-config. php file to your computer. You can open and edit it using a plain text editor program like Notepad or Text Edit.

How do I access functions php in WordPress?

Accessing functions..
Log in to the WordPress Admin interface..
In the left sidebar, hover over Appearances, then click Theme Editor..
In the right sidebar, click functions. php..

How do I customize a WordPress function?

To make it useful, you need to add your custom function to the PHP file and then upload the folder to the plugin directory of your WordPress site, usually wp-content/plugins/. If you need to add new WordPress functions, you can simply overwrite the old version with your changes.

How do I add code to functions php in WordPress?

To add your code to functions. php go to Appearance > Editor then select Theme Functions file and paste your code in the end of editor and click Update File: If there is a syntax error on that code, your site will stop working!