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.

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

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

    function myPHPscript ($atts) {

    echo "Hello World!";


  • 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.

  • 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:


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

Related Searches


  • Photo Credit Comstock/Comstock/Getty Images
Promoted By Zergnet


You May Also Like

  • 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 Build Web Site Script

    Scripts add functionality to your website and can be used for such things as processing forms; activating buttons, menus or pop-up windows;...

  • 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 Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!