Is there a way that i can get the HTML source of a single post dynamically from within WordPress site?

for example, if i wanted an external html I would use:


or using cUrl, But I'm looking for a way to do it from within my WordPress to get full qualified HTML for one of my posts

Is that possible ?

Thanks in advance

  • Not sure what you mean exactly. Can you please explain more? Thanks. – Greeso Aug 4 '17 at 2:04
  • Including header, footer, and sidebar etc? Or just the post content? – Jacob Peattie Aug 4 '17 at 2:34
  • @Jacob yes including header, footer..etc – Huthayfa Aug 5 '17 at 10:48

If you have the post's ID, I assume that you are the owner of the blog ( Not because others can't have the ID, but because only the owner should do such tasks ).

To convert the post ID to the post's URL, you can use get_the_permalink():

$url = get_the_permalink( $id );

Afterward, you can use file_get_contents() to fetch its content, including the full source of that web page. So, your full code will be:

$url = get_the_permalink( $id );
$data = file_get_contents( $url );

Which data is your HTML source. Note that this will only work in the WordPress environment.

  • Thanks @Jack , indeed i want it to work from within WordPress environment, are there any solutions using WordPress internal functions/hooks to do the same ? – Huthayfa Aug 5 '17 at 10:53
  • Hi. Those are internal WordPress functions though. – Jack Johansson Aug 5 '17 at 10:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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