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

  • How to Run a PHP File in XAMPP

    The XAMPP suite of Web development tools, created by Apache Friends, makes it easy to run PHP (Personal Home Pages) scripts locally...

  • How to Call a Function in JavaScript

    Functions--named groups of statements that can be "called" or "invoked"--lie at the heart of JavaScript. Functions can be given special inputs, called...

  • How to Create a WordPress Template Page

    WordPress templates allow you to customize the look of certain page on your WordPress site, be it for a layout change or...

  • How to Build Web Site Script

    Open your Web editing tool and start a new Web page. When you save the page, be sure to give it the...

  • How to Run a PHP File

    PHP is a programming language designed for Web development and use. Almost every website uses PHP to display content and interact with...

  • How to Add Posts to My Pages in WordPress

    The WordPress website allows you to create a blog for free. The site includes a variety of blog templates, as well as...

Related Ads

View Blog Post

Enter the Geek Vs Geek Giveaway and Win a Roku!