WordPress.org

Ready to get started?Download WordPress

Forums

Custom url rewrites needed (2 posts)

  1. gr1zzly
    Member
    Posted 4 years ago #

    I have a custom search on an external products database which I output results to a WP Page using a custom template file. I need to use a specific seo url structure for this page...

    - Currently the Page's url is '/equipment'

    What I would like to do is use '/equipment/search' for the search results and 'equipment/{manufacturer_name}/{part_number}' for the individual product details page, preferably using the same template file.

    I have added a couple of custom URL params in 'functions.php' using the following:

    function custom_query_vars( $vars ) {
    	$vars[] = ' load'; // Page type (i.e. Search or Details)
    	$vars[] = ' mfr'; // Manufacturer
    	$vars[] = ' prt'; // Part
    	return $vars;
    }
    add_filter('query_vars', 'custom_query_vars' );

    I also have another code snippet which I have been trying to adapt:

    function add_rewrite_rules($wp_rewrite) {
    	$wp_rewrite->rules = array(
    		'equipment/(?:\?load=(.+))' => 'index.php?page_id=227&load='.$wp_rewrite->preg_index(1)
    	) + $wp_rewrite->rules;
    }
    add_action('generate_rewrite_rules', 'add_rewrite_rules');

    I have also tried adding the Rewrite Rules directly into the htaccess file, outside of the WP block, but neither method seems to work.

    Please, there must be an easy way to add my own URL rules to wordpress?!

  2. chucklin72
    Member
    Posted 3 years ago #

    You can write a simple plugin and custom route the URL. Basically the idea is to hook into wordpress's hierarchy and instead of it returning "page not found", intercept and use the URL as you wish.

    I ran into this problem as well and I wrote a document on how to do this.
    Custom Routing

Topic Closed

This topic has been closed to new replies.

About this Topic