Bas
Bas

Reputation: 118

Change permalink using PHP in Wordpress on one page

I am working on a cardealer website but there is a problem with the URL/permalink. When you want to share the page it doesn't use the actual URL but the permalink set on the page. I know this sounds logical but the URL is created in a external PHP file. This PHP file get's the information of the car out of the database and shows it on the page. The actual URL also get's build in this PHP file. So the permalink set in WordPress is a default and the actual URL is what I would like to share.

Permalink set in WordPress: This is the permalink set in WordPress

Actual URL on the page: Actual URL on the page

So if someone shares this page I want to send the actual URL (https://www.prinsauto.nl/vakgarage-aanbod-details/audi-a6-avant...) but not the permalink. Again, the actual URL is build in a PHP file.

How can I do that?

Upvotes: 1

Views: 1306

Answers (1)

user1165759
user1165759

Reputation: 333

WordPress is not very easy when it comes to custom URLs. Viewing the code of the template-loader.php file, it seams that you could use a template file to run your page functionality.

As can seen in the code (line 77) you can use the template_include filter in your plugin (or template's functions.php file) to check for your specific "route" and return the template file to load.

Example:

add_filter('template_include', 'your_function_name', 0, 1);

function your_function_name($template) {
  $uri = $_SERVER['REQUEST_URI'];
  if (strpos($uri, 'vakgarage-aanbod-details') !== false) {
    return 'template-file-name.php';
  }
  else {
    return false;
  }
}

All the PHP logic you described in your question should go in included template file.

Upvotes: 1

Related Questions