I knew WordPress was customisable, but whenever I’ve had an idea that wasn’t an out-of-the-box function, I wasn’t able to execute it without the help of someone else’s plugin.
As proof of my ongoing PHP learning, I managed to customise WP for my Haiku blog with custom functions and an RSS feed.
Previous fail
A while back I, tried to write a plugin that used javascript to make expandable/retractable link lists. I failed miserably; probably because I had next to no idea about PHP syntax. I tried to follow a tutorial, but it was too much for me (the same tutorial is almost too much for me now, but probably one of the best, most thorough and useful).
The purpose
This time around, while building Haiku, I wanted to create a fairly different structure to usual blogs. For each haiku based on a chapter of the bible, I wanted to structure to be category: book, title:chapter (and verses). Then on the archives page I could list all the chapters after the book name.
For this to make any sense to readers, I’d need to show both category and title in post headings, both on the site and in the RSS feed.
Hello custom functions
I didn’t realise the significance of Chris Pearson’s article How you can use WordPress functions to run a smarter blog the first time I saw it. When I read it again I realised it would be perfect for what I wanted to do with the category and title headings.
I had some code that was already doing the job of calling the category name. To streamline the code a little, I put the category and title code into one function.
function userfunc_cat_title() {
global $post;
foreach [ref]get_the_category([/ref] as $category) {
echo $category->cat_name . ' '; } the_title();
}
So now, anywhere that I would have put <?php the_title(); ?>
I can put <?php userfunc_cat_title(); ?>
and it shows the category and title together.
Small snag solved with conditional statements
I hit a small snag when I realised that not all my haiku would be directly from chapters of the bible; some would be topical. It would be superfluous and perhaps a little confusing for a topical haiku title to start with the category.
To fix that I needed to add a conditional statement saying “if the category is a book category then get the category name”. WP already has some useful Conditional Tags and an “in category” tag. It doesn’t support multiple categories or child categories; so I would have had to list all 66 books categories as if ( in_category(2) || in_category (3)...
What a pain!
Eventually I figured out that I could use the statement I was already using to get the category and say “if the category ID is more than 1 and less than 73 then get the category name” (I made all the book categories and they happened to range from 2 to 72, I guess the other four numbers got assigned to tags that I’d already made).
function userfunc_cat_title() {
global $post;
foreach [ref]get_the_category([/ref] as $category) {
if ($category->cat_ID > 1 && $category->cat_ID < 73) {
echo $category->cat_name . ' '; }} the_title();
}
Custom RSS feed
The question of a custom RSS feed using my new title function was a lot harder to find an answer for.
There is the Feed Wrangler plugin, but I didn’t feel like I needed something like that when I only wanted one custom feed.
I found the files feeds, they’re located in the wp-includes folder. They’re all titled feed-[feed type].php. Editing the RSS2 file changes the default feed, but I didn’t want to do that because Haiku was sharing WP files with another blog (see multiple wordpress blogs using one set of WP files).
After much searching, I found a couple of nice articles about custom feeds at Daily Cup of Tech and Lorelle on WordPress. However, these feeds use the URL to provide specific RSS content; not the kind of customisation I was looking for. Eventually (through my own hacking and a couple of old WP forum posts) I managed to modify enough files to produce my feed.
Steps for a custom feed
- Copy
wp-includes>feed-rss2.php
, modify it to your liking and save it asfeed-rss2-custom.php
. - Copy wp-rss2.php, change
feed=rss2
tofeed=custom
and change the file path inside that file fromfeed-rss2.php
tofeed-rss2-custom.php
, save it aswp-rss2-custom.php
. - Open
wp-includes>functions.php
and add
function do_feed_haiku() {
load_template( ABSPATH . WPINC . '/feed-rss2-haiku.php' );
}
somewhere near the other similar feed functions. - Open
wp-includes>default-filters.php
and add
add_action('do_feed_haiku', 'do_feed_haiku', 10, 1);
somewhere near similar feed filters.
Putting it in a plugin
However, if you’re ever going to upgrade WP, which is usually a good idea when an upgrade is available, you’d have to make these changes every time.
It’s much easier to put it into a plugin. Given my previous failure at writing a plugin, I hadn’t thought it would be easier to write a plugin, but after doing this much hacking and not breaking anything it didn’t seem so hard. It wasn’t. All it needed was the standard plugin header, a function to get the RSS, and the aforementioned filter to apply the RSS.
Plugin header
/*
Plugin Name: Custom Haiku Feed
Plugin URI:
Description: Adds a custom feed to your WordPress blog.
Author: kristarella
Version: 0.1
Author URI: https://kristarella.blog/
*/
Plugin function
function do_feed_haiku() {
header('Content-Type: text/xml; charset=' . get_option('blog_charset'), true);
$more = 1;
\the RSS code goes here or a file path to an rss file
}
Plugin filter
add_action('do_feed_haiku', 'do_feed_haiku', 10, 1);
Future proof customisation
By putting custom functions in the theme’s functions.php and custom feeds in a plugin, they should be fairly future-proofed.
Congratulations on making it to the end of this long-winded explanation! I hope that someone finds this useful, since it took me a lot of Google searches to figure this stuff out. 😉
Arjen says
Great post. I’d like to thank you for your email and the comments on my blog. I really appreciate it. I think I’m going to run my photoblog within WP, as I like it having it all under the same software.
Of course I’ll have a look at the software you mentioned.
At the moment I’ve created a photoblog category within WP, and excluded it from the general “blog” and the feed. I’ve created another feed for the photoblog.
After some research on Google I found that there are some WP plugins to show the EXIF data of an image.
Thanks so much for the time you’ve spent to sent me such a valuable email.
Keep up the good work,
Arjen
Matt says
I can see what you’re trying to do, but I think you lost me about halfway through..eep
I’ve saved all the links you’ve dropped in this article, might have to start educating myself a little.
kristarella says
Your welcome Arjen. Thanks for the email and the comment.
The main reason I like creating my own functions, rather than using plugins is because I don’t like being dependent on someone else, who might not be able to update or maintain the plugin in the future. That said, I use plenty of plugins that probably do much more complicated things than I’m capable of.
Matt, the links are probably the most educational bits, definitely check them out. As for the rest, this is potentially a confusing and boring post, but I wanted to write it because no one else has written it before. I tried to find this information and it took much longer than I thought it should. Hopefully it’ll be easier for someone else who finds this post. 😛
sandrar says
Hi! I was surfing and found your blog post… nice! I love your blog. 🙂 Cheers! Sandra. R.
angelina jolie says
I love your site. 🙂 Love design!!! I just came across your blog and wanted to say that I?ve really enjoyed browsing your blog posts. Sign: ndsam
Kevin says
Hi, I just came across this entry of yours when I was researching how to make my own custom feed, and it’s exactly what I needed. So thanks. Thankfully it’s the second hit when searching for “custom wordpress feed” in google. I needed a custom feed because I’m making some flash applications, and I wanted to integrate content from my wordpress blogs, so I’m using the rss2 feed as a data source. But the default feed doesn’t have everything I want. At first I altered the main rss2 file for wordpress until it had everything and was accessible in my apps, but I knew this wasn’t a permanent solution because it will be wiped out as soon as I upgrade, and I didn’t want to damage the validity of the rss feed for other readers. So I then tried a suggestion of creating a custom page that’s actually a feed which worked fine, but the theme I’m using is one that automatically creates a link in the navigation for every page that exists. So I had to modify the theme code to exclude the page number for the feed page, but I would have the same issue eventually when a newer version of the theme is available. Your solution is definitely the best option.
Thanks again.
Robert Simpson says
Hi Kristarella,
Fantastic tutorial! I’m trying to do something “sort of” similar and I was wondering if you had any idea how to go about it.
Basically I have my main feed and I’d like to exclude any post with a specific custom field value, in this case it’s “_rss_include” and “0”.
Any ideas? Any help would be awesome. You can see the code I’m currently using (and failing with) here at the WP support site: http://wordpress.org/support/t.....st-1690828
Cheers,
Robert