WordPress : Execute external php file

If you have created a folder within your word press directory and have placed a php file there and you would like to execute this php file then this article will tell you in detail how to run php files external to your theme.There are 3 Methods explained in this article.You will not require a wordpress plugin to include the php file.

Method 1

Add this code to your theme’s or Child Theme’s functions.php file

function hook_addl_header_elements() {
 
 
require_once('/home/hosting-username/public_html/my-domain-folder-com/wordpress-dir/wp-content/custom-php/user.php');
 
 
 
<!-- html scripts to add in header can come here -->
<?php
}
add_action('wp_head', 'hook_addl_header_elements');

In the above code at line 4, we define the root path to the file on the web server.You will have to replace hosting-username and my-domain-folder-com/wordpress-dir , as per your web hosting.You can check the path to the external php file by using a FTP client to connect to your server.Instead of include php directive require_once is being used to include the php file only once.

This will execute the php file user.php whenever the site is loaded.This is suitable when you want the php file to do other things like capturing $_SERVER variables etc.

This file can execute word press loop WP_query as well.

However that is not recommended as a output in header will not be desired.

However it will do the job of running the php file perfectly.

Method 2

If you would like to include a external php file in a php page,simply create a new page template,using existing page.php file in your theme folder.It is recommended to create the new page template php file in the child theme folder,though you can create it in the main theme folder as well.

Create a new page in the admin editor and use the custom template you have just created.

You can add the following line to your php page

 require_once('/home/hosting-username/public_html/my-domain-folder-com/wordpress-dir/wp-content/custom-php/user.php'); 

How to customize the above code for your website has been explained in Method 1.

OR

if you simply want to run some php code on your server,then you can remove the loop from the new custom template you have created and paste your php code to be executed.The page will be required to be published and loaded for the php code to be run.

The following code snippets will further demonstrate where to place the php code on your wordpress page template to execute it.

In my theme page.php looks like as follows

/**
 * The template for displaying all pages.
 *
 * This is the template that displays all pages by default.
 * Please note that this is the WordPress construct of pages
 * and that other 'pages' on your WordPress site will use a
 * different template.
 *
 * @package themeName
 */
get_header(); ?>
 
   <section id="primary" class="content-area">
      <main id="main" class="site-main" role="main">
                
         <?php while ( have_posts() ) : the_post();
 
            get_template_part( 'template-parts/content', 'page' );
 
            comments_template();
 
         endwhile; ?>
       
      </main><!-- #main -->
   </section><!-- #primary -->
    
   <?php get_sidebar(); ?>
 
<?php get_footer(); ?>

In the above code The loop begins at line 16 and ends at line 22. You can delete lines 16 to 22 in above code and insert php code to be executed on the page template.so that it looks as follows

/*
Template Name: Run PHP
Template Post Type: post, page, event
*/
 
// Page code here...
get_header(); ?>
 
   <section id="primary" class="content-area">
      <main id="main" class="site-main" role="main">
                
         <?php
                              // you can place the code to include external php file on the page to execute 
                              // it if,you want it to be executed only when the page is loaded.
                           
                              require_once('/home/hosting-username/public_html/my-domain-folder-com/wordpress-dir/wp-content/custom-php/user.php');
 
                             // comment above line if not required
 
                             // Other Custom PHP Code follows
 
                             echo "<br><br>"."Hello I am PHP Code running on loading a page";
 
                             // You can also run WP_query loop here
                        ?>
       
      </main><!-- #main -->
   </section><!-- #primary -->
    
   <?php get_sidebar(); ?>
 
<?php get_footer(); ?>

Remember that you will have to create page/pages in the editor using the Run PHP page template file.The php code will be executed when the created page will be loaded with it’s link.

Now that is not the end of executing php in wordpress, we move on to Method 3.

Method 3 – Inserting php in wordpress using shortcodes

In this method to execute php in wordpress we shall create a custom function,create a shortcode for that function and we can place the shortcode in a page created with a fresh page template as described above or place the shortcode anywhere in your theme.

Though you can add code to your theme’s functions.php directly, it is recommended that you create a child theme .In the child theme create a functions.php file in addition to style.css.

In the functions.php file add a new function as under

 // The php code in this function will executed when the shortcode 
// we shall register is called from a page template or anywhere from the theme 

function tech_articles_hub_php_shortcode() 
{    // The code to be executed comes here .... 
     // if just want to execute a external php file include the php here
 
     require_once('/home/hosting-username/public_html/my-domain-folder-com/wordpress-dir/wp-content/custom-php/user.php'); 

// or just comment the line above and add your own code below this ... 

echo "Hello I am PHP code running on a page template from a shortcode";
}  

// now register the shortcode with wordpress

add_shortcode('execute_my_PHP', 'tech_articles_hub_php_shortcode');  

To execute the php code on a page in wordpress, add the following code in the page created using the custom page template as described in method 2 or anywhere else in your theme/child theme.


      do_shortcode('[execute_my_PHP]',true);

In this article we described how to execute a external php file and how to run php code in wordpress page using multiple methods.



That is all.

If you liked this article please do leave a reply and share it with friends.

Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.