Support » Developing with WordPress » Fetch Post HTML for “Admin Only” page

  • Hi,

    I am trying to create a feature that will allow admins to create a PDF of the front-end view of a post while in the admin area. The process is pretty simple.

    1. Fetch the HTML
    2. Send the HTML to the PDF maker

    Step two works without issue. The problem is on step one. I’m passing in the URL and I’m storing it in $html. I have used each of the following.

    $html = file_get_contents($url);
    $html = wp_remote_get($url);

    and this one (has two steps):

    $html_body = wp_remote_post($url);
    $html = $html_body['body'];

    All of these return the HTML for the login page. This is because I want to prevent non-logged in users from accessing the pages on this site. So going to any page, when not logged in, takes you to the login page. What I cannot figure out is how to somehow authenticate that this request is coming from a logged in user. It seems like I should be able to pass some args to one of the functions above that would handle that but I am having a really hard time with it when I Google it. I just get articles on how to password protect pages. Anything that points me in the right direction would be much appreciated.

    Oh, and I need the entire HTML for the post, not just the content. So I can’t use get_content().


    • This topic was modified 3 years, 3 months ago by swani.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello @swani,

    I can recommend you to use JSON API plugin so that you can easily fetch the data bypassing your user details in the URL. I hope it’s the best approach to fetch any data from another site. You can check the following articles.


    Moderator bcworkz


    I advise using the built-in REST API over the JSON API plugin. JSON has not been regularly maintained. REST is available in any site by default and is constantly maintained by the WP core team. The issue with either is you get a JSON response, not a complete HTML page. The HTML needs to be reconstructed by your code.

    Another possibility is to send requests through /wp-admin/admin-post.php where you can code a custom response by getting the appropriate templates for the particular request just as WP would do. While this may bypass the need to be logged in (a template could still cause problems for you), you should take measures to ensure the request is from a valid, proper resource by including some sort of security token with the request, such as a nonce.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Fetch Post HTML for “Admin Only” page’ is closed to new replies.