2

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!

2
  • How are you storing sermons? As a CPT probably? Commented Oct 22, 2012 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
    Commented Oct 22, 2012 at 12:49

2 Answers 2

1

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

9
  • 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
    Commented Oct 22, 2012 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
    Commented Oct 22, 2012 at 14:07
  • maybe this help prodeveloper.org/… Don't forget to refresh your permalinks from admin panel Commented Oct 22, 2012 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
    Commented Oct 22, 2012 at 14:35
  • try add_rewrite_tag Commented Oct 22, 2012 at 14:39
0

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!

1
  • 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
    Commented Feb 27, 2020 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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