• I’m having trouble calling a php file from within a php file. What I’m trying to do is create a shortcode that will call a php file that will display some content on a page.

    The shortcode part works, because I have displayed something simple to test that it’s working. I have used include in the past, but that was for when I wanted to use a function from another file. I want the whole file to run, not a function.

    I humored myself and tried include(“file.php”) and include file.php and neither works. From what I’ve read the exec command should work, but that hasn’t worked for me either.

    exec(‘php file.php’) is an example of what I’ve tried.

    Thanks for any suggestions.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Include should work for you, or if you NEED the file to be in place, require(‘php file.php’) though I’d suggest not having spaces in your file name, even if this is an example. Without seeing the parameters of what you’re attempting, it may be hard to help further.

    Dion

    (@diondesigns)

    If your PHP script wants to include a file from the same directory, the proper format would be:

    include(dirname(__FILE__) . '/file.php');

    If you omit the dirname(__FILE__) portion, PHP will look for the file in the current working directory (“cwd”), which in the case of WordPress will be either the root WP directory, or root/wp-admin.

    __DIR__ can be used in place of dirname(__FILE__) with PHP 5.3 or later. For more information:

    https://www.php.net/manual/en/language.constants.predefined.php

    Thread Starter djones8520

    (@djones8520)

    It doesn’t seem to be working. The code I’m using is

    function displayStaff(){
    echo "Test<br />";
    include(dirname(__FILE__) . '/displayStaff.php');
    echo "Test2";
    }
    add_shortcode('displayStaff', 'displayStaff');

    The 2nd php file is

    <?php
    	echo "<h1>Hi</h1>";
    ?>

    The Test and Test2 print, but not the Hi. The files are in the same direction.

    Thanks for the help.

    Dion

    (@diondesigns)

    Please try the following function. It should assist in your solving this problem. You’ll know what to do if you see the “file not found” message. If you see the “File found!” message but not the “test123” message, there is an error in your included file. or an open_basedir restriction is not allowing you to include the file..

    function displayStaff(){
    	if (file_exists(dirname(__FILE__) . '/displayStaff.php')) {
    		echo 'File Found!<br />';
    		include(dirname(__FILE__) . '/displayStaff.php');
    		echo '<br />Test123';
    	}
    	else {
    		echo 'File not found: ' . dirname(__FILE__) . '/displayStaff.php';
    	}
    }
    Thread Starter djones8520

    (@djones8520)

    Defiantly a stupid moment… The file name was staffDisplay.php. I always knew I should have if statements in case the file isn’t found, but I never do. I suppose sometimes you learn the hard way.

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Calling a php file within a php file’ is closed to new replies.