Modify URL Structure

By: Shelley BB | Asked: 05/29/2024
ForumsCategory: How-toModify URL Structure
Shelley BBShelley BB asked 7 months ago
Hi Guys I'm using a number of different types of views and I need to modify the URL structure into a more SEO friendly format. Is this possible WITHOUT generating posts from entries? Example: I have a side-by-side comparison feature that displays a grid of form entries with a compare checkbox in each. The entries are cameras and accompanying specifications.

I have a second view that displays the comparison results after the compare button is clicked and this all works as expected.

The URLs display with the query parameters like this: /camera-comparison/?compare[]=1000&compare[]=1001

I need them to display like this: /camera-comparison/camera-name-vs-camera-name (camera name can be the entry key if needed)

I also need the canonical URL in the above friendly format.

I've read the docs but can't find anything for this use case. Is this possible?
2 Answers
Best Answer
Shelley BBShelley BB answered 6 months ago

I managed to create a solution and have added the code below in case anyone else wants to do this.

// Custom rewrite rules for comparison
function custom_rewrite_rules() {
add_rewrite_rule(
'^compare/([^/]+(?:-vs-[^/]+)*)$',
'index.php?pagename=compare&compare=$matches[1]',
'top'
);
}
add_action('init', 'custom_rewrite_rules');

// Add compare to query vars
function custom_query_vars($vars) {
$vars[] = 'compare';
return $vars;
}
add_filter('query_vars', 'custom_query_vars');

// Parse the compare query var into an array
function parse_compare_query_var($query) {
if (!is_admin() && $query->is_main_query() && $query->is_page('compare')) {
$compare = $query->get('compare');
if ($compare && !is_array($compare)) {
$query->set('compare', explode('-vs-', $compare));
}
}
}
add_action('pre_get_posts', 'parse_compare_query_var');


// Remove the default canonical URL
remove_action('wp_head', 'rel_canonical');

// Add custom canonical URL
function add_canonical_url() {
if (is_page('compare')) {
$compare = get_query_var('compare');

if ($compare) {
// Ensure $compare is an array
if (!is_array($compare)) {
$compare = array($compare);
}

// Filter out empty values
$compare = array_filter($compare);

// Generate the canonical URL
$canonical_url = home_url('/compare/' . implode('-vs-', $compare));
echo '<link rel="canonical" href="' . esc_url($canonical_url) . '" />';
}
}
}
add_action('wp_head', 'add_canonical_url');

 

document.addEventListener('DOMContentLoaded', function () {
const params = new URLSearchParams(window.location.search);
const compare = params.getAll('compare[]');

if (compare.length > 0) {
// Generate the clean URL with the new page name
const cleanUrl = '/compare/' + compare.join('-vs-');

// Update the URL in the browser without reloading the page
window.history.replaceState({}, document.title, cleanUrl);
}
});

Victor Font answered 7 months ago
Let me see if I understand you correctly. You want to filter a view without using a query string to pass parameters? Is that correct?
Shelley BBShelley BB replied 7 months ago

I'm not sure if that's the correct description. The views are based are selections by the user so it is a query, I just want to change how the URL of the resulting view is displayed.

Victor Font replied 7 months ago

When you pass parameters to a view filter, you have to use a Query string. I don't know of a work around to this. This is how view filters work. There is the frm-set-get shortcode that will set filter values, but you sitill need to pass the values through a query string.

You may be able to customize something with AJAX or REST that works outside of Formidable's structure to do what you want, but that would require a project engagement because you want to do something Formidable is not designed to do.

Shelley BBShelley BB replied 7 months ago

<p>Would that involve messing with the filters/queries themselves, or just how the URL is displayed? I don't want to compromise the stability of the application. Thanks!</p>

Victor Font replied 7 months ago

I have no idea what it would take. You're the first person to ever ask this question.

Shelley BBShelley BB replied 7 months ago

OKay, thanks for your help. Much appreciated!

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right