Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

The title isn't the best, so feel free to change it to make it more clear...

Problem:

I am trying to load up a page from within a plugin and replace anything that WP puts out. Have it set so if they are on the front page of the site, it will display a PHP page within my plugin:

if(is_front_page()){

    $full_path = WP_PLUGIN_URL.'/'.str_replace(basename( FILE),"",plugin_basename(FILE));

    $url = $full_path . "/PLUGINNAME/file.php";

    include($url);

    die();
}

But thats not outputting the file. Even when I put the FULL path to the PHP file, it wont display it, or anything. It will only render up this point, in the source code:

 <meta name="generator" content="WordPress 3.3.1" />

Then after that, its nothing.

I have never been able to get include to work, I really don't want to use an iFrame solution.

Thoughts? :)

Thanks!

  • Dylan

UPDATE

So I used get_file_contents and the hook template_redirect to achieve this.

Going to do some testing on the live server to see whats up! :)

share|improve this question
    
doing echo file_get_contents("$url", true); works, but the CSS has to be loaded with full paths, is there any solution to fully display a page like include would? –  DylanJones_md Mar 29 '12 at 3:15
add comment

1 Answer 1

A better solution might be to do it along the lines of

/* In your main plugin file */
if ( ! defined( 'YOUR_PLUGIN_ABSPATH' ) ) {
    define( 'YOUR_PLUGIN_ABSPATH', dirname( __FILE__ ) );
}

/* In any file of the plugin */
if( is_front_page() ) {
    /* adjust path if file.php is in a subfolder */
    require_once ( YOUR_PLUGIN_ABSPATH . '/file.php' );
}

If you still have to deal with full paths, they're in your file.php.

Additional Option

Also, it might not be the best idea to use a conditional tag (if( is_front_page() )). Alternatively, you could have the plugin generate a shortcode, which outputs the desired markup.

This would have two benefits: For one, it is easily placed and moved within your (front-)page. For another, you can use it anywhere you like, and that without altering code.

/* In your main plugin file */
if ( ! defined( 'YOUR_PLUGIN_ABSPATH' ) ) {
    define( 'YOUR_PLUGIN_ABSPATH', dirname( __FILE__ ) );
}

/* In any file of the plugin */
function your_include( $atts ) {
    /* "path" is a shortcode attribute, you can use it to include several files */
    extract( shortcode_atts(
        array(
            'path' => 'file.php'
        ),
        $atts ) );

    require_once ( YOUR_PLUGIN_ABSPATH . '/' . $path );

    /**
    * file.php should be adjusted to save whatever you were echoing in before
    * in a variable (in this example $output), which is returned
    * by the shortcode function
    */

    return $output
}
add_shortcode( 'your-include', 'your_include' );
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.