• I am trying to keep within wordpress’s preferred way of doing thing rather then a work around or hack. The end result is a page route such as ?page=checkout and to be able to load a custom php file that still loads and has access to wordpress functions.

    I started off using custom page template, creating a page and setting the page template to my custom php so that I could use the slug to load the file.

    This was a little sloppy since now there are a bunch of black pages whos only purpose is to add a route and load the custom php.

    The files still uses things like get_header() && get_footer() for the most part but some are just form processing and a redirect to a thank you page.

    Next I messed around with adding add_submenu_page( NULL …) with a null parent as to not create a menu item. This also worked to accomplish what I wanted to do but it still did not feel like the correct way.

    So in attempts to clean this method up I started hooking into after_setup_theme

    function new_route() {
    
    	if($_GET['page'] == 'checkout'){
    		load_template( dirname( __FILE__ ) . '/templates/checkout.php' );
    		exit;
    	}
    
    }
    add_action( 'after_setup_theme', 'new_route' );

    Above is what I am currently using however is this really the best way to do this? It seems like a hack.

    The page route does not have to be ‘page=XYZ’ really any slug would do I just need to be able to pull in my custom php files based on a uri slug.

    I appreciate any advice.

  • The topic ‘Plugin Page Templates // routes – Best coding practice ?’ is closed to new replies.