In many cases, you will need to add custom PHP code in the theme. In this article, let us explain some ways how you can do so:
1. Custom Layouts module in Astra Pro (Easiest)
Astra Pro Addon is a premium plugin that extends features of Astra theme. If you have purchased Astra Pro Addon, use the Custom Layouts module to add custom PHP code. Below are the steps that show how to add code –
Activate the Custom Layouts module from Astra Pro.Insert the code in the Code Editor.Select Hooks layout and choose the appropriate action / wp_head action.Choose to display it on the Entire WebsitePublish the layout.
Here is an example that shows how to add Google Analytics code. Similarly, you can add any custom code.
2. With Third-Party Plugin
There are free plugins available on WordPress Repository that you can use to add custom PHP code. Below are two of them:
Code SnippetsMy Custom Functions
3. In Child Theme
You can add a filter or custom PHP code through the WordPress Dashboard, follow the steps on how you can do the same –
Step 1: Install and Activate the Astra Child Theme from the WordPress Dashboard > Appearance > Themes > Astra Child Theme.
Step 2: Open the Theme Editor under the Appearance. Not seeing Theme Editor, troubleshoot the same.
Step 3: Navigate to the child theme』s functions.php, from the right-hand section and select functions.php.
Step 4: Add your code at the bottom of the file and click on Update File at the bottom.
Note: If you have the Parent theme active and visit the Theme Editor, you will see the following message, which informs you about how directly editing your theme could crash your website. Click on 「I understand」.
Or if you have access to cPanel of your website, you can use FTP software like FileZilla. Below are the steps that explain how to do so –
Step 1: Install and activate the Astra Child Theme from the WordPress Dashboard.
Step 2: Open your WordPress site via FTP. We would recommend you to use FileZilla.
Step 3: Navigate to wp-content > themes > astra-child
Step 4: Right-click functions.php and select View/Edit
Step 5: Add your code at the bottom of the file and save it.