I'm making a website for a small church. They would like to upload sermons to their site. I've looked for a WP plugin to do that, but none of them did exactly what they wanted. So, I'm making a plugin to do what they want.

I've made a page called 'sermons' that has a page template 'sermon archive'. That file will display the sermon archive. I'd like to have it so when they click on a sermon, it would go to sermon/my-sermon-name (rather than sermon?name=my-sermon-name). Is there a way to do that in WordPress?


  • How are you storing sermons? As a CPT probably? – Mridul Aggarwal Oct 22 '12 at 12:47
  • @Mridul Aggarwal I'm uploading them, and then storing the url, name, speaker, date, etc in a database (not created by WP). – Caleb Oct 22 '12 at 12:49

You have to either work it out with the WP_Rewrite class or add this to the .htaccess file

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^sermon/(.*)/ /sermon?name=$1 [L]


Forgot to mention that the htaccess code is supposed to be above the wordpress generated htaccess code

| improve this answer | |
  • 1
    I added that above the .htaccess (I changed ?name= to ?sermon_name=). In sermon.php (the page template), I set it to echo $_GET['sermon_name']. When I go to /wordpress/sermon/my-sermon, I get a 404 error. When I go to /wordpress/sermons/?sermon_name=my-sermon, it shows my-sermon. Is there something I'm doing wrong? – Caleb Oct 22 '12 at 13:37
  • I put this code into a plugin: add_rewrite_rule('^sermon/([^/]*)/?','sermons/?sermon_name=$matches[1]','top');. When I go to /wordpress/sermon/my-sermon, I get this error: Fatal error: Call to a member function add_rule() on a non-object in localhost/wordpress/wp-includes/rewrite.php on line 21 – Caleb Oct 22 '12 at 14:07
  • maybe this help prodeveloper.org/… Don't forget to refresh your permalinks from admin panel – Mridul Aggarwal Oct 22 '12 at 14:26
  • This code (add_rewrite_rule('^sermon/([^/]*)/?','index.php?pagename=sermons&sermon_name=$matches[1]','top');) goes to the sermons page, but it doesn't pass the sermon_name argument. How can I get it to do that? – Caleb Oct 22 '12 at 14:35
  • try add_rewrite_tag – Mridul Aggarwal Oct 22 '12 at 14:39

I got my answer from this page.

The final code:

function yoursite_init() {
    global $wp,$wp_rewrite;
    $wp_rewrite->add_rule('sermon/([^/]+)/?', 'index.php?pagename=sermons&sermon_name=$matches[1]', 'top');


| improve this answer | |
  • And, as autor sad in that page you should add $wp_rewrite->flush_rules(false); in yoursite_init() and remove it, after 1 execution to clear the sef-rules. – user313776 Feb 27 at 15:11

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.