0

I will keep this very short.

I've read some tutorials about changing the following url for Wordpress:

localhost/mysite/werkvorm?item=test

To:

localhost/mysite/werkvorm/test

So I can get clean links for search engines. And the page loads like its just reading the values of the $_GET['item'] but with an easier to read url.

With the following code I tried to achieve my goal:

Functions.php:

/**
 * For rewriting URLS on werkvorm
 */

add_action('init', function(){
    add_rewrite_rule(
    '^werkvorm/([^/]+)([/]?)(.*)',
    'index.php?pagename=werkvorm&item=$matches[1]',
    'top'
    );
});

/**
 * Retrieve the data from the get parameter for werkvorm page
 */

add_filter('query_vars', function( $vars ){
    $vars[] = 'werkvorm';
    $vars[] = 'item';
    return $vars;
});

On the werkvorm page i can use:

$test = get_query_var( 'item' ); // to retrieve the data

Now when I use the url: localhost/mysite/werkvorm/test I'm getting a 404. However when I use localhost/mysite/werkvorm/?item=test it works fine only the url is not changed. Any help on this is appreciated.

5
  • 1
    Does changing the permalink structure to 'post name' not do the trick? (admin->settings->permalinks) Commented Nov 30, 2016 at 11:07
  • @Dan. Lol yup that was it. Thanks, i'm not so into Wordpress so i didn't know really. Commented Nov 30, 2016 at 11:10
  • No worries haha. Depending on your URL structure requirements, you may have to write some code. But the post name structure setting does the trick here Commented Nov 30, 2016 at 11:13
  • @Dan. Yeah, luckily I'm familiar with the rest of the required code haha but thanks for your help it saved me a lot of time. I can accept it as the answer below if you need any points. Commented Nov 30, 2016 at 11:16
  • I don't see why not :) Commented Nov 30, 2016 at 11:39

1 Answer 1

1

Change the Permalink Structure to 'Post Name' in admin->settings->permalinks.

Depending on your URL structure requirements, you may have to write some code. But the post name structure setting will do the trick here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.