How to Add PHP Code Using a Plugin

January 26, 2024 / WordPress

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-

  1. Log into WordPress.
    WordPress Login
  2. Search for Plugins, and click on “Add New.”
    Add new plugin
  3. Install “WPCode” and activate the plugin.
    Install and activate the plugin.
  4. Go to Code Snippets, click “+Add Snippet” option to access a library of ready-made code snippets for specific functions.
    Code Snippets
  5. Click the “Use Snippet” button under the “Add Your Custom Code (New Snippet)” option.
     Add Your Custom Code
  6. Type “Get Time Zone” in the “Add title for snippet” field.
    Get Time Zone.
  7. Set the Code Type using the dropdown menu, select “PHP Snippet”.
    Code Type
  8. Name your new PHP code snippet and enter the code in the Code Preview box.
    Code Preview
  9. Try Auto Insert and set the location to the Site Wide Footer.
    Insert
  10. Turn on the toggle in the top menu to activate the code snippet.
    TOGGLE
  11. Click on “Save Snippet.”
    Shortcode
  12. Use Shortcode to simplify your code. Click Copy, then paste the shortcode on your chosen WordPress post or page.
    Shortcode
  13. 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.
    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.

Spread the love
Dominos Search