• I have WP installed in a subdirectory and would like to access it from the home page of the site (just to get a summary of the latest posts).

    I have been trying to use fread to access a php file in the WP subdirectory hoping that it would be executed there and just the output would be read onto the home page. But so far i have been getting errors.

    code on home page:

    <?php
    $summary_scr = fopen("https://www.star-channel.net/news/wp-content/themes/star/summary.php", "r");
    $summary = fread($summary_scr, 200000);
    fclose($summary_scr);
    print $summary;
    ?>

    error message on site:

    Fatal error: Call to undefined function: have_posts() in /home/star/public_html/news/wp-content/themes/star/summary.php on line 2

    Ought I be doing something to allow fopen to use a URL as the parameter?

    Does WP not allow me to remotely access it?

    Would I be better off bypassing WP and simply accessing the MySQL database from the home page?

    Any views?

    Rob

Viewing 4 replies - 1 through 4 (of 4 total)
  • you’re not really supposed to call pages like that directly in your theme. you’re not loading all the functions and such when you try and bypass. What does the summary.php do? How can you view it in your browser? Basically, figure out how to access just by typing a uri in your browser, and use that uri in your php code.

    Thread Starter RobR

    (@robr)

    Thanks for your reply manstraw.

    summary.php is a short version of the index.php which just has the 5 most recent posts as excerpts, ie summary. It needs sweeking but is the same as the index.php which works so summary.php should work.

    summary.php does not work even when executed in the browser. just changing the name from index.php to something else (summary.php in this case) gives the errors.

    the bit of code that opens files and reads them (shown above) seems to be working fine – at least with files that have no WP code.

    Unless anyone has a clever idea or can suggest something I will have to resort to accessing the WP database directly.

    Just put the following block at the top of the index.php file:

    <?php
    if(isset($_GET['summary']) && $_GET['summary']==1){
    include("summary.php");
    die();
    };
    ?>

    Then you can get the summary like this: https://example.com/path/to/wp/index.php?summary=1

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    The reason his summary.php is not working is because he’s using the theme’s index.php, thinking that’s the homepage’s index.php. It’s not, and you can’t access the blog directly from the theme directory like that.

    Anyway, you’re doing all this in perhaps the most difficult manner possible. If you just want access to the data, and don’t want the blog itself to display, and if this calling PHP script is on the same server, then just add this to the calling script (the one where you’re trying to do the fopen):
    require('path_to_your_blog_dir/wp-blog-header.php');

    Done and done. You now have full access to all the blog functionality. Write a Loop, it’ll work. Pull some data out using the $wpdb object. Access the plugins. Whatever, it’s all there.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘can I use fread to access WP remotely?’ is closed to new replies.