Support » Plugins » Include php inside php using relative paths

  • One of my pages uses a custom script: /wp-content/themes/currenttheme/chat.php

    I want to include in the above php another one located in /forum/chat/index.php The index.php includes its own files and for integration reasons must not be moved.

    Not working:

    include('/../../../forum/chat/index.php');
    /* returns Error_1 */
    $root = $_SERVER['DOCUMENT_ROOT'];
    include($root."/forum/chat/index.php");
    /* returns Error_1 */
    $path   = getcwd();
    $myfile = "/forum/chat/index.php";
    include ($path.$myfile);
    /* returns Error_2 */

    The errors:
    Error_1

    Warning: require(D:/My Dropbox/xampp/htdocs/lib/custom.php) [function.require]: failed to open stream: No such file or directory in D:\My Dropbox\xampp\htdocs\forum\chat\index.php on line 17
    
        Fatal error: require() [function.require]: Failed opening required 'D:/My Dropbox/xampp/htdocs/lib/custom.php' (include_path='.;\My Dropbox\xampp\php\PEAR') in D:\My Dropbox\xampp\htdocs\forum\chat\index.php on line 17

    Error_2

    Warning: include(D:\My Dropbox\xampp\htdocs\forum/forum/chat/index.php) [function.include]: failed to open stream: No such file or directory in D:\My Dropbox\xampp\htdocs\wp-content\themes\currenttheme\chat.php on line 24
    
        Warning: include() [function.include]: Failed opening 'D:\My Dropbox\xampp\htdocs\forum/forum/chat/index.php' for inclusion (include_path='.;\My Dropbox\xampp\php\PEAR') in D:\My Dropbox\xampp\htdocs\wp-content\themes\currenttheme\chat.php on line 24

Viewing 4 replies - 1 through 4 (of 4 total)
  • Did you try ABSPATH?

    <?php
    echo "ABSPATH ".ABSPATH."<br />";
    echo "WP_CONTENT_DIR ".WP_CONTENT_DIR."<br />";
    echo "WP_CONTENT_URL ".WP_CONTENT_URL."<br />";
    echo "WPINC ".WPINC."<br />";
    echo "WP_PLUGIN_DIR ".WP_PLUGIN_DIR."<br />";
    echo "WP_PLUGIN_URL ".WP_PLUGIN_URL."<br />";
    echo "PLUGINDIR ".PLUGINDIR."<br />";
    echo "WPMU_PLUGIN_DIR ".WPMU_PLUGIN_DIR."<br />";
    echo "WPMU_PLUGIN_URL ".WPMU_PLUGIN_URL."<br />";
    echo "MUPLUGINDIR ".MUPLUGINDIR."<br />";
    echo "Parent theme ".TEMPLATEPATH."<br />";
    echo "Child theme ".STYLESHEETPATH."<br />";
    
    echo "Parent template_directory".get_bloginfo('template_directory')."<br />";
    echo "Parent template_url".get_bloginfo('template_url')."<br />";
    cho "Child stylesheet_directory".get_bloginfo('stylesheet_directory')."<br />";
    echo "Child stylesheet_url".get_bloginfo('stylesheet_url')."<br />"; 
    
    ?>

    ABSPATH returns Error_1 mentioned above.
    It finds the included index.php, but it fails to go on from there with the php’s included in index.php

    For some reason, it searches for them in root, so every file inclusion that follows is treated omitting /forum/chat and so on.

    Hmm–if this is an ‘external’ script, then WordPress constants wouldn’t be available with this.. Integrating_WordPress_with_Your_Website

    It is a simple chat script.

    Content of /forum/chat/index.php (partial)

    // Path to the chat directory:
    define('AJAX_CHAT_PATH', dirname($_SERVER['SCRIPT_FILENAME']).'/');
    
    // Include custom libraries and initialization code:
    require(AJAX_CHAT_PATH.'lib/custom.php');
    
    // Include Class libraries:
    require(AJAX_CHAT_PATH.'lib/classes.php');

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Include php inside php using relative paths’ is closed to new replies.