{"id":676,"date":"2019-11-20T04:44:52","date_gmt":"2019-11-20T04:44:52","guid":{"rendered":"http:\/\/blog.hostinger.io\/hostinger-tutorials\/uncategorized\/wordpresshow-to-add-php-code-to-wordpress-post-or-page\/"},"modified":"2026-03-09T19:19:23","modified_gmt":"2026-03-09T19:19:23","slug":"how-to-add-php-code-to-wordpress","status":"publish","type":"post","link":"\/ng\/tutorials\/how-to-add-php-code-to-wordpress","title":{"rendered":"Adding PHP to WordPress: 2 methods to add PHP code snippets"},"content":{"rendered":"<?xml encoding=\"utf-8\" ?><p>Adding PHP to WordPress lets you <strong>add custom functionality, automation, and specific logic directly to your website&rsquo;s architecture<\/strong>.<\/p><p>By adding these PHP snippets, you can customize theme behavior, extend plugin capabilities, and enhance security features beyond the default settings.<\/p><p>WordPress doesn&rsquo;t allow direct PHP input in the block or classic editor because it would expose your site to code injection attacks &ndash; a vulnerability that could give attackers full server access.<\/p><p>To work around this safely, you can use two methods to add PHP to your WordPress site:<\/p><ol class=\"wp-block-list\">\n<li><strong>Use a plugin<\/strong>. This option works best for beginners and non-technical users because it manages code execution safely without risking site errors.<\/li>\n\n\n\n<li><strong>Add PHP manually<\/strong>. This method suits users who want full control of their file structure or need to implement complex logic in a child theme.<\/li>\n<\/ol><p>\n\n\n\n<\/p><h2 class=\"wp-block-heading\" id=\"h-how-to-add-php-code-using-a-plugin\">How to add PHP code using a plugin<\/h2><p>WordPress prevents users from adding custom PHP code directly in the built-in editor due to security concerns. To safely add PHP code to a WordPress post or page, you can convert it into a shortcode first.<\/p><p>You can <a href=\"\/ng\/tutorials\/create-a-shortcode-in-wordpress\">create a shortcode in WordPress<\/a> manually, but a plugin streamlines the process. Several options are available in the WordPress repository. For this tutorial, we&rsquo;ll use one of the most popular choices: <strong>WPCode<\/strong>.<\/p><p>WPCode includes built-in error checking. If you make a syntax error or paste malformed code, it automatically deactivates the snippet and displays an error message explaining what went wrong.<\/p><p>Here&rsquo;s how to add a PHP code snippet using the WPCode plugin:<\/p><ol class=\"wp-block-list\">\n<li><a href=\"\/ng\/tutorials\/how-to-install-wordpress-plugins\">Install and activate the plugin<\/a>. The <strong>Code Snippets<\/strong> menu appears in your WordPress dashboard after activation.<\/li>\n\n\n\n<li>Go to <strong>Code Snippets &rarr; + Add Snippet<\/strong>. You&rsquo;ll see a library of pre-made snippets. To add custom PHP code, hover over <strong>Add Your Custom Code (New Snippet)<\/strong> and click <strong>+ Add Custom Snippet<\/strong>.<\/li>\n<\/ol><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69e1fb3b2fac2\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"2556\" height=\"1020\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-add-snippet-add-custom-snippet-highlighted.png\/public\" alt=\"WordPress WPCode screen with Add Custom Snippet option highlighted\" class=\"wp-image-137261\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-add-snippet-add-custom-snippet-highlighted.png\/w=2556,fit=scale-down 2556w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-add-snippet-add-custom-snippet-highlighted.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-add-snippet-add-custom-snippet-highlighted.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-add-snippet-add-custom-snippet-highlighted.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-add-snippet-add-custom-snippet-highlighted.png\/w=768,fit=scale-down 768w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-add-snippet-add-custom-snippet-highlighted.png\/w=1536,fit=scale-down 1536w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-add-snippet-add-custom-snippet-highlighted.png\/w=2048,fit=scale-down 2048w\" sizes=\"auto, (max-width: 2556px) 100vw, 2556px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><ol start=\"3\" class=\"wp-block-list\">\n<li>Name your snippet and set the <strong>Code Type<\/strong> to <strong>PHP Snippet<\/strong>. Write your code in the Code Preview box. The example below shows a simple timezone snippet. You don&rsquo;t need to include the opening <strong>&lt;?php<\/strong> tag, as WPCode adds it automatically.<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Display Los Angeles timezone\n   $timezone = new DateTimeZone('America\/Los_Angeles');\n   $datetime = new DateTime('now', $timezone);\n   echo '&lt;p&gt;Current time in Los Angeles: ' . $datetime-&gt;format('g:i A') . '&lt;\/p&gt;';<\/pre><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69e1fb3b31393\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"2556\" height=\"1088\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-create-custom-snippet.png\/public\" alt=\"WPCode editor showing custom PHP snippet fields and configuration options\" class=\"wp-image-137262\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-create-custom-snippet.png\/w=2556,fit=scale-down 2556w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-create-custom-snippet.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-create-custom-snippet.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-create-custom-snippet.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-create-custom-snippet.png\/w=768,fit=scale-down 768w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-create-custom-snippet.png\/w=1536,fit=scale-down 1536w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-create-custom-snippet.png\/w=2048,fit=scale-down 2048w\" sizes=\"auto, (max-width: 2556px) 100vw, 2556px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><ol start=\"4\" class=\"wp-block-list\">\n<li>Configure the Insertion settings. Choose <strong>Auto Insert<\/strong> and set the location to <strong>Site Wide Footer<\/strong> to display the timezone in the footer of all pages.<\/li>\n<\/ol><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69e1fb3b32b59\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"2540\" height=\"1346\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-insertion-auto-insert-site-wide-footer-selected.png\/public\" alt=\"WPCode Insertion settings with Auto Insert and Site Wide Footer selected\" class=\"wp-image-137263\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-insertion-auto-insert-site-wide-footer-selected.png\/w=2540,fit=scale-down 2540w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-insertion-auto-insert-site-wide-footer-selected.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-insertion-auto-insert-site-wide-footer-selected.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-insertion-auto-insert-site-wide-footer-selected.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-insertion-auto-insert-site-wide-footer-selected.png\/w=768,fit=scale-down 768w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-insertion-auto-insert-site-wide-footer-selected.png\/w=1536,fit=scale-down 1536w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-insertion-auto-insert-site-wide-footer-selected.png\/w=2048,fit=scale-down 2048w\" sizes=\"auto, (max-width: 2540px) 100vw, 2540px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><ol start=\"5\" class=\"wp-block-list\">\n<li>Use the <strong>Shortcode<\/strong> option if you prefer to embed the snippet in posts or pages. After saving it, Click <strong>Copy<\/strong> and paste the shortcode where you want it to appear.<\/li>\n\n\n\n<li>Set conditional logic if you&rsquo;re using <strong>Auto Insert<\/strong>. The free version supports user and page conditions, including user role, page type, and specific URLs. Select <strong>Add new group<\/strong> and configure your rules.<\/li>\n<\/ol><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69e1fb3b344bb\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1598\" height=\"1398\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-smart-conditional-logic-logged-in-selected.png\/public\" alt=\"WPCode conditional logic settings with logged-in user rule selected\" class=\"wp-image-137264\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-smart-conditional-logic-logged-in-selected.png\/w=1598,fit=scale-down 1598w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-smart-conditional-logic-logged-in-selected.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-smart-conditional-logic-logged-in-selected.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-smart-conditional-logic-logged-in-selected.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-smart-conditional-logic-logged-in-selected.png\/w=768,fit=scale-down 768w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-wpcode-smart-conditional-logic-logged-in-selected.png\/w=1536,fit=scale-down 1536w\" sizes=\"auto, (max-width: 1598px) 100vw, 1598px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><ol start=\"7\" class=\"wp-block-list\">\n<li>Activate and save your snippet. Turn on the toggle in the upper menu bar and click <strong>Save snippet<\/strong>.<\/li>\n<\/ol><p>If WPCode doesn&rsquo;t meet your needs, consider these alternatives:<\/p><ul class=\"wp-block-list\">\n<li><strong>Code Snippets<\/strong>. Includes an editor with error checking and lets you group related snippets with tags.<\/li>\n\n\n\n<li><strong>Head and Footer Code<\/strong>. Sets the priority of custom code for the header, body, and footer.<\/li>\n\n\n\n<li><strong>Post Snippets<\/strong>. Lets you reorder snippets using drag-and-drop.<\/li>\n<\/ul><h2 class=\"wp-block-heading\" id=\"h-how-to-add-php-to-wordpress-manually\">How to add PHP to WordPress manually<\/h2><p>The plugin method makes adding custom PHP code easy, but it may be limiting for advanced customization. For more flexibility, you can add PHP manually.<\/p><p><a href=\"\/ng\/tutorials\/backup-wordpress\">Back up your WordPress website<\/a> before changing any PHP files to protect your data in case something goes wrong. It&rsquo;s also best to test changes in a <a href=\"\/ng\/tutorials\/wordpress-staging-environment\">staging environment<\/a> instead of a live site unless it&rsquo;s absolutely necessary.<\/p><p>There are two main ways to add PHP code to your WordPress site manually: using the <a href=\"\/ng\/tutorials\/wordpress-theme-editor\">WordPress theme editor<\/a> or using a file manager or FTP client with a code editor.<\/p><p>The theme editor works well for quick edits because it&rsquo;s accessible from your WordPress dashboard. But it lacks features like error checking and autocompletion that a proper code editor provides.<\/p><p>If you want to modify theme files, create a <a href=\"\/ng\/tutorials\/how-to-create-wordpress-child-theme\">WordPress child theme<\/a> and add your PHP snippets there. A child theme keeps your customizations separate and prevents theme updates from overwriting them.<\/p><p>If you&rsquo;re using a modern block theme like <strong>Twenty Twenty-Four<\/strong>, some customizations that previously required <strong>functions.php<\/strong> can now be handled in the <strong>theme.json<\/strong> file. Check your theme&rsquo;s documentation for the recommended approach.<\/p><p>Here&rsquo;s how to add custom PHP in WordPress using the theme editor:<\/p><ol class=\"wp-block-list\">\n<li>Head to <strong>Appearance &rarr; Theme File Editor<\/strong> in your WordPress dashboard.<\/li>\n\n\n\n<li>Select <strong>Theme Functions<\/strong> on the right sidebar to open the <a href=\"\/ng\/tutorials\/functions-php-wordpress\">functions.php<\/a> file.<\/li>\n\n\n\n<li>Add the code for your desired functionality. Insert your custom PHP at the end of the file, just before the closing <strong>?&gt;<\/strong> tag if it exists. Here&rsquo;s an example of modifying the WordPress excerpt length:<\/li>\n<\/ol><pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ Custom function to modify excerpt length\n   function custom_excerpt_length( $length ) {\n       return 30; \/\/ Number of words\n   }\n   add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );<\/pre><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69e1fb3b35d6f\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"2560\" height=\"1503\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-edit-themes-theme-functions-custom-function-highlighted-scaled.png\/public\" alt=\"WordPress Theme File Editor with functions.php and custom function highlighted\" class=\"wp-image-137265\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-edit-themes-theme-functions-custom-function-highlighted-scaled.png\/w=2560,fit=scale-down 2560w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-edit-themes-theme-functions-custom-function-highlighted-scaled.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-edit-themes-theme-functions-custom-function-highlighted-scaled.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-edit-themes-theme-functions-custom-function-highlighted-scaled.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-edit-themes-theme-functions-custom-function-highlighted-scaled.png\/w=768,fit=scale-down 768w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-edit-themes-theme-functions-custom-function-highlighted-scaled.png\/w=1536,fit=scale-down 1536w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/wordpress-admin-edit-themes-theme-functions-custom-function-highlighted-scaled.png\/w=2048,fit=scale-down 2048w\" sizes=\"auto, (max-width: 2560px) 100vw, 2560px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><ol start=\"4\" class=\"wp-block-list\">\n<li>Once done, click <strong>Update File<\/strong>.<\/li>\n<\/ol><p>To modify plugin files, go to <strong>Plugins &rarr; Plugin File Editor <\/strong>and follow the same steps. If you introduce a syntax error, WordPress deactivates the plugin and shows an error message so you can fix it.<\/p><p>On the other hand, editing files using your web host&rsquo;s File Manager or an <a href=\"\/ng\/tutorials\/filezilla-ftp-configuration\">FTP client like FileZilla<\/a> is better for users with some technical experience. This method also lets you access and modify files beyond themes and plugins.<\/p><p>Here&rsquo;s how to add a PHP snippet using <a href=\"\/ng\/support\/4548688-basic-actions-in-the-file-manager-in-hostinger\">Hostinger&rsquo;s File Manager<\/a>:<\/p><ol class=\"wp-block-list\">\n<li>Select <strong>File Manager<\/strong> from your hPanel dashboard.<\/li>\n<\/ol><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69e1fb3b379b9\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"2182\" height=\"1166\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/hpanel-websites-dashboard-file-manager-highlighted.png\/public\" alt=\"hPanel dashboard showing Websites page with File Manager option highlighted\" class=\"wp-image-137266\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/hpanel-websites-dashboard-file-manager-highlighted.png\/w=2182,fit=scale-down 2182w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/hpanel-websites-dashboard-file-manager-highlighted.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/hpanel-websites-dashboard-file-manager-highlighted.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/hpanel-websites-dashboard-file-manager-highlighted.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/hpanel-websites-dashboard-file-manager-highlighted.png\/w=768,fit=scale-down 768w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/hpanel-websites-dashboard-file-manager-highlighted.png\/w=1536,fit=scale-down 1536w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/hpanel-websites-dashboard-file-manager-highlighted.png\/w=2048,fit=scale-down 2048w\" sizes=\"auto, (max-width: 2182px) 100vw, 2182px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><ol class=\"wp-block-list\">\n<li>Open the <strong>public_html<\/strong> folder to view your site files.<\/li>\n\n\n\n<li>Open <strong>wp-content <\/strong>to access your theme or plugin folders. For example, go to <strong>themes &rarr; your active theme folder<\/strong> and add functions to the <strong>functions.php<\/strong> file. Double-click the file to open it, and click <strong>Save<\/strong> when you&rsquo;re done.<\/li>\n<\/ol><div class=\"wp-block-image\"><figure data-wp-context='{\"imageId\":\"69e1fb3b39336\"}' data-wp-interactive=\"core\/image\" class=\"aligncenter size-large wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"2560\" height=\"1442\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-on-async--load=\"callbacks.setButtonStyles\" data-wp-on-async-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/hpanel-file-manager-functions-php-selected-scaled.png\/public\" alt=\"Hostinger File Manager with functions.php selected inside theme folder\" class=\"wp-image-137267\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/hpanel-file-manager-functions-php-selected-scaled.png\/w=2560,fit=scale-down 2560w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/hpanel-file-manager-functions-php-selected-scaled.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/hpanel-file-manager-functions-php-selected-scaled.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/hpanel-file-manager-functions-php-selected-scaled.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/hpanel-file-manager-functions-php-selected-scaled.png\/w=768,fit=scale-down 768w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/hpanel-file-manager-functions-php-selected-scaled.png\/w=1536,fit=scale-down 1536w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2019\/11\/hpanel-file-manager-functions-php-selected-scaled.png\/w=2048,fit=scale-down 2048w\" sizes=\"auto, (max-width: 2560px) 100vw, 2560px\" \/><button class=\"lightbox-trigger\" type=\"button\" aria-haspopup=\"dialog\" aria-label=\"Enlarge\" data-wp-init=\"callbacks.initTriggerButton\" data-wp-on-async--click=\"actions.showLightbox\" data-wp-style--right=\"state.imageButtonRight\" data-wp-style--top=\"state.imageButtonTop\">\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewbox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\"><\/path>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure><\/div><?xml encoding=\"utf-8\" ?><div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><a class=\"hgr-tutorials-cta hgr-tutorials-cta-coupon-wordpress-hosting\" href=\"\/ng\/cart?product=hosting:hostinger_business&amp;period=12&amp;coupon_cookie=WPTUTORIALS\"><img loading=\"lazy\" decoding=\"async\" width=\"2048\" height=\"600\" src=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/05\/EN_WP_in-text-banner.png\/public\" alt=\"\" class=\"wp-image-128217\" srcset=\"https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/05\/EN_WP_in-text-banner.png\/w=2048,fit=scale-down 2048w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/05\/EN_WP_in-text-banner.png\/w=300,fit=scale-down 300w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/05\/EN_WP_in-text-banner.png\/w=1024,fit=scale-down 1024w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/05\/EN_WP_in-text-banner.png\/w=150,fit=scale-down 150w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/05\/EN_WP_in-text-banner.png\/w=768,fit=scale-down 768w, https:\/\/imagedelivery.net\/LqiWLm-3MGbYHtFuUbcBtA\/wp-content\/uploads\/sites\/2\/2025\/05\/EN_WP_in-text-banner.png\/w=1536,fit=scale-down 1536w\" sizes=\"auto, (max-width: 2048px) 100vw, 2048px\" \/><\/a><\/figure><\/div><h2 class=\"wp-block-heading\" id=\"h-why-add-php-to-wordpress\">Why add PHP to WordPress<\/h2><p>For websites running on managed WordPress hosting, inserting custom PHP code offers the following benefits:<\/p><ul class=\"wp-block-list\">\n<li><strong>Enhanced functionality<\/strong>. Add unique features that might not be achievable with standard plugins or themes.<\/li>\n\n\n\n<li><strong>Tailored solutions<\/strong>. Create customized solutions for your specific requirements and growing user demands.<\/li>\n\n\n\n<li><strong>Unique designs<\/strong>. Implement special design elements and layouts that set your website apart from others using off-the-shelf themes.<\/li>\n\n\n\n<li><strong>Integration<\/strong>. Integrate external services, APIs, or databases seamlessly into your WordPress site, enabling complex interactions and data handling.<\/li>\n\n\n\n<li><strong>Automation<\/strong>. Automate repetitive tasks to save time and streamline data processing.<\/li>\n\n\n\n<li><strong>Improved security<\/strong>. If implemented correctly, you can gain better control over your website&rsquo;s security measures.<\/li>\n\n\n\n<li><strong>Cost savings<\/strong>. You don&rsquo;t have to rely on expensive third-party plugins to scale your website.<\/li>\n<\/ul><h2 class=\"wp-block-heading\" id=\"h-continue-building-with-javascript-in-wordpress\">Continue building with JavaScript in WordPress<\/h2><p>By adding PHP code to your WordPress core files, you can control how the server processes data and constructs pages before they reach the user. But a modern website needs more than server-side logic &ndash; it also needs interaction.<\/p><p>PHP handles the server-side work, while JavaScript runs in the browser to power dynamic features like live search, instant form validation, and interactive maps without requiring a page reload.<\/p><p>Combining PHP&rsquo;s data processing with JavaScript&rsquo;s interactivity is the standard approach for advanced plugin development and custom theme design. To round out your toolkit, read our guide on <a href=\"\/ng\/tutorials\/wordpress-javascript\">how to add JavaScript to WordPress<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Adding PHP to WordPress lets you add custom functionality, automation, and specific logic directly to your website&rsquo;s architecture. By adding these PHP snippets, you can customize theme behavior, extend plugin capabilities, and enhance security features beyond the default settings. WordPress doesn&rsquo;t allow direct PHP input in the block or classic editor because it would expose [&#8230;]<\/p>\n<p><a class=\"btn btn-secondary understrap-read-more-link\" href=\"\/ng\/tutorials\/how-to-add-php-code-to-wordpress\">Read More&#8230;<\/a><\/p>\n","protected":false},"author":115,"featured_media":144934,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_title":"2 quick methods for adding PHP to WordPress","rank_math_description":"Find out two effective methods for adding PHP to WordPress: using the WPCode plugin or adding the code manually to your WordPress files.","rank_math_focus_keyword":"adding php to wordpress","footnotes":""},"categories":[22637],"tags":[],"class_list":["post-676","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress"],"hreflangs":[{"locale":"en-US","link":"https:\/\/www.hostinger.com\/tutorials\/how-to-add-php-code-to-wordpress","default":0},{"locale":"fr-FR","link":"https:\/\/www.hostinger.com\/fr\/tutoriels\/ajouter-php-a-wordpress","default":0},{"locale":"es-ES","link":"https:\/\/www.hostinger.com\/es\/tutoriales\/insertar-codigo-php-en-wordpress","default":0},{"locale":"en-UK","link":"https:\/\/www.hostinger.com\/uk\/tutorials\/how-to-add-php-code-to-wordpress","default":0},{"locale":"en-MY","link":"https:\/\/www.hostinger.com\/my\/tutorials\/how-to-add-php-code-to-wordpress","default":0},{"locale":"en-PH","link":"https:\/\/www.hostinger.com\/ph\/tutorials\/how-to-add-php-code-to-wordpress","default":0},{"locale":"es-MX","link":"https:\/\/www.hostinger.com\/mx\/tutoriales\/insertar-codigo-php-en-wordpress","default":0},{"locale":"es-CO","link":"https:\/\/www.hostinger.com\/co\/tutoriales\/insertar-codigo-php-en-wordpress","default":0},{"locale":"es-AR","link":"https:\/\/www.hostinger.com\/ar\/tutoriales\/insertar-codigo-php-en-wordpress","default":0},{"locale":"en-IN","link":"https:\/\/www.hostinger.com\/in\/tutorials\/how-to-add-php-code-to-wordpress","default":0},{"locale":"en-CA","link":"https:\/\/www.hostinger.com\/ca\/tutorials\/how-to-add-php-code-to-wordpress","default":0},{"locale":"en-AU","link":"https:\/\/www.hostinger.com\/au\/tutorials\/how-to-add-php-code-to-wordpress","default":0},{"locale":"en-NG","link":"https:\/\/www.hostinger.com\/ng\/tutorials\/how-to-add-php-code-to-wordpress","default":0}],"_links":{"self":[{"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/posts\/676","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/users\/115"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/comments?post=676"}],"version-history":[{"count":38,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/posts\/676\/revisions"}],"predecessor-version":[{"id":144933,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/posts\/676\/revisions\/144933"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/media\/144934"}],"wp:attachment":[{"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/media?parent=676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/categories?post=676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hostinger.com\/ng\/tutorials\/wp-json\/wp\/v2\/tags?post=676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}