-1

I currently have a wordpress website where the template is fixed but I would like to have the content all stored into 1 file that I can use to update the content. The reasoning behind this is that I will have many different websites with the same template but different content. This way, if I ever required changes, all I would need to do is edit this 1 content.html file.

I have attempted to make a single HTML page where there would be different divs for different variables of the website, however the entire text of the HTML file is showing on the wordpress website rather than the specific id DIV "homepagetitle".

How do I use the file_get_contents or anything similar to retrieve specific sections of information through to my php wordpress website?

THE HTML FILE:

<html>
<div id="homepagetitle">AGENT SUCCESS 2 </div>
<div id="other">othercontent</div>    
</html>

MY WORDPRESS SITE PHP FILE:

$homepagetitle = file_get_contents('http://neil1.zxstudios.ca/wp-content/themes/fullredpin5/content.html #homepagetitle');

        echo '<h1 class="intro-text">'.$homepagetitle.'</h1>';
Zed223
  • 3
  • 2
  • Tentative duplicate of http://stackoverflow.com/questions/3979802/alternative-to-file-get-contents?rq=1 and most of the related section. – Gordon Dec 14 '15 at 07:19
  • It is not a duplicate per se. The asker wants to know how to emulate something else completely which `file_get_contents` does not provide (HTML fragment fetching). – ojrask Dec 14 '15 at 07:24
  • 1
    You might try a solution with SimpleXML (`simplexml_load_file()`) and replace specific ids/attributes. – Jan Dec 14 '15 at 07:28

1 Answers1

0

That is not how file_get_contents works. file_get_contents gets all file contents for the the specified target source. From the looks of it, I think you're coming from jQuery, where the .get() method allows to fetch page fragments with the #-identitifer.

If you want to emulate fetching a page fragment (e.g. #homepagetitle), look into DOM document parsing methods available in PHP (http://php.net/manual/en/class.domdocument.php). In pseudo-code:

$file = file_get_contents('/path/to/file.html');

$domParser = new DOMDocument($file);
$element = $domParser->getElement('element-id-here');
$text = $element->text;

echo '<h1>' . $text . '</h1>';

If you don't want to do this parsing, you need to split the file to multiple files and fetch the contents for different parts one-by-one. If you'd split that HTML file to two parts, they don't even need to be HTML:

file1.txt:

AGENT SUCCESS 2

file2.txt:

othercontent

Then in WP templates:

echo '<h1>' . file_get_contents('http://example.com/file1.txt') . '</h1>';

echo '<div class="content">' . file_get_contents('http://example.com/file2.txt') . '</div>';

Also, you're in deep sh*t in case someone hacks your file system and replaces those files, which would result unwanted content displayed on all websites where those files are fetched and displayed on.

You're using WordPress. Have you considered creating a central WordPress installation and use its RSS feeds (or maybe the REST API as of WP 4.4) to display the content elsewhere?

ojrask
  • 2,799
  • 1
  • 23
  • 23
  • Thank you for that clear and very thorough answer!! I had not thought of utilizing RSS feeds or a centralized Wordpress Installation but I'll look into those right now. In the mean time, having an understanding of the above is very very helpful. – Zed223 Dec 14 '15 at 07:44