简体   繁体   中英

WordPress -Mainintaing URL parameters

I have created a custom table namely company Now I want to load respective company's details based on URL

I am thinking of creating URL such as example.com/?company=1 and example.com/?company=1&depart=2

Now after login employee will be redirected to its respected company URL. Now based on company URL ( $_GET['company'] )I will be setting some global vars which will contain company details such as COMPANY_NAME so that I can access on any page.

With this approach I have to maintain the URL parameters on every page load/redirect. And also maintain global vars. Please tell me if this is a good approach or if there is any other better way. I don't want to use multisite.

I have started with using query vars

add_filter( 'query_vars', 'addnew_query_vars' );
function addnew_query_vars($vars)
{   
    $vars[] = 'company'; 
    $vars[] = 'depart';     
    return $vars;

}
add_action( 'template_redirect', 'setVars' );
function setVars()
{   
    if($_GET['company']){
        set_query_var( 'store',$_GET['company'] );
    }else{
        set_query_var( 'company',2 );
    }
    if($_GET['depart']){
        set_query_var( 'depart',$_GET['depart'] );
    }else{
        set_query_var( 'depart',2 );
    } 

}

If parameters are set in the URL then it will get that param values else it will take the default one (ie 2 )

Edit

  • I need to create URLs for each company so that I can send to clients,therefore I have to use URL parameters anyway

Well, that's an old approach, now a days, Wordpress and php even all technologies are very flexible, they provide thousands of different ways to do the same thing. For your approach, best way is to do the things using Wordpress query param, which is a modified way to make an awesome url. If you don't prefer to share the company ids on the url then try using php session, which is also a good practise and also you have secure data. but with session you will not be able to share the url, so best way is to use the query param.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM