{"id":37708,"date":"2022-10-25T07:00:06","date_gmt":"2022-10-25T14:00:06","guid":{"rendered":"https:\/\/dhblog.dream.press\/blog\/?p=37708"},"modified":"2026-08-22T14:53:03","modified_gmt":"2026-08-22T21:53:03","slug":"guide-to-wp-functions","status":"publish","type":"post","link":"https:\/\/www.dreamhost.com\/blog\/guide-to-wp-functions\/","title":{"rendered":"Everything You Need to Know About the WordPress functions.php File"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A WordPress theme can include one small file that changes how your whole site behaves. <b>The WordPress <i>functions.php<\/i> file \u2014 also known as the functions file \u2014 is a PHP file in your theme\u2019s folder that works much like a plugin: WordPress loads it automatically along with your active theme, and any custom PHP code you add to it runs on both the front end and the admin side of your site.<\/b> You\u2019ll find it at <i>wp-content\/themes\/your-theme\/functions.php<\/i>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">That makes the functions file a perfect place to start with <a href=\"https:\/\/www.dreamhost.com\/blog\/learn-wordpress\/\" target=\"_blank\" rel=\"noopener\">WordPress development<\/a>. By editing this one file, you can add Google Analytics to your site, create custom menus, or display a post\u2019s estimated reading time \u2014 no extra plugin required. In this guide, we\u2019ll explain how the file works, show you 2 safe ways to edit it, and share 8 practical code snippets you can copy today.<\/p>\n\n\n\n<h2 id=\"h-what-is-the-functions-php-file\" class=\"wp-block-heading\"><b>What Is the functions.php File?<\/b><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <i>functions.php<\/i> file is one of WordPress\u2019s standard theme files \u2014 technically optional, and easy to create yourself if your theme doesn\u2019t have one. To the untrained eye, it may not look like much, but the functions file is a powerful tool that enables you to do a lot of interesting things:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"1024\" height=\"379\" data-src=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-theme-support-1024x379.jpg\" alt=\"WordPress functions.php\" class=\"wp-image-37723 lazyload\" data-srcset=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-theme-support-1024x379.jpg 1024w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-theme-support-300x111.jpg 300w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-theme-support-768x284.jpg 768w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-theme-support-1536x569.jpg 1536w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-theme-support-600x222.jpg.webp 600w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-theme-support-750x278.jpg.webp 750w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-theme-support-100x37.jpg 100w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-theme-support.jpg.webp 1588w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/379;\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">The <a href=\"https:\/\/developer.wordpress.org\/themes\/core-concepts\/custom-functionality\/\" target=\"_blank\" rel=\"noopener\">WordPress Theme Handbook<\/a> describes the functions file like this:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u201cThe functions.php essentially acts like a WordPress plugin, letting you add custom PHP functions, classes, interfaces, and more. It opens up the entirety of the PHP programming language to your theme.\u201d<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In simple terms, the functions file enables you to add custom code to your site. It lets you create new functions or reference existing ones in customized ways. As the handbook points out, this makes the functions file very similar to a plugin \u2014 but there are some differences between the two.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The most important difference is that the functions file belongs to a specific theme. For a standalone active theme, WordPress loads that theme\u2019s functions.php. When a child theme is active, WordPress loads both the child and parent functions.php files. If you switch away from that theme, its custom code stops running, and a theme update can overwrite edits made directly to that theme\u2019s files.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For this reason, you should consider creating a <a href=\"https:\/\/www.dreamhost.com\/blog\/how-to-create-wordpress-child-theme\/\" target=\"_blank\" rel=\"noopener\">child theme<\/a> and adding your code to the child\u2019s functions file instead. WordPress loads a child theme\u2019s functions.php just before the parent theme\u2019s, so your customizations keep running \u2014 and survive parent theme updates.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Theme Handbook\u2019s rule of thumb is that themes should only deal with the site\u2019s design. If you\u2019re building a feature that should stick around no matter what your site looks like, it\u2019s best practice to <a href=\"https:\/\/www.dreamhost.com\/blog\/how-to-create-your-first-wordpress-plugin\/\" target=\"_blank\" rel=\"noopener\">create a plugin<\/a> for it. For design-tied tweaks, the functions file is fair game \u2014 the choice is yours. For now, let\u2019s look at the different ways you can edit your functions file!<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-where-is-the-functions-php-file-located\"><b>Where Is the functions.php File Located?<\/b><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Your theme\u2019s functions file lives in the root of the theme\u2019s own folder: <i>wp-content\/themes\/your-theme\/functions.php<\/i>. If you use a child theme, the child keeps its own separate <i>functions.php<\/i> in its folder. If the file doesn\u2019t exist, create it there and put <i>&lt;?php<\/i> on the first line before adding PHP snippets; WordPress will load it automatically.<\/p>\n\n\n\n<h2 id=\"h-how-to-edit-the-functions-file-2-methods\" class=\"wp-block-heading\"><b>How to Edit the Functions File (2 Methods)<\/b><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Editing your functions file is easy when using a standard text editor, like TextEdit or Notepad. However, before you get started, it is vitally important that you <a href=\"https:\/\/www.dreamhost.com\/blog\/how-to-back-up-wordpress-tutorial\/\" target=\"_blank\" rel=\"noopener\">create a backup<\/a> of your site and save the original, unedited <i>functions.php<\/i> file. This will enable you to restore your website if something goes wrong during the editing process. (Even safer: test your changes on a staging copy of your site first. <a href=\"https:\/\/www.dreamhost.com\/wordpress\/managed\/\" target=\"_blank\" rel=\"noopener\">DreamPress<\/a>, DreamHost\u2019s managed WordPress hosting, includes 1-Click Staging on every plan for exactly this.)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-use-the-wordpress-editor\"><b>1. Use the WordPress Editor<\/b><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you have access to the WordPress admin interface, you can edit the functions file directly from the Theme File Editor. Go to <i>Appearance &gt; Theme File Editor<\/i>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"1024\" height=\"474\" data-src=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-theme-editor-stylesheet-css-1024x474.jpg\" alt=\"WordPress Theme File Editor\" class=\"wp-image-37724 lazyload\" data-srcset=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-theme-editor-stylesheet-css-1024x474.jpg 1024w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-theme-editor-stylesheet-css-300x139.jpg 300w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-theme-editor-stylesheet-css-768x356.jpg 768w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-theme-editor-stylesheet-css-1536x712.jpg 1536w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-theme-editor-stylesheet-css-600x278.jpg.webp 600w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-theme-editor-stylesheet-css-750x347.jpg.webp 750w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-theme-editor-stylesheet-css-100x46.jpg 100w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-theme-editor-stylesheet-css.jpg.webp 1999w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/474;\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">On the right-hand side of the screen, you will see a list of all your theme files. These differ depending on which theme you use, but one of the options should be <i>Theme Functions<\/i> (<i>functions.php<\/i>).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Simply click on the file to open it in the editor:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"1024\" height=\"452\" data-src=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-theme-functions-functions-php-file-1024x452.jpg\" alt=\"WordPress Theme Editor functions.php file\" class=\"wp-image-37725 lazyload\" data-srcset=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-theme-functions-functions-php-file-1024x452.jpg 1024w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-theme-functions-functions-php-file-300x133.jpg 300w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-theme-functions-functions-php-file-768x339.jpg 768w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-theme-functions-functions-php-file-1536x678.jpg 1536w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-theme-functions-functions-php-file-600x265.jpg.webp 600w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-theme-functions-functions-php-file-750x331.jpg.webp 750w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-theme-functions-functions-php-file-100x44.jpg 100w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-theme-functions-functions-php-file.jpg.webp 1938w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/452;\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Now, you can edit the file directly. Don\u2019t forget to click on <i>Update File<\/i> at the bottom to save your changes when you\u2019re done.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One warning from <a href=\"https:\/\/wordpress.org\/documentation\/article\/appearance-theme-file-editor-screen\/\" target=\"_blank\" rel=\"noopener\">WordPress\u2019s own documentation<\/a>: the Theme File Editor doesn\u2019t make backup copies. If a change crashes your site, you can\u2019t use the editor to fix it \u2014 you\u2019ll need the second method below to restore your saved copy of the file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-access-the-file-through-ftp\"><b>2. Access the File Through FTP<\/b><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If you are unable to use the admin dashboard or prefer to configure files directly, you can also access the functions file using a <a href=\"https:\/\/www.dreamhost.com\/glossary\/hosting\/sftp\/\" target=\"_blank\" rel=\"noopener\">Secure File Transfer Protocol (SFTP) client <\/a>such as <a href=\"https:\/\/filezilla-project.org\/\" target=\"_blank\" rel=\"noopener\">FileZilla<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Open your SFTP client and enter your hosting credentials to connect to your site. To find the right file, navigate to <i>wp-content\/themes\/[the name of your theme]<\/i>. When you open this folder, you\u2019ll see the <i>functions.php<\/i> file:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"1024\" height=\"477\" data-src=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-php-file-ftp-client-1024x477.jpg\" alt=\"editing the WordPress functions.php file through an FTP client\" class=\"wp-image-37722 lazyload\" data-srcset=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-php-file-ftp-client-1024x477.jpg 1024w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-php-file-ftp-client-300x140.jpg 300w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-php-file-ftp-client-768x358.jpg 768w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-php-file-ftp-client-1536x716.jpg 1536w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-php-file-ftp-client-600x280.jpg.webp 600w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-php-file-ftp-client-750x350.jpg.webp 750w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-php-file-ftp-client-100x47.jpg 100w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-php-file-ftp-client.jpg.webp 1999w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/477;\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">All you have to do now is to edit it using your preferred text editing software. When you\u2019re done, save the file and overwrite it with the exact same name and extension.<\/p>\n\n\n\n<h2 id=\"h-8-tricks-you-can-accomplish-with-the-wordpress-functions-file\" class=\"wp-block-heading\"><b>8 Tricks You Can Accomplish With the WordPress Functions File<\/b><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You should now be ready to start editing your functions file. To get you started, we\u2019ll look at some changes that you can make. All you need to do is copy the provided code snippets and paste them on a new line at the very bottom of your functions file (don\u2019t forget to save it!).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">One tip from the Theme Handbook before you paste anything: if your functions file ends with a closing <i>?&gt;<\/i> tag, leave it out. Stray whitespace after that tag is a classic cause of the WordPress \u201cwhite screen of death,\u201d and skipping the closing tag altogether is perfectly valid PHP \u2014 and standard practice.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-1-add-google-analytics-to-your-site\"><b>1. Add Google Analytics to Your Site<\/b><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Google Analytics 4 (which <a href=\"https:\/\/support.google.com\/analytics\/answer\/11583528\" target=\"_blank\" rel=\"noopener\">replaced Universal Analytics<\/a> \u2014 standard Universal Analytics properties stopped processing data back in July 2023) tracks your site through the Google tag: a snippet of code that needs to load on every page. Because analytics should keep working when you switch themes, a plugin or site-level integration is the safer production choice. If you use functions.php, the example below hooks the tag into the active theme\u2019s header and will stop working when you switch themes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, grab your tag from Google Analytics. Go to <i>Admin &gt; Data streams<\/i>, click your web data stream, then choose <i>View tag instructions &gt; Install manually<\/i> and <a href=\"https:\/\/support.google.com\/analytics\/answer\/9539598\" target=\"_blank\" rel=\"noopener\">copy the Google tag code<\/a> \u2014 it begins with <i>&lt;!&#8211; Global tag (gtag.js) &#8211;&gt;<\/i> and already contains your tag ID (the one that usually starts with \u201cG-\u201d).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then paste this wrapper at the bottom of your functions file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">add_action('wp_head', 'wpb_add_googleanalytics');\n\nfunction wpb_add_googleanalytics() { ?&gt;\n\n&lt;!-- Replace this line with your Google tag (gtag.js) snippet --&gt;\n\n&lt;?php }<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Swap the placeholder line for the Google tag snippet you copied, save the functions file, and every page on your site will load the tag. If you\u2019re moving over from an old Universal Analytics setup, our guide to <a href=\"https:\/\/www.dreamhost.com\/blog\/universal-analytics-to-ga4\/\" target=\"_blank\" rel=\"noopener\">switching from Universal Analytics to GA4<\/a> walks through the whole migration.<\/p>\n\n\n\n<div class=\"article-newsletter article-newsletter--gradient\">\n\n\n<h2>Get Content Delivered Straight to Your Inbox<\/h2><p>Subscribe now to receive all the latest updates, delivered directly to your inbox.<\/p><form class=\"nwsl-form\" id=\"newsletter_block_\" novalidate><div class=\"messages\"><\/div><div class=\"form-group\"><label for=\"input_newsletter_block_\"><input type=\"email\"name=\"email\"id=\"input_newsletter_block_\"placeholder=\"Enter your email address\"novalidatedisabled=\"disabled\"\/><\/label><button type=\"submit\"class=\"btn btn--brand\"disabled=\"disabled\"><span>Sign Me Up!<\/span><svg width=\"21\" height=\"14\" viewBox=\"0 0 21 14\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\">\n<path d=\"M13.8523 0.42524L12.9323 1.34521C12.7095 1.56801 12.7132 1.9304 12.9404 2.14865L16.7241 5.7823H0.5625C0.251859 5.7823 0 6.03416 0 6.3448V7.6573C0 7.96794 0.251859 8.2198 0.5625 8.2198H16.7241L12.9405 11.8535C12.7132 12.0717 12.7095 12.4341 12.9323 12.6569L13.8523 13.5769C14.072 13.7965 14.4281 13.7965 14.6478 13.5769L20.8259 7.39879C21.0456 7.17913 21.0456 6.82298 20.8259 6.60327L14.6477 0.42524C14.4281 0.205584 14.0719 0.205584 13.8523 0.42524Z\" fill=\"white\"\/>\n<\/svg>\n<\/button><\/div><\/form><\/div>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-2-change-the-default-login-error-message\"><b>2. Change the Default Login Error Message<\/b><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">By default, when somebody makes an unsuccessful login attempt to a WordPress site, they\u2019ll see an error message like this:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"1024\" height=\"792\" data-src=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-default-login-error-message-1024x792.jpg\" alt=\"WordPress default login error message\" class=\"wp-image-37715 lazyload\" data-srcset=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-default-login-error-message-1024x792.jpg 1024w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-default-login-error-message-300x232.jpg 300w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-default-login-error-message-768x594.jpg 768w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-default-login-error-message-600x464.jpg.webp 600w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-default-login-error-message-750x580.jpg.webp 750w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-default-login-error-message-100x77.jpg 100w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-default-login-error-message.jpg.webp 1318w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/792;\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Unfortunately, this message is giving potential intruders information about why the attempt didn\u2019t work. To reveal less detail in the login-form response, you can change this to a generic message instead. This is not a substitute for controls such as strong passwords, multifactor authentication, and login rate limiting.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can do this easily with WordPress\u2019s <a href=\"https:\/\/developer.wordpress.org\/reference\/hooks\/login_errors\/\" target=\"_blank\" rel=\"noopener\"><i>login_errors<\/i> filter<\/a>, which controls the error messages displayed above the login form. Add the following code to your functions file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function no_wordpress_errors(){\nreturn 'Something went wrong!';\n}\nadd_filter( 'login_errors', 'no_wordpress_errors' );<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">See that <i>Something went wrong!<\/i> message on the second line? That message will now appear the next time an incorrect login attempt occurs:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"1024\" height=\"711\" data-src=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-change-default-login-error-message-1024x711.jpg\" alt=\"WordPress custom login error message\" class=\"wp-image-37717 lazyload\" data-srcset=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-change-default-login-error-message-1024x711.jpg 1024w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-change-default-login-error-message-300x208.jpg 300w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-change-default-login-error-message-768x533.jpg 768w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-change-default-login-error-message-600x417.jpg.webp 600w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-change-default-login-error-message-750x521.jpg.webp 750w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-change-default-login-error-message-100x69.jpg 100w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-change-default-login-error-message.jpg.webp 1308w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/711;\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">You can change the text to whatever you want, as long as you keep the single quote characters. Try it out with different messages to see how it works.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-3-add-the-estimated-reading-time-for-a-post\"><b>3. Add the Estimated Reading Time for a Post<\/b><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This neat trick enables you to calculate and display the estimated amount of time required to read a post. Your visitors can then get a general idea of the content\u2019s length right away.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To implement this code, you will need to make two separate edits. The first one happens within the <i>functions.php<\/i> file, where you\u2019ll want to paste the following snippet (it assumes a reading speed of 200 words per minute \u2014 change that number to taste):<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function reading_time() {\n$content = get_post_field( 'post_content', get_the_ID() );\n$word_count = str_word_count( strip_tags( $content ) );\n$readingtime = ceil($word_count \/ 200);\nif ($readingtime == 1) {\n$timer = \" minute\";\n} else {\n$timer = \" minutes\";\n}\n$totalreadingtime = $readingtime . $timer;\nreturn $totalreadingtime;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">However, this snippet only performs the calculation. You\u2019ll now need to add the following code wherever you want the results to be displayed:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">echo reading_time();<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For example, you could add it to the metadata that appears alongside each post. Every theme is constructed differently, but typically you\u2019ll find it in <i>template-parts &gt; post &gt; content.php<\/i>:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"978\" height=\"332\" data-src=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wp-functions-add-estimated-reading-time-for-wordpress-posts.jpg.webp\" alt=\"WordPress functions.php estimated reading time\" class=\"wp-image-37726 lazyload\" data-srcset=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wp-functions-add-estimated-reading-time-for-wordpress-posts.jpg.webp 978w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wp-functions-add-estimated-reading-time-for-wordpress-posts-300x102.jpg 300w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wp-functions-add-estimated-reading-time-for-wordpress-posts-768x261.jpg 768w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wp-functions-add-estimated-reading-time-for-wordpress-posts-600x204.jpg.webp 600w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wp-functions-add-estimated-reading-time-for-wordpress-posts-750x255.jpg.webp 750w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wp-functions-add-estimated-reading-time-for-wordpress-posts-100x34.jpg 100w\" data-sizes=\"(max-width: 978px) 100vw, 978px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 978px; --smush-placeholder-aspect-ratio: 978\/332;\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">The estimated reading time will now appear in each post\u2019s header alongside the date.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-4-remove-the-wordpress-version-number\"><b>4. Remove the WordPress Version Number<\/b><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Old versions of WordPress may contain security flaws that malicious hackers and bots can exploit. Removing the generator tag can hide one public version indicator, but it does not fix vulnerabilities in outdated software or replace promptly updating WordPress. This is called <a href=\"https:\/\/en.wikipedia.org\/wiki\/Security_through_obscurity\" target=\"_blank\" rel=\"noopener\">security through obscurity<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Before we move on, it\u2019s important to note that obscurity should never be your <i>only<\/i> security measure. It\u2019s more like adding an extra bulwark to your already <a href=\"https:\/\/www.dreamhost.com\/blog\/secure-your-wordpress-website\/\" target=\"_blank\" rel=\"noopener\">secure WordPress fortress<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">By default, WordPress prints a generator meta tag \u2014 including your WordPress version \u2014 in your page header via the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/wp_generator\/\" target=\"_blank\" rel=\"noopener\"><i>wp_generator<\/i> function<\/a>. Hiding it only requires adding the following code snippet to the functions file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">remove_action('wp_head', 'wp_generator');<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The generator tag \u2014 and the version number it contains \u2014 will no longer appear in your page source.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-5-automatically-update-your-copyright-notice\"><b>5. Automatically Update Your Copyright Notice<\/b><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Updating the year in your copyright notice is one of those little tasks that\u2019s easy to forget. You can edit your functions file to generate a copyright year range from the earliest and latest published posts.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Paste the following code into your functions file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function wpb_copyright() {\nglobal $wpdb;\n$copyright_dates = $wpdb-&gt;get_results(\"\nSELECT\nYEAR(min(post_date_gmt)) AS firstdate,\nYEAR(max(post_date_gmt)) AS lastdate\nFROM\n$wpdb-&gt;posts\nWHERE\npost_status = 'publish'\n\");\n$output = '';\nif($copyright_dates) {\n$copyright = \"\u00a9 \" . $copyright_dates[0]-&gt;firstdate;\nif($copyright_dates[0]-&gt;firstdate != $copyright_dates[0]-&gt;lastdate) {\n$copyright .= '-' . $copyright_dates[0]-&gt;lastdate;\n}\n$output = $copyright;\n}\nreturn $output;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Then add the following code wherever you want the copyright information to be displayed:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php echo wpb_copyright(); ?&gt;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You\u2019ll now see the dynamically updating copyright date on your site.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"1024\" height=\"92\" data-src=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-php-dynamic-auto-update-copyright-date-1024x92.png\" alt=\"A dynamically-updating copyright date on a website\" class=\"wp-image-37721 lazyload\" data-srcset=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-php-dynamic-auto-update-copyright-date-1024x92.png 1024w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-php-dynamic-auto-update-copyright-date-300x27.png 300w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-php-dynamic-auto-update-copyright-date-768x69.png 768w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-php-dynamic-auto-update-copyright-date-1536x139.png 1536w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-php-dynamic-auto-update-copyright-date-600x54.png.webp 600w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-php-dynamic-auto-update-copyright-date-750x68.png.webp 750w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-php-dynamic-auto-update-copyright-date-100x9.png 100w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-php-dynamic-auto-update-copyright-date.png.webp 1695w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/92;\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">In our case, we added the date to the <i>footer.php<\/i> file, so it would be displayed at the bottom of the page.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-6-add-custom-menus\"><b>6. Add Custom Menus<\/b><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Most themes have pre-defined <a href=\"https:\/\/www.dreamhost.com\/blog\/navigation-menu-design\/\" target=\"_blank\" rel=\"noopener\">navigation menus<\/a>. However, what if you want to create your own menu and place it wherever you want on your site?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">All you need to do is register a new menu location with the <a href=\"https:\/\/developer.wordpress.org\/reference\/functions\/register_nav_menu\/\" target=\"_blank\" rel=\"noopener\"><i>register_nav_menu()<\/i> function<\/a> by pasting this code into your functions file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function wpb_custom_new_menu() {\nregister_nav_menu('my-custom-menu',__( 'My Customized Menu' ));\n}\nadd_action( 'init', 'wpb_custom_new_menu' );<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can replace <i>\u2018My Customized Menu\u2019<\/i> with the name you want to give the menu. If you go to <i>Appearance &gt; Menus<\/i> in your admin area, you should see the new option listed on the page:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"1024\" height=\"254\" data-src=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-add-custom-menu-1024x254.jpg\" alt=\"customize your WordPress menus using the functions.php file\" class=\"wp-image-37716 lazyload\" data-srcset=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-add-custom-menu-1024x254.jpg 1024w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-add-custom-menu-300x74.jpg 300w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-add-custom-menu-768x190.jpg 768w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-add-custom-menu-600x149.jpg.webp 600w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-add-custom-menu-750x186.jpg.webp 750w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-add-custom-menu-100x25.jpg 100w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-add-custom-menu.jpg.webp 1234w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/254;\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">You can now add the new menu anywhere on your site with <i>wp_nav_menu()<\/i>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;?php\nwp_nav_menu( array(\n'theme_location' =&gt; 'my-custom-menu',\n'container_class' =&gt; 'custom-menu-class' ) );\n?&gt;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Most probably, you\u2019ll want to place this code in the <i>header.php<\/i> file. This will place the menu at the very top of your site.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-7-customize-your-excerpts\"><b>7. Customize Your Excerpts<\/b><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Excerpts are short sample descriptions of your posts that you can display on your homepage or blog feed. By default, all excerpts have the same length and link text, but you can change that.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, let\u2019s alter the text of the link that takes you from the excerpt to the full post. This is usually \u201cRead more\u201d or \u201cContinue reading,\u201d but you can make it whatever you want with the <a href=\"https:\/\/developer.wordpress.org\/reference\/hooks\/excerpt_more\/\" target=\"_blank\" rel=\"noopener\"><i>excerpt_more<\/i> filter<\/a>. Paste the following snippet into your functions file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function new_excerpt_more($more) {\nglobal $post;\nreturn '&lt;a class=\"moretag\" href=\"'. get_permalink($post-&gt;ID) . '\"&gt; Read the full article...&lt;\/a&gt;';\n}\nadd_filter('excerpt_more', 'new_excerpt_more');<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the link text has been set to <i>Read the full article&#8230;<\/i><\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"1024\" height=\"363\" data-src=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-customize-post-excerpts-1024x363.jpg\" alt=\"customize WordPress excerpts by editing the functions.php file\" class=\"wp-image-37718 lazyload\" data-srcset=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-customize-post-excerpts-1024x363.jpg 1024w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-customize-post-excerpts-300x106.jpg 300w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-customize-post-excerpts-768x273.jpg 768w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-customize-post-excerpts-600x213.jpg.webp 600w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-customize-post-excerpts-750x266.jpg.webp 750w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-customize-post-excerpts-100x35.jpg 100w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-customize-post-excerpts.jpg.webp 1082w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/363;\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Then, let\u2019s change the length of the excerpt using the <a href=\"https:\/\/developer.wordpress.org\/reference\/hooks\/excerpt_length\/\" target=\"_blank\" rel=\"noopener\"><i>excerpt_length<\/i> filter<\/a>. Paste this code into your functions file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function new_excerpt_length($length) {\nreturn 20;\n}\nadd_filter('excerpt_length', 'new_excerpt_length', 999);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">By default, the standard length is 55 words. However, in this example, it\u2019s been set to 20. You can change the number to whatever you wish. (That <i>999<\/i> at the end is the filter priority \u2014 WordPress\u2019s documentation recommends a high number like this so the default filter doesn\u2019t run afterward and override your setting.)<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-8-generate-a-random-background-to-your-site\"><b>8. <\/b><b>Generate<\/b><b> a Random Background to Your Site<\/b><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, let\u2019s end with a fun design trick. This tweak lets you randomly generate a new background color for your site every time somebody visits it. Start by adding the following code to the functions file:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">function wpb_bg() {\n$rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');\n$color ='#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].\n$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];\necho $color;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This code generates a random hex color code, so all you need to do now is to make sure it gets applied to the page. To do that, you\u2019ll need to find the &lt;body&gt; tag, which should look like this:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;body &lt;?php body_class(); ?&gt;&gt;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is usually in the <i>header.php<\/i> file but can be elsewhere, depending on your theme. When you\u2019ve located the right line, simply replace it with the following code:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;body &lt;?php body_class(); ?&gt; style=\"background-color:&lt;?php wpb_bg();?&gt;\"&gt;&gt;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Save your file and open your website. You should see that it has a new background color:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"1024\" height=\"552\" data-src=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-generate-random-background-color-2-1024x552.jpg\" alt=\"create random background colors on reload by editing your WordPress functions.php file\" class=\"wp-image-37719 lazyload\" data-srcset=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-generate-random-background-color-2-1024x552.jpg 1024w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-generate-random-background-color-2-300x162.jpg 300w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-generate-random-background-color-2-768x414.jpg 768w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-generate-random-background-color-2-1536x828.jpg 1536w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-generate-random-background-color-2-600x324.jpg.webp 600w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-generate-random-background-color-2-750x404.jpg.webp 750w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-generate-random-background-color-2-100x54.jpg 100w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-generate-random-background-color-2.jpg.webp 1999w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/552;\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Reload the page, and you\u2019ll see a new color every time:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter\"><img decoding=\"async\" width=\"1024\" height=\"552\" data-src=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-generate-random-background-color-1024x552.jpg\" alt=\"create random background colors on reload by editing your WordPress functions.php file\" class=\"wp-image-37720 lazyload\" data-srcset=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-generate-random-background-color-1024x552.jpg 1024w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-generate-random-background-color-300x162.jpg 300w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-generate-random-background-color-768x414.jpg 768w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-generate-random-background-color-1536x828.jpg 1536w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-generate-random-background-color-600x324.jpg.webp 600w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-generate-random-background-color-750x404.jpg.webp 750w, https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/wordpress-functions-generate-random-background-color-100x54.jpg 100w, https:\/\/www.dreamhost.com\/blog\/wp-content\/smush-webp\/2022\/10\/wordpress-functions-generate-random-background-color.jpg.webp 1999w\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1024px; --smush-placeholder-aspect-ratio: 1024\/552;\" \/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">This is obviously not the right design choice for every site, but it\u2019s a neat trick for some!<\/p>\n\n\n\n<h2 id=\"h-faqs-about-the-wordpress-functions-php-file\" class=\"wp-block-heading\"><b>FAQs About the WordPress functions.php File<\/b><\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-faq-where-is-functions-php-in-wordpress\"><b>Where is functions.php in WordPress?<\/b><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Your theme\u2019s functions.php file is at <i>wp-content\/themes\/your-theme\/functions.php<\/i> \u2014 the root of your active theme\u2019s folder. A child theme has its own separate functions.php in its own folder. You can reach the file through <i>Appearance &gt; Theme File Editor<\/i> in the WordPress dashboard, or over SFTP.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-faq-functions-php-or-plugin\"><b>Should I add code to functions.php or a plugin?<\/b><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Put design-related code in the functions file and everything else in a plugin. The WordPress Theme Handbook\u2019s rule of thumb is that themes should only deal with the site\u2019s design \u2014 features that should keep working regardless of which theme is active belong in a plugin, because the functions file stops running the moment you switch themes.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-faq-theme-updates\"><b>Will my functions.php changes survive a theme update?<\/b><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No \u2014 updating a theme replaces its files, and any code you added to its functions.php goes with them. The fix is a <a href=\"https:\/\/www.dreamhost.com\/blog\/how-to-create-wordpress-child-theme\/\" target=\"_blank\" rel=\"noopener\">child theme<\/a>: WordPress loads the child theme\u2019s functions.php just before the parent theme\u2019s, so your custom code keeps running through every parent update.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-faq-broken-site\"><b>What if editing functions.php breaks my site?<\/b><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Restore your backed-up copy of the file over SFTP \u2014 the Theme File Editor doesn\u2019t make backup copies, so you can\u2019t undo a site-crashing change from inside WordPress. To avoid one possible source of breakage, don\u2019t leave a closing <i>?&gt;<\/i> tag at the end of the file: trailing whitespace after it can white-screen your site, and omitting the tag is valid PHP.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-faq-what-are-php-functions\"><b>What are functions in PHP?<\/b><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">A PHP function is a named, reusable block of code that runs when you call it. WordPress defines its own PHP functions \u2014 like the <i>register_nav_menu()<\/i> and <i>wp_nav_menu()<\/i> examples above \u2014 and your theme\u2019s functions file is where you can call them or define new ones of your own.<\/p>\n\n\n\n<h2 id=\"h-edit-your-functions-php-file\" class=\"wp-block-heading\"><b>Edit Your functions.php File<\/b><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The WordPress <i>functions.php<\/i> file is the perfect place to tinker with your site\u2019s default functionality. It\u2019s a powerful file that gives you a lot of control over your site once you understand how it works.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Depending on your WordPress theme, you might be able to use the built-in Theme File Editor to access and edit your functions.php file. Otherwise, you can access it via SFTP. Then, you can use custom code to do everything from displaying the estimated reading time of a post to customizing your excerpts.<\/p>\n\n\n\n\n\n<div\n\tclass=\"article-cta-shared article-cta-small\"\n>\n    <div class=\"tr-img-wrap-outer jsLoading\"><img decoding=\"async\" class=\"js-img-lazy \" src=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/themes\/blog2018\/assets\/img\/lazy-loading-transparent.webp\" data-srcset=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/themes\/blog2018\/assets\/img\/article-small-ad-cta-placeholder.webp 1x, https:\/\/www.dreamhost.com\/blog\/wp-content\/themes\/blog2018\/assets\/img\/article-small-ad-cta-placeholder-x2.webp 2x\"  alt=\"Ad background image\" \/><\/div>\n\n\n\n\n  <div class=\"content-btm\">\n    <h2\n      class=\"h2--md\"\n    >\n      Test Code Changes the Safe Way\n    <\/h2>\n    <p\n      class=\"p--md\"\n    >\n      Every DreamPress managed WordPress plan includes 1-Click Staging and daily backups, so you can try new functions.php code before it touches your live site.\n    <\/p>\n\n            <a\n            href=\"https:\/\/www.dreamhost.com\/wordpress\/managed\/\"\n                        class=\"btn btn--white-outline btn--sm btn--round\"\n                                    target=\"_blank\"\n            rel=\"noopener noreferrer\"\n            >\n                            Check Out Plans                    <\/a>\n\n  <\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>A WordPress theme can include one small file that changes how your whole site behaves. The WordPress functions.php file \u2014 also known as the functions file \u2014 is a PHP file in your theme\u2019s folder that works much like a plugin: WordPress loads it automatically along with your active theme, and any custom PHP code [&hellip;]<\/p>\n","protected":false},"author":1062,"featured_media":87046,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"_yoast_wpseo_opengraph-title":"WordPress functions.php File: Edit It + Snippets - DreamHost","_yoast_wpseo_opengraph-description":"WordPress functions.php file: learn what it does, 2 safe ways to edit it, and 8 practical code snippets you can copy today.","_yoast_wpseo_twitter-title":"WordPress functions.php File: Edit It + Snippets - DreamHost","_yoast_wpseo_twitter-description":"WordPress functions.php file: learn what it does, 2 safe ways to edit it, and 8 practical code snippets you can copy today.","toc_headlines":"[[\"h-what-is-the-functions-php-file\",\"What Is the functions.php File?\"],[\"h-how-to-edit-the-functions-file-2-methods\",\"How to Edit the Functions File (2 Methods)\"],[\"h-8-tricks-you-can-accomplish-with-the-wordpress-functions-file\",\"8 Tricks You Can Accomplish With the WordPress Functions File\"],[\"h-faqs-about-the-wordpress-functions-php-file\",\"FAQs About the WordPress functions.php File\"],[\"h-edit-your-functions-php-file\",\"Edit Your functions.php File\"]]","hide_toc":false,"show_updated_at":"1","footnotes":""},"categories":[13129,550,696],"tags":[],"class_list":["post-37708","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developer-central","category-tutorials","category-wordpress"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>WordPress functions.php File: Edit It + Snippets - DreamHost<\/title>\n<meta name=\"description\" content=\"WordPress functions.php file: learn what it does, 2 safe ways to edit it, and 8 practical code snippets you can copy today.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dreamhost.com\/blog\/guide-to-wp-functions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"WordPress functions.php File: Edit It + Snippets - DreamHost\" \/>\n<meta property=\"og:description\" content=\"WordPress functions.php file: learn what it does, 2 safe ways to edit it, and 8 practical code snippets you can copy today.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dreamhost.com\/blog\/guide-to-wp-functions\/\" \/>\n<meta property=\"og:site_name\" content=\"DreamHost Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DreamHost\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-25T14:00:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-08-22T21:53:03+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/WordPress-Functions.php-Social.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Jason Cosper\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:title\" content=\"WordPress functions.php File: Edit It + Snippets - DreamHost\" \/>\n<meta name=\"twitter:description\" content=\"WordPress functions.php file: learn what it does, 2 safe ways to edit it, and 8 practical code snippets you can copy today.\" \/>\n<meta name=\"twitter:creator\" content=\"@dreamhost\" \/>\n<meta name=\"twitter:site\" content=\"@dreamhost\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Jason Cosper\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"WordPress functions.php File: Edit It + Snippets - DreamHost","description":"WordPress functions.php file: learn what it does, 2 safe ways to edit it, and 8 practical code snippets you can copy today.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dreamhost.com\/blog\/guide-to-wp-functions\/","og_locale":"en_US","og_type":"article","og_title":"WordPress functions.php File: Edit It + Snippets - DreamHost","og_description":"WordPress functions.php file: learn what it does, 2 safe ways to edit it, and 8 practical code snippets you can copy today.","og_url":"https:\/\/www.dreamhost.com\/blog\/guide-to-wp-functions\/","og_site_name":"DreamHost Blog","article_publisher":"https:\/\/www.facebook.com\/DreamHost\/","article_published_time":"2022-10-25T14:00:06+00:00","article_modified_time":"2026-08-22T21:53:03+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/10\/WordPress-Functions.php-Social.jpg","type":"image\/jpeg"}],"author":"Jason Cosper","twitter_card":"summary_large_image","twitter_title":"WordPress functions.php File: Edit It + Snippets - DreamHost","twitter_description":"WordPress functions.php file: learn what it does, 2 safe ways to edit it, and 8 practical code snippets you can copy today.","twitter_creator":"@dreamhost","twitter_site":"@dreamhost","twitter_misc":{"Written by":"Jason Cosper","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dreamhost.com\/blog\/guide-to-wp-functions\/#article","isPartOf":{"@id":"https:\/\/www.dreamhost.com\/blog\/guide-to-wp-functions\/"},"author":{"name":"Jason Cosper","@id":"https:\/\/www.dreamhost.com\/blog\/#\/schema\/person\/696d1ca9f2e23c846e02e04944e8fca4"},"headline":"Everything You Need to Know About the WordPress functions.php File","datePublished":"2022-10-25T14:00:06+00:00","dateModified":"2026-08-22T21:53:03+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dreamhost.com\/blog\/guide-to-wp-functions\/"},"wordCount":2671,"publisher":{"@id":"https:\/\/www.dreamhost.com\/blog\/#organization"},"image":{"@id":"https:\/\/www.dreamhost.com\/blog\/guide-to-wp-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2026\/08\/blog-hero-guide-to-wp-functions-dreamops-3be5087d74063392230f74fc.webp","articleSection":["Developer Central","Tutorials","WordPress"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.dreamhost.com\/blog\/guide-to-wp-functions\/","url":"https:\/\/www.dreamhost.com\/blog\/guide-to-wp-functions\/","name":"WordPress functions.php File: Edit It + Snippets - DreamHost","isPartOf":{"@id":"https:\/\/www.dreamhost.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.dreamhost.com\/blog\/guide-to-wp-functions\/#primaryimage"},"image":{"@id":"https:\/\/www.dreamhost.com\/blog\/guide-to-wp-functions\/#primaryimage"},"thumbnailUrl":"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2026\/08\/blog-hero-guide-to-wp-functions-dreamops-3be5087d74063392230f74fc.webp","datePublished":"2022-10-25T14:00:06+00:00","dateModified":"2026-08-22T21:53:03+00:00","description":"WordPress functions.php file: learn what it does, 2 safe ways to edit it, and 8 practical code snippets you can copy today.","breadcrumb":{"@id":"https:\/\/www.dreamhost.com\/blog\/guide-to-wp-functions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dreamhost.com\/blog\/guide-to-wp-functions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.dreamhost.com\/blog\/guide-to-wp-functions\/#primaryimage","url":"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2026\/08\/blog-hero-guide-to-wp-functions-dreamops-3be5087d74063392230f74fc.webp","contentUrl":"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2026\/08\/blog-hero-guide-to-wp-functions-dreamops-3be5087d74063392230f74fc.webp","width":1536,"height":1024},{"@type":"BreadcrumbList","@id":"https:\/\/www.dreamhost.com\/blog\/guide-to-wp-functions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.dreamhost.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Everything You Need to Know About the WordPress functions.php File"}]},{"@type":"WebSite","@id":"https:\/\/www.dreamhost.com\/blog\/#website","url":"https:\/\/www.dreamhost.com\/blog\/","name":"DreamHost Blog","description":"","publisher":{"@id":"https:\/\/www.dreamhost.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dreamhost.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.dreamhost.com\/blog\/#organization","name":"DreamHost","url":"https:\/\/www.dreamhost.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.dreamhost.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/dhblog.dream.press\/blog\/wp-content\/uploads\/2019\/01\/dh_logo-blue-2.png","contentUrl":"https:\/\/dhblog.dream.press\/blog\/wp-content\/uploads\/2019\/01\/dh_logo-blue-2.png","width":1200,"height":168,"caption":"DreamHost"},"image":{"@id":"https:\/\/www.dreamhost.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DreamHost\/","https:\/\/x.com\/dreamhost","https:\/\/www.instagram.com\/dreamhost\/","https:\/\/www.linkedin.com\/company\/dreamhost\/","https:\/\/www.youtube.com\/user\/dreamhostusa"]},{"@type":"Person","@id":"https:\/\/www.dreamhost.com\/blog\/#\/schema\/person\/696d1ca9f2e23c846e02e04944e8fca4","name":"Jason Cosper","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/02\/IMG_4807-scaled.jpeg","url":"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/02\/IMG_4807-scaled.jpeg","contentUrl":"https:\/\/www.dreamhost.com\/blog\/wp-content\/uploads\/2022\/02\/IMG_4807-scaled.jpeg","caption":"Jason Cosper"},"description":"Jason is DreamHost\u2019s WordPress Product Advocate, based out of Bakersfield, CA. He is currently working on making our DreamPress product even better. In his free time, he likes to curl up on the couch and watch scary movies with his wife Sarah and three very small dogs. Follow him on Twitter.","url":"https:\/\/www.dreamhost.com\/blog\/author\/jasoncosper\/"}]}},"lang":"en","translations":{"en":37708,"es":37728,"de":52356,"pl":55749,"pt":55782,"ru":55804,"uk":55807,"it":68777,"fr":71157,"nl":71182},"pll_sync_post":{},"_links":{"self":[{"href":"https:\/\/www.dreamhost.com\/blog\/wp-json\/wp\/v2\/posts\/37708","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dreamhost.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dreamhost.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dreamhost.com\/blog\/wp-json\/wp\/v2\/users\/1062"}],"version-history":[{"count":1,"href":"https:\/\/www.dreamhost.com\/blog\/wp-json\/wp\/v2\/posts\/37708\/revisions"}],"predecessor-version":[{"id":87072,"href":"https:\/\/www.dreamhost.com\/blog\/wp-json\/wp\/v2\/posts\/37708\/revisions\/87072"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.dreamhost.com\/blog\/wp-json\/wp\/v2\/media\/87046"}],"wp:attachment":[{"href":"https:\/\/www.dreamhost.com\/blog\/wp-json\/wp\/v2\/media?parent=37708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dreamhost.com\/blog\/wp-json\/wp\/v2\/categories?post=37708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dreamhost.com\/blog\/wp-json\/wp\/v2\/tags?post=37708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}