How to Use PHP Scripts in WordPress Posts or Pages

When creating a post or a page in WordPress, you can easily add elements such as embedded audio or videos by typing in your HTML code. However, you might sometimes want to add more functionality that can be attained only by using PHP scripts. You can create a template, but an easier way to execute PHP script on a post or a WordPress page is to use a shortcode. A shortcode lets you call PHP functions within posts or pages and also serves as a reusable holder for commonly used PHP scripts.

Instructions

    • 1

      Open the "functions.php" file of your WordPress theme in Microsoft Notepad.

    • 2

      Add your PHP script as a function. For example, the following PHP script displays "Hello World!":

      function myPHPscript ($atts) {

      echo "Hello World!";

      }

    • 3

      Define your shortcode by adding the following code in your "functions.php" file:

      add_shortcode('myshortcode', 'myPHPScript');

      Replace 'myshortcode' with the name of the shortcode you want to use when calling the PHP script on your WordPress page or post, and replace 'myPHPScript' with the name of your PHP function.

      Save your file.

    • 4

      Open the post or page you want to add the PHP script to, and within the brackets, type the shortcode you defined in the last step, as in the following example:

      [myshortcode]

    • 5

      Save your post or page, and view it to see the PHP script in action.

Related Searches

References

  • Photo Credit Comstock/Comstock/Getty Images

You May Also Like

Related Ads

Check It Out

Prep for Black Friday Checklist [Infographic]