Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

Thanks!

share|improve this question
    
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

2 Answers 2

I got my answer from this page.

The final code:

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

Thanks!

share|improve this answer

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]

UPDATE

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

share|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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.