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>';
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?
Related
I'm building a simple tool that generates some custom HTML based on data from the website.
After, we can simply publish this content to a Facebook page via the Facebook SDK (API).
The following PHP code is an excerpt of how the $content is set up:
$content = '';
$content .= '📍 The address<br>';
$content .= '🛀 2 bathrooms<br>';
Via some AJAX code, this $content is shown as HTML in a div on the page, where the user can further edit the texts.
Then, the user can click on 'share' and via AJAX the post is published to Facebook. I use the following PHP code to format the content before it is passed to the FB API:
$content = str_replace('<br>',chr(10),$content);
$content = html_entity_decode($content);
$content = strip_tags($content);
The problem is that the emojis are not showing on Facebook. When I test it with
$content = html_entity_decode('️');
Then the emoji is showing correctly, but it seems that it's not working because I get $content via AJAX/Jquery with
var content = $('#content').html();
And then pass it through AJAX.
So I suppose there is a formatting issue, but I can't wait a way to fix it..
EDIT: because the content is first shown on the page in a div, the emojis are turned into <img draggable="false" role="img" class="emoji" alt="📍" src="https://s.w.org/images/core/emoji/14.0.0/svg/1f4cd.svg">
After, my Jquery/AJAX code takes the contents of this div, so I assume it takes the above instead of the '📍'. But how can I work around this?
EDIT 2:
Passing
$content = '';
$content .= '📍 The address<br>';
$content .= '🛀 2 bathrooms<br>';
Directly to the Facebook API works and shows the emojis.
First using an other PHP function that adds the above code to a div via JQUERY/AJAX, and then getting the div's content using $('#content').html() and passing this does not work.
I found a working solution.
As I suspected, the problem was with Jquery's element.HTML(), which seems to format the data in some way.
I have now added a hidden input field, where I add and get the data via element.val() instead of element.html();
Also, to prevent the auto-formatting (perhaps caused by WordPress) of emojis into images, I have created some custom tags like {sun}, which are only replaced with their HTML codes (like ️) just before posting to Facebook.
So my school has this very annoying way to view my rooster.
you have to bypass 5 links to get to my rooster.
this is the link for my class (it updates weekly without changing the link)
https://webuntis.a12.nl/WebUntis/?school=roc%20a12#Timetable?type=1&departmentId=0&id=2147
i want to display the content from that page on my website but with my
own stylesheet.
i don't mean this:
<?php
$homepage = file_get_contents('http://www.example.com/');
echo $homepage;
?>
or an iframe....
I think this can be better done using jquery and ajax. You can get jquery to load the target page, use selectors to strip out what you need, then attach it to your document tree. You should then be able to style it anyway you like.
I would recommend you to use the cURL library: http://www.php.net/manual/en/curl.examples.php
But you have to extract part of the page you want to display, because you will get the whole HTML document.
You'd probably read the whole page into a string variable (using file_get_contents like you mentioned for example) and parse the content, here you have some possibilities:
Regular expressions
Walking the DOM tree (eg. using PHPs DOMDocument classes)
After that, you'd most likely replace all the style="..." or class="..." information with your own.
I have a web site that have HTML pages stored in a private folder. I want a PHP script that can read the HTML file then push it to the browser.
My tought was to get the html file with the file() function in PHP. Then echo() it to the browser. That works for the html content of the page. The images and the css does not follow however.
I heard of a "render" function in IIS or ASP that render the HTML content of a web page in a private folder then send the images in a binary format. Does PHP have something similar?
Currently I read the file as follow :
$htmlFile = file(PATHTOFILE);
echo(implode('',$htmlFile));
The reason we are trying to do that is to protect the url / information of the pages contained in this folder. The user will have to connect to the web service, then the PHP script will push the html pages
You can use the tag base to solve the problem of the relative path of the files, something like this:
$html = file_get_contents($url);
$html = str_replace('<head>', '<head><base href="FULL PATH OF DIR" />', $html);
echo $html;
CSS and images are not displayed because their paths in the HTML files is relative to HTML files, right? And if you have these CSS and images in the same private folder, how can you hope the user will fetch them?
Indirect, you should fetch CSS and images the same way you do with HTML. But this means you have to replace all paths in your displayed HTML, that is quite absurd. In fact, we are talking about some kind of proxy now... ?!?!?
Why you need it?
Anyway echo(file_get_contents($htmlFile)); is less stressful.
Another option if it is an <img /> tag and the image is also stored outside of the root you can just make the src= attribute as so:
src="get_image.php?file=thisfile.png" // add a $_GET if needed to distinguish files
then get_image.php:
$file = $_GET['file'];
// security checks if you wish
header(sprintf("Content-type: %s;",'image/png'));
readfile($file);
exit;
I am trying to write a blog system. The main page is consist of part of the content of blog entries.
The problem is how could I make sure the excerpt is truncated correctly, since the blog entries is stored in HTML code.
Thanks.
Your best bet would be to use strip_tags() to remove the HTML from it and show only the first 300 or so characters using substr. Otherwise you'd have to parse the HTML to break it at an appropriate place so as not to break the rest of your layout.
strip_tags() and wordwrap()
<?php
$blog_entry = '<div class="myclass"><p><h1>I am trying to write a blog system.</h1> The main page is consist of part of the content of blog entries.</p>
<p>The problem is how could I make sure the excerpt is truncated correctly, since the blog entries is stored in HTML code.</p>
<p>Thanks.</p></div>';
// Allow a couple of tags (<p>,<a>), or don't - wrap excerpts into your own CSS class in your UI
$thisExcerpt = wordwrap(strip_tags($blog_entry, '<p>,<a>'),50);
$thisExcerpt = explode("\n", $thisExcerpt);
$thisExcerpt = $thisExcerpt[0];
echo $thisExcerpt . '...';
?>
Outputs :
I am trying to write a blog system. The main...
I would like to integrate my tumblr feed in to my website. It seems that tumblr has an API for this, but I'm not quite sure how to use it. From what I understand, I request the page, and tumblr returns an xml file with the contents of my blog. But how do I then make this xml into meaningful html? Must I parse it with php, turning the relevant tags into headers and so on? I tell myself it cannot be that painful. Anyone have any insights?
There's a javascript include that does this now, available from Tumblr (you have to login to see it): http://www.tumblr.com/developers
It winds up being something like this:
<script type="text/javascript" src="http://{username}.tumblr.com/js"></script>
You can use PHPTumblr, an API wrapper written in PHP which makes retrieving posts a breeze.
If you go to http://yourblog.tumblr.com/api/read where "yourblog" should be replaced with the name of your blog (be careful, if you host your Tumblr blog on a custom domain, like I do, use that) you'll see the XML version of your blog. It comes up really messy for me on Firefox for some reason so I use Chrome, try a couple of different browser, it'll help to see the XML file well-formed, indented and such.
Once your looking at the XML version of your blog, notice that each post has a bunch of data in an attribute="value" orientation. Here's an example from my blog:
<post id="11576453174" url="http://wamoyo.com/post/11576453174" url-with-slug="http://wamoyo.com/post/11576453174/100-year-old-marathoner-finishes-race" type="link" date-gmt="2011-10-17 18:01:27 GMT" date="Mon, 17 Oct 2011 14:01:27" unix-timestamp="1318874487" format="html" reblog-key="E2Eype7F" slug="100-year-old-marathoner-finishes-race" bookmarklet="true">
So, there's lots of ways to do this, I'll show you the one I used, and drop my code on the bottom of this post so you can just tailor that to your needs. Notice the type="link" part? Or the id="11576453174" ? These are the values you're going to use to pull data into your PHP script.
Here's the example:
<!-- The Latest Text Post -->
<?php
echo "";
$request_url = "http://wamoyo.com/api/read?type=regular"; //get xml file
$xml = simplexml_load_file($request_url); //load it
$title = $xml->posts->post->{'regular-title'}; //load post title into $title
$post = $xml->posts->post->{'regular-body'}; //load post body into $post
$link = $xml->posts->post['url']; //load url of blog post into $link
$small_post = substr($post,0,350); //shorten post body to 350 characters
echo // spit that baby out with some stylish html
'<div class="panel" style="width:220px;margin:0 auto;text-align:left;">
<h1 class="med georgia bold italic black">'.$title.'</h1>'
. '<br />'
. '<span>'.$small_post.'</span>' . '...'
. '<br /></br><div style="text-align:right;"><a class="bold italic blu georgia" href="'.$link.'">Read More...</a></div>
</div>
<img style="position:relative;top:-6px;" src="pic/shadow.png" alt="" />
';
?>
So, this is actually fairly simple. The PHP script here places data (like the post title and post text) from the xml file into php variables, and then echos out those variable along with some html to create a div which features a snippet from a blog post. This one features the most recent text post. Feel free to use it, just go in and change that first url to your own blog. And then choose whatever values you want from your xml file.
For example let's say you want, not the most recent, but the second most recent "photo" post. You have to change the request_url to this:
$request_url = "http://wamoyo.com/api/read?type=photo&start=1"
Or let's say you want the most recent post with a specific tag
$request_url = "http://wamoyo.com/api/read?tagged=events";
Or let's say you want a specific post, just use the id
$request_url = "http://wamoyo.com/api/read?id=11576453174";
So all you have to do is tack on the ? with whatever parameter and use an & if you have multiple parameters.
If you want to do something fancier, you'll need the tumblr api docs here: http://www.tumblr.com/docs/en/api/v2
Hope this was helpful!
There are two main ways to do this. First, you can parse the xml, pulling out the content from the the tags you need (a few ways to do this depending on whether you use a SAX or DOM parser). This is the quick and dirty solution.
You can also use an XSLT transformation to convert the xml source directly to the html you want. This is more involved since you have to learn the syntax for xslt templates, which is a bit verbose.