Tag Archives: php

Display the Contents of a File Using PHP

How to Display the Contents of a File Using PHP

PHP has a built in function to display the contents of a file. This is helpful if you want to update a webpage to reflect some data that is regularly updated or output to a file. It is also helpful if you have access to the web server and it is easier for you to update a text file than it is to edit html. Simply point your webpage to the text file and the contents of the webpage get updated automatically.

Here’s how it works:

echo file_get_contents('http://brockangelo.com/uptime.txt'); 

Easy enough. I used to have a message in the footer of this webpage that said “This server has been up for 14 days, 7 hours, 22 minutes.” All that was doing was reading the contents of the text file that got updated every hour.

Removing Categories from your RSS feed

Removing Categories from your RSS feed:
It’s pretty straightforward. Just go into your feed files and add a filter saying: “if the post is in one of these categories, don’t include it in the feed”. Well, that’s the basic concept. Here are the details:I edited the /wp-includes/feed-rss.php & /wp-includes/feed-rss2.php You probably should also do the atomfeeds and others found inside wp-includes, but everyone who is subscribed to our blog uses Google Reader, and afaik, Google uses RSS, so I didn’t bother. I added the following lines just after the beginning of the loop and right after the

while have_posts : the_post:

Add this php code:

if ((!(in_category(53))) &amp;&amp; (!(in_category(55))) &amp;&amp; (!(in_category(56)))) : {

Where 53, 55 & 56 are categories that you don’t want in your feed. Notice that this is an if statement and this ends with an opening curly brace. So all you have to do is close this if statement.

You must close the if statement at the bottom of the file by issuing a closing curly brace and an endif statement just before the endwhile. So the bottom of my “feed-rss.php” looks like this:

&lt;?php while (have_posts()) : the_post(); ?&gt;
&lt;?php if ((!(in_category(53))) &amp;&amp; (!(in_category(55))) &amp;&amp; (!(in_category(56)))) : { ?&gt;
&lt;title&gt;&lt;?php the_title_rss() ?&gt;&lt;/title&gt;
&lt;?php if (get_option('rss_use_excerpt')) { ?&gt;
&lt;description&gt;&lt;![CDATA[&lt;?php the_excerpt_rss() ?&gt;]]&gt;&lt;/description&gt;
&lt;?php } else { // use content ?&gt;
&lt;description&gt;&lt;?php the_content_rss(”, 0, ”, get_option(’rss_excerpt_length’)) ?&gt;&lt;/description&gt;
&lt;?php } ?&gt;
&lt;link&gt;&lt;?php permalink_single_rss() ?&gt;&lt;/link&gt;
&lt;?php do_action(’rss_item’); ?&gt;
&lt;?php } endif; ?&gt;
&lt;?php endwhile; ?&gt;

The Codex article can be found here: Customizing Feeds.

Aligning Navbar to both Left and Right

Aligning a Navigation Bar to both the Left side and the Right sides:

I have always thought it would be nice to have a navigation bar that aligns text to both the left and the right, so I finally took the time to get this working. Once I figured it out, it was very simple to setup.

So on the left you’ll have this:

And on the right you’ll have this:

It is as simple as an html table. Yep. Just think about it. Your navigation bar is a single row, with two columns. I set my left column to 70% and my right column to 30%. Then I can align each table to the left or right.

I set the entire table inside an unordered list, then each page link is just an li or a list item. Your header.php file will probably look similar to mine, but without the table. What you choose to do with your newly-balanced right side is up to you. I am displaying the current time with a greeting depending on the time of day. At my office we are displaying the visitors IP address (very handy on a LAN).

<div id="navbar">
		<table width="100%">
		<td width="80%"><li><a href="<?php echo get_settings('home'); ?>">Home</a></li>
		<?php wp_list_pages(); ?>
		<?php wp_list_categories(); ?>
		<td width="20%" align="right"><div id="txt"></div></td>

Using PHP code in Posts & Pages

There really is no other way around it: you need to be able to add php code to Posts & Pages if you are going to mod your WP install. I use so much PHP & custom SQL queries on the company intranet that WordPress is almost an aside to the main event.

But in order to use PHP code inside posts, pages & widgets, you need to get the Exec-PHP plugin. It is truly amazing. Once it is installed, just turn off your Rich Text Editor and start coding away. But what do you do when you want to display PHP code inside of one of your posts? There are times that you’ll want to show off your PHP code without executing it. That can be done too. Taken from the exec-php homepage:
If you just want to print out code and don’t want to execute it, e.g. like it is done here on this page, you have to make sure to convert your code to the correct XHTML representation.










This is, however, very tedious if you think you’ll put PHP in your code more than once. You can do this conversion much more intelligently by using the WP-Syntax plugin. It creates nice colorful code boxes and formats them to any code format (PHP, Java, HTML, C+, Ruby, etc). It looks like this:

&lt;div id=&quot;navbar&quot;&gt;
	&lt;ul&gt;	&lt;table width=&quot;100%&quot;&gt;
		&lt;td width=&quot;80%&quot;&gt;&lt;li&gt;&lt;a href=&quot;&lt;?php echo get_settings('home'); ?&gt;&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
		&lt;?php wp_list_pages('title_li=&amp;depth=1&amp;include=38'); ?&gt;
		&lt;li&gt;&lt;a href=&quot;http://brockangelo.com/category/ubuntu/&quot;&gt;Ubuntu&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href=&quot;http://brockangelo.com/category/wordpress/&quot;&gt;WordPress&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;a href=&quot;&lt;?php bloginfo('rss2_url'); ?&gt;&quot;&gt;RSS&lt;/a&gt;&lt;/li&gt;&lt;/td&gt;
		&lt;td width=&quot;20%&quot; align=&quot;right&quot;&gt;&lt;li&gt;&lt;a&gt;&lt;?php echo (date(&quot;g:i a&quot;)); ?&gt;&lt;/a&gt;&lt;/li&gt;&lt;/td&gt;