WordPress has stopped users from adding code directly to prevent attacks. To insert PHP code safely in a post, convert it into a shortcode.
You can create a shortcode manually in WordPress, but using a plugin makes it easier. Many plugins in the WordPress repository allow adding custom code.
In this guide, we’ll use a popular option called WPCode-
- Log into WordPress.
- Search for Plugins, and click on “Add New.”
- Install “WPCode” and activate the plugin.
- Go to Code Snippets, click “+Add Snippet” option to access a library of ready-made code snippets for specific functions.
- Click the “Use Snippet” button under the “Add Your Custom Code (New Snippet)” option.
- Type “Get Time Zone” in the “Add title for snippet” field.
- Set the Code Type using the dropdown menu, select “PHP Snippet”.
- Name your new PHP code snippet and enter the code in the Code Preview box.
- Try Auto Insert and set the location to the Site Wide Footer.
- Turn on the toggle in the top menu to activate the code snippet.
- Click on “Save Snippet.”
- Use Shortcode to simplify your code. Click Copy, then paste the shortcode on your chosen WordPress post or page.
- If you pick Auto Insert, you can make the PHP code run conditionally. In the free version, you get User and Page options like user role, page type, and specific URL. Add a new group, set the conditions, and click Update.
In this way, you can add PHP Code using a Plugin.
Once you’ve installed any plugin, you can Set up Auto-updates for WordPress plugins to stay up-to-date and give your users a faster, smoother website experience.