I'm trying to update my link from this: /?eb_event_id=106098978902 To this: /[guest_name_here]/[event_id_here]
Following this discussion, I was able to come up with this code:
In my functions.php:
/*rewrite rule*/
function custom_rewrite_rule() {
add_rewrite_rule('^next-event/([^/]*)/([^/]*)/?','index.php?page_id=69&guest_name=$matches[1]&event_id=$matches[2]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
add_filter('query_vars', 'add_query_vars');
function add_query_vars($aVars) {
$aVars[] = "guest_name";
$aVars[] = "event_id";
return $aVars;
}
/*end rewrite rule*/
In my next-event page:
/*
Template Name: Single Event Template (updated)
*/
get_header();
global $wp_query;
$guest_name = $wp_query->query_vars['guest_name'];
$test_eb_event_id = $wp_query->query_vars['event_id'];
echo $guest_name;
echo $test_eb_event_id;
When I try to test the URL, the rewrite is not working. I also clicked the save button in the permalinks page to apply the changes.
So when I enter: .php?page_id=69&guest_name=test&event_id=123
I get /?guest_name=test&event_id=123
Any help is highly appreciated. Thank you very much!
I'm trying to update my link from this: https://dev.brunchwork/next-event/?eb_event_id=106098978902 To this: https://dev.brunchwork/next-event/[guest_name_here]/[event_id_here]
Following this discussion, I was able to come up with this code:
In my functions.php:
/*rewrite rule*/
function custom_rewrite_rule() {
add_rewrite_rule('^next-event/([^/]*)/([^/]*)/?','index.php?page_id=69&guest_name=$matches[1]&event_id=$matches[2]','top');
}
add_action('init', 'custom_rewrite_rule', 10, 0);
add_filter('query_vars', 'add_query_vars');
function add_query_vars($aVars) {
$aVars[] = "guest_name";
$aVars[] = "event_id";
return $aVars;
}
/*end rewrite rule*/
In my next-event page:
/*
Template Name: Single Event Template (updated)
*/
get_header();
global $wp_query;
$guest_name = $wp_query->query_vars['guest_name'];
$test_eb_event_id = $wp_query->query_vars['event_id'];
echo $guest_name;
echo $test_eb_event_id;
When I try to test the URL, the rewrite is not working. I also clicked the save button in the permalinks page to apply the changes.
So when I enter: https://dev.brunchwork/index.php?page_id=69&guest_name=test&event_id=123
I get https://dev.brunchwork/next-event/?guest_name=test&event_id=123
Any help is highly appreciated. Thank you very much!
Share Improve this question edited Jun 15, 2020 at 8:21 CommunityBot 1 asked May 30, 2020 at 3:24 elimariaaaelimariaaa 1212 bronze badges1 Answer
Reset to default 0There's nothing wrong with my code. Maybe it was not refreshed instantly so the changes did not propagate right away.