How can I pass a variable to a page with a SEF url?

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?


Solutions Collecting From Web of "How can I pass a variable to a page with a SEF url?"

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

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');