• I call this plugin from a custom made plugin.
    I’m wondering how do I make multiple routes.

    The routes that I’d like to set up are:
    ^find/practitioners/
    and
    ^practitioners/

    The error I’m getting from the below code is:
    Fatal error: Cannot redeclare WP_Router_Sample::generate_routes()...

    Here is the plugin code so far:

    // I'm adding an array as an argument with the different routes I'm trying to add
    add_action( 'wp_router_generate_routes', array('iabt_practitioner_find_routes', 'iabt_practitioner_results_routes', 20) );
    
    function iabt_practitioner_find_routes( $router ) {
        $route_args = array(
                            'path' => '^find/practitioners/',
                            'query_vars' => array( ),
                            'page_callback' => 'iabt_find_practitioner_route_callback',
                            'page_arguments' => array( ),
                            'access_callback' => true,
                            'title' => __( 'Demo Route' ),
                            'template' => array(
                                        'page.php',
                                    dirname( __FILE__ ) . '/page.php'
                            )
                    );
    
        $router->add_route( 'iabt-find-route-id', $route_args );
    }
    
    function iabt_find_practitioner_route_callback( ) {
        return "Congrats! Your demo callback is fully functional. Now make it do something fancy";
    }
    
    function iabt_practitioner_results_routes( $router ) {
        $route_args = array(
                            'path' => '^find/practitioners/',
                            'query_vars' => array( ),
                            'page_callback' => 'iabt_results_practitioner_route_callback',
                            'page_arguments' => array( ),
                            'access_callback' => true,
                            'title' => __( 'Returning BCST Results' ),
                            'template' => array(
                                        'page.php',
                                    dirname( __FILE__ ) . '/page.php'
                            )
                    );
    
        $router->add_route( 'iabt-return-route-id', $route_args );
    }
    
    function iabt_results_practitioner_route_callback( ) {
        return "Congrats! Your demo callback is fully functional. Now make it do something fancy";
    }

    https://www.ads-software.com/plugins/wp-router/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Jonathan Brinley

    (@jbrinley)

    Take a look at https://gist.github.com/jbrinley/f2a263db096eee3f0032. Your add_action() statement was incorrectly formatted.

    Regarding “Fatal error: Cannot redeclare WP_Router_Sample::generate_routes()”: that’s not coming from this code. It sounds like you made some modifications to the plugin itself, or you’re redeclaring that class elsewhere. Try removing the plugin and downloading a fresh copy of it to remove any modifications you made.

    Thread Starter Sjourney

    (@sjourney)

    Thanks, your instructions helped. I actually got it to run this way:
    https://gist.github.com/luzpaz/f9475abcc571b663d407

    The odd thing is that i needed to make this multiple call:

    add_action( 'wp_router_generate_routes', 'BT_practitioner_find_routes', 20 );
    add_action( 'wp_router_generate_routes', 'BT_practitioner_results_routes', 20 );
    add_action( 'wp_router_generate_routes', 'BT_practitioner_email_routes', 20 );

    This is a little while ago. But for everyone who is looking for something similar. I came up with a litter more convenient solution.

    I created a codeigniter like solution where the user can register routes in a simple way and my functions in combination with WP Routes will fix the rest.

    First I create a few vars:

    // Init all the routes

    $mv_route['beheer/user/%'] 		= 'MV_Seller/user/var1'; // Controller/Function/Var
    
    $mv_route['beheer'] 				= 'MV_Seller/show';
    $mv_route['beheer/profiel'] 		= 'MV_Beheer/profile';
    $mv_route['calculator'] 			= 'MV_Calculator/show';

    After that I init the routes generation

    add_action('wp_router_generate_routes', 'mv_generate_routes');

    Than I create my function

    function mv_generate_routes(WP_Router $router) {
    	global $mv_route;
    
    	foreach ($mv_route as $route_data => $uri) {
    		$route = mv_route_data(
    			(string) $route_data,
    			(string) $uri
    		);
    
    		if (!$route) {
    			continue;
    		}
    
    		$router->add_route($route['id'], array(
    			'path' => '^'. $route['route'] .'$',
    			'query_vars' => $route['vars'],
    			'page_callback' => array(
    				$route['class'],
    				$route['function']
    			),
    			'page_arguments' => $route['arguments'],
    			'access_callback' => TRUE,
    			'title' => 'WP Router Sample Page',
    			'template' => array(
    				dirname(__FILE__) . DIRECTORY_SEPARATOR .'no-sidebar.php',
    				locate_template('no-sidebar.php'),
    				'no-sidebar.php'
    			)
    		));
    	}
    }
    
    function mv_route_data($route, $uri) {
    
    	// Generate the Router id
    	$id = str_replace('/', '-', $route);
    	$id = str_replace('%', 'var', $id);
    
    	// Generate the router route
    	$route = str_replace('%', '(.*?)', $route);
    
    	// Get the class
    	$class = explode('/', $uri)[0];
    
    	// Get the function
    	$function = explode('/', $uri)[1];
    
    	// Get vars
    	$parts = explode('/', $uri);
    	$parts  = ($parts)? array_filter(array_slice($parts, 2)) : array();
    
    	foreach ($parts as $key => $value) {
    		$vars[$value] = $key+1;
    	}
    
    	// Get keys
    	$arguments  = ($vars)? array_keys($vars) : array();
    
    	return array(
    		'id' 		=> $id,
    		'route' => $route,
    		'class' => $class,
    		'function' => $function,
    		'vars' 	=> $vars,
    		'arguments' => $arguments
    	);
    }

    All you need to do is include the controller and add the functions

    So I include my controller:

    require_once('MV_Seller')

    This is the example of my Controller:

    <?php
    
    class MV_Seller {
    	public static function show() {
    		echo "My story";
    	}
    
    	public static function user($var) {
    		echo "Yet another story with vars ". $var;
    	}
    }

    Hope this helps. If you need more explanation. Please reply.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to add multiple routes’ is closed to new replies.