Support » Plugins » Hacks » Rewrite a strange url

Viewing 6 replies - 1 through 6 (of 6 total)
  • It looks ok to me.

    You didn’t mention if it works?

    Thread Starter hitech95

    (@hitech95)

    I have tried but i have some problems.
    This is my code:

    add_action('init','initialize');
    function initialize(){
    	add_rewrite_rule('^api/(.*)\\?(.+)?','index.php?&packdata=$matches[1].$matches[2]','top');
    	add_rewrite_tag('%packdata%','(.*)');
    }
    
    add_action('template_redirect', 'template_redirect_file');
    function template_redirect_file(){
    	global $wp_query;
    	if($wp_query->get('packdata')){
    		if(file_exists(plugin_dir_path(__FILE__).'site/index.php')){
    			include(plugin_dir_path(__FILE__).'site/index.php');
    			exit;
    		}
    		echo "not yet implemented received: ". get_query_var( 'packdata' );
    		exit;
    	}
    
    }

    On register_activation_hook i execute also this: flush_rewrite_rules()
    Is it correct? I have to call this function on “init”?

    My problem is on “template_redirect” action i call the function but don’t work.

    Moderator bcworkz

    (@bcworkz)

    You are using action hooks and flush_rewrite_rules() correctly. Some of you regexp syntax looks a bit unusual, perhaps it is not working the way you think it should? Try experimenting with this tool: http://www.regextester.com/

    Thread Starter hitech95

    (@hitech95)

    The regx si working for me.
    There is an error on the previous post.
    The correct regx is ^api/(.*)?

    I have tried some solution, it seems work.
    I have a question: to receive the “include” parameter have I add an other rewrite tag?
    Thanks

    Moderator bcworkz

    (@bcworkz)

    I’m not sure I understand, I think you want to add another parameter “include” in addition to “packdata” as part of the url?

    If you wish to have a parameter available as a separate query var or it is necessary in forming the query, then you need an associated rewrite tag. Other ways to get a parameter is by serializing it with other data or having your script get it directly with $_GET['include'].

    Thread Starter hitech95

    (@hitech95)

    I have added the tag.
    It work thanks!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Rewrite a strange url’ is closed to new replies.