1

I need to convert this url : http://localhost/wordpress/mp3s/mp3/name?playlist=123

To This: http://localhost/wordpress/mp3s/mp3/name/playlist/123

Is it possible ?

Here is my try :

add_rewrite_rule( '^mp3s/mp3/([^/]+)/playlist/([^/]+)/?$',
    'index.php?songs=$matches[1]&playlist=$matches[2]',
'top' );
0

1 Answer 1

2

Add this code to functions.php file:

function custom_rewrite_basic() {
    add_rewrite_rule('^mp3s/mp3/([^/]+)/playlist/([0-9]+)/?', 'index.php?page_id={PAGE_ID}&songs=$matches[1]&playlist=$matches[2]', 'top');
}

add_action('init', 'custom_rewrite_basic');

function custom_rewrite_tag() {
    add_rewrite_tag('%songs%', '([^&]+)');
    add_rewrite_tag('%playlist%', '([^&]+)');
}

add_action('init', 'custom_rewrite_basic');

Create a file named my-custom-template.php in your themes root directory as following:

<?php
/**
 * Template Name: Music
 */
get_header(); 

global $wp_query;
echo 'Songs : ' . $wp_query->query_vars['songs'];
echo '<br />';
echo 'Play List : ' . $wp_query->query_vars['playlist'];
// ... more ...
get_footer(); 
?>

Using this template, create a page and replace {PAGE_ID} with built page ID in the first code.

Then, go to Setting -> Permalinks and click on Save Changes.

2
  • What is the Best way to get 123 value ?
    – daniel
    Mar 29, 2017 at 14:00
  • 1
    Code updated. Please accept your answer so the question is no longer considered unanswered; click the check mark under the vote arrows on the left.
    – MahdiY
    Mar 29, 2017 at 19:24

Your Answer

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

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