{"id":1113,"date":"2018-08-08T10:31:50","date_gmt":"2018-08-08T05:01:50","guid":{"rendered":"https:\/\/www.coimbatorewebhosting.com\/blog\/?p=1113"},"modified":"2019-03-11T11:57:01","modified_gmt":"2019-03-11T06:27:01","slug":"safely-add-code-php-in-wordpress","status":"publish","type":"post","link":"https:\/\/coimbatorewebhosting.com\/blog\/safely-add-code-php-in-wordpress\/","title":{"rendered":"How to (Safely) Add Code to functions.php in WordPress"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/coimbatorewebhosting.com\/blog\/safely-add-code-php-in-wordpress\/#Introduction\" >Introduction:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/coimbatorewebhosting.com\/blog\/safely-add-code-php-in-wordpress\/#What_is_functionsphp\" >What is functions.php<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/coimbatorewebhosting.com\/blog\/safely-add-code-php-in-wordpress\/#Problems_Risk_in_Functionsphp\" >Problems \/Risk in Functions.php:<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/coimbatorewebhosting.com\/blog\/safely-add-code-php-in-wordpress\/#Step_1_Locate_the_functionsphp_in_your_theme\" >Step:1 Locate the functions.php in your theme.<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/coimbatorewebhosting.com\/blog\/safely-add-code-php-in-wordpress\/#Step_2_Making_Sure_Theres_No_Closing_%3E_Tag\" >Step 2: Making Sure There\u2019s No Closing ?&gt; Tag<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/coimbatorewebhosting.com\/blog\/safely-add-code-php-in-wordpress\/#Step_3_Formatting_your_Code\" >Step 3: Formatting your Code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/coimbatorewebhosting.com\/blog\/safely-add-code-php-in-wordpress\/#Step_4_Pasting_the_Code_into_functionsphp\" >Step 4: Pasting the Code into functions.php<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/coimbatorewebhosting.com\/blog\/safely-add-code-php-in-wordpress\/#Access_from_cPanel_File_Manager\" >Access from cPanel File Manager<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/coimbatorewebhosting.com\/blog\/safely-add-code-php-in-wordpress\/#Use_a_Custom_Plugin_for_WordPress_Code\" >Use a Custom Plugin for WordPress Code<\/a><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Introduction\"><\/span>Introduction:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Surfing the Internet for solutions to WordPress problems will inevitably bring you to sites that ask you to insert some piece of code or the other into functions.php. If you\u2019re new to WordPress, this can be a confusing thing.<\/p>\n<p>By default, WordPress doesn\u2019t have any obvious interface for you to insert code. And besides\u2026copy pasting stuff into your installation is dangerous.<\/p>\n<p>Here you will come to know how to do it.<\/p>\n<h3 class=\"western\"><span class=\"ez-toc-section\" id=\"What_is_functionsphp\"><\/span>What is functions.php<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>functions.php or the theme functions file is a template used by WordPress themes. It acts like a plugin and gets automatically loaded in both admin and front-end pages of a WordPress site. Usually this file is used to define functions, classes, actions and filters to be used by other templates in the theme. It can be used to add features and extend the functionality of both the theme, and the WordPress installation.<\/p>\n<p>The functions.php file can be found in your theme\u2019s folder. You can add both built in WordPress functions and regular PHP functions to hooks and filters that are predefined throughout the WordPress core.<\/p>\n<p>Although every theme you have installed on your site has its own functions.php file, only the active theme\u2019s file will run its code. If your theme doesn\u2019t have a functions.php file you can simply create a plain-text file named functions.php and add it to your theme\u2019s directory.<\/p>\n<p><a href=\"https:\/\/themeforest.net\" rel=\"nofollow noopener\" target=\"_blank\">Child themes<\/a> can have their own functions.php files which can be used to either build on top of or entirely replace the one in the parent theme directory. Some of the things you can do with a functions.php file include use WordPress actions and filters, you can enable post thumbnails, post formats, and navigation menus.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Problems_Risk_in_Functionsphp\"><\/span>Problems \/Risk in Functions.php:<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Despite its value, functions.php is a dangerous file. The reason is that it\u2019s linked to your theme. If there\u2019s a syntax error in the code, it could disable the theme itself and make your site inaccessible. So be careful! We\u2019ll also show you how to avoid pitfalls when inserting code.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Step_1_Locate_the_functionsphp_in_your_theme\"><\/span>Step:1 Locate the functions.php in your theme.<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Log into your WordPress dashboard and go to \u201cAppearance-&gt;Editor\u201d as shown here:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1114 size-full\" src=\"https:\/\/www.coimbatorewebhosting.com\/blog\/wp-content\/uploads\/2018\/08\/wp-functions.png\" alt=\"wordpress\" width=\"394\" height=\"433\" srcset=\"https:\/\/coimbatorewebhosting.com\/blog\/wp-content\/uploads\/2018\/08\/wp-functions.png 394w, https:\/\/coimbatorewebhosting.com\/blog\/wp-content\/uploads\/2018\/08\/wp-functions-273x300.png 273w\" sizes=\"auto, (max-width: 394px) 100vw, 394px\" \/><\/p>\n<p>This will bring you to a page where you can edit your theme files. These files are listed on the right-hand side. Your current theme is selected by default. Locate the file labeled \u201cTheme Functions\u201d and click it as shown here:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1117 size-full\" src=\"https:\/\/www.coimbatorewebhosting.com\/blog\/wp-content\/uploads\/2018\/08\/wp-functions1.png\" alt=\"wordpress\" width=\"357\" height=\"509\" srcset=\"https:\/\/coimbatorewebhosting.com\/blog\/wp-content\/uploads\/2018\/08\/wp-functions1.png 357w, https:\/\/coimbatorewebhosting.com\/blog\/wp-content\/uploads\/2018\/08\/wp-functions1-210x300.png 210w\" sizes=\"auto, (max-width: 357px) 100vw, 357px\" \/><\/p>\n<h3 class=\"western\"><span class=\"ez-toc-section\" id=\"Step_2_Making_Sure_Theres_No_Closing_%3E_Tag\"><\/span>Step 2: Making Sure There\u2019s No Closing ?&gt; Tag<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>This will open up functions.php in the textbox on the left-hand side. Scroll all the way down. If you see the following symbol at the&nbsp;<i>very end<\/i>&nbsp;of the file, delete it:<\/p>\n<pre>?&gt;<\/pre>\n<p>The old way of doing things was to ensure that every php file ended with a ?&gt; tag. But this led to problems where people would accidentally leave a blank line or even a space afterward, and it would crash their site. It was also very hard to debug.<\/p>\n<pre>So before you modify functions.php, ensure that it doesn't end with ?&gt; . If it doesn't remove it.<\/pre>\n<h3 class=\"western\"><span class=\"ez-toc-section\" id=\"Step_3_Formatting_your_Code\"><\/span>Step 3: Formatting your Code<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Older sites, or those adhering to older WordPress PHP standards might make a few mistakes in their code. So copy the code from their site, paste it into a text editor like Notepad and make the following changes:<\/p>\n<pre><span style=\"font-family: Consolas, Monaco, monospace;\">Remove any opening &lt;?php code<\/span>\r\n<span style=\"font-family: Consolas, Monaco, monospace;\">Remove any trailing ?&gt; code<\/span><\/pre>\n<p>Basically ensure that the code you\u2019re copying is not enclosed in &lt;?php and ?&gt; tags. If it is, you\u2019re going to have problems and your site will crash because we removed the closing ?&gt; tag in Step 2.<\/p>\n<p>Once your code is clean, we\u2019re ready to paste!<\/p>\n<h3 class=\"western\"><span class=\"ez-toc-section\" id=\"Step_4_Pasting_the_Code_into_functionsphp\"><\/span>Step 4: Pasting the Code into functions.php<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Scroll all the way down to the textbox Step 1 with \u201cfunctions.php\u201d selected on the right-hand side. Now paste your code at the very bottom as shown here:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1116\" src=\"https:\/\/www.coimbatorewebhosting.com\/blog\/wp-content\/uploads\/2018\/08\/wpfunctions2.png\" alt=\"\" width=\"496\" height=\"233\" srcset=\"https:\/\/coimbatorewebhosting.com\/blog\/wp-content\/uploads\/2018\/08\/wpfunctions2.png 496w, https:\/\/coimbatorewebhosting.com\/blog\/wp-content\/uploads\/2018\/08\/wpfunctions2-300x141.png 300w\" sizes=\"auto, (max-width: 496px) 100vw, 496px\" \/><\/p>\n<p>To reiterate (because it\u2019s so important), make sure there is no closing ?&gt; tag! Now save your changes and you\u2019ve successfully added code to functions.php in WordPress! Here\u2019s the output of the code we just added in this step as an example:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-1120\" src=\"https:\/\/www.coimbatorewebhosting.com\/blog\/wp-content\/uploads\/2018\/08\/wpfunctions3.png\" alt=\"\" width=\"528\" height=\"149\" srcset=\"https:\/\/coimbatorewebhosting.com\/blog\/wp-content\/uploads\/2018\/08\/wpfunctions3.png 528w, https:\/\/coimbatorewebhosting.com\/blog\/wp-content\/uploads\/2018\/08\/wpfunctions3-300x85.png 300w\" sizes=\"auto, (max-width: 528px) 100vw, 528px\" \/><\/p>\n<h3 class=\"western\"><span class=\"ez-toc-section\" id=\"Access_from_cPanel_File_Manager\"><\/span>Access from cPanel File Manager<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Even though We\u2019ve shown you how to access and modify functions.php from inside WordPress, we suggest you do it from your cPanel\u2019s file manager instead. Locate your WordPress installation, and go to the following location:<\/p>\n<pre><i>[WordPress Folder]<\/i>-&gt;wp-content-&gt;themes-&gt;<i>[theme folder]<\/i>-&gt;functions.php<\/pre>\n<p>You can now right-click and edit functions.php from here. The reason why we recommend doing this is so that you can revert the changes if your site crashes!<\/p>\n<h3 class=\"western\"><span class=\"ez-toc-section\" id=\"Use_a_Custom_Plugin_for_WordPress_Code\"><\/span>Use a Custom Plugin for WordPress Code<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>This requires a separate tutorial by itself. But the idea is that you create your own separate plugin for WordPress specific code instead of inserting it into functions.php. This way if something goes wrong, WordPress can just disable the plugin instead of crashing your site!<\/p>\n<p>We hope this was a useful tutorial for you on how to safely insert code into functions.php in WordPress!<\/p>\n<p>You can know about increase the php memory upload size in word press <a href=\"https:\/\/www.coimbatorewebhosting.com\/blog\/how-to-increase-the-php-memory-upload-size-in-word-press\/\">click here.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: Surfing the Internet for solutions to WordPress problems will inevitably bring you to sites that ask you to insert some piece of code or the other into functions.php. If you\u2019re new to WordPress, this can be a confusing thing. By default, WordPress doesn\u2019t have any obvious interface for you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1123,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[1],"tags":[104,100,103,101,102],"class_list":["post-1113","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-latest","tag-formatting-your-code","tag-functions-php","tag-inserting-code","tag-template","tag-theme-directory"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/coimbatorewebhosting.com\/blog\/wp-content\/uploads\/2018\/08\/the_matrix_bg_2.jpg","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pa2YU7-hX","_links":{"self":[{"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/comments?post=1113"}],"version-history":[{"count":0,"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/posts\/1113\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/media\/1123"}],"wp:attachment":[{"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/media?parent=1113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/categories?post=1113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coimbatorewebhosting.com\/blog\/wp-json\/wp\/v2\/tags?post=1113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}