I have following scenario:
my-domain-name/google
my-domain-name/twitter
I need a custom permalink structure to pass google
or twitter
to domain_name
query variable.
I have tried following code:
add_rewrite_tag('%domain_name%', '([^&]+)');
add_rewrite_rule('^/([^/]*)/?','index.php?domain_name=$matches[1]','top');
But not working
For better example Check this site: I am trying to do the similar
I have following scenario:
my-domain-name/google
my-domain-name/twitter
I need a custom permalink structure to pass google
or twitter
to domain_name
query variable.
I have tried following code:
add_rewrite_tag('%domain_name%', '([^&]+)');
add_rewrite_rule('^/([^/]*)/?','index.php?domain_name=$matches[1]','top');
But not working
For better example Check this site: http://currentlydown/facebook
I am trying to do the similar
I'm not sure if I should assume that you were issuing your examples inside of the init hook, but if not that is the reason they weren't working for you.
So inside functions.php
function sd_rewrite_rules() {
add_rewrite_tag('%domain_name%', '([^&]+)');
add_rewrite_rule('^/([^/]*)/?','index.php?domain_name=$matches[1]','top');
}
add_action('init', 'sd_rewrite_rules', 10, 0);
Then if you wanted to retrieve the query variable to do something with it, you would do something like this in a page template file using the $wp_query global.
$domain_value = $wp_query->query_vars['domain_name'];
Everything you would want to know plus more can also be located by reading all about the Rewrite_API and it has tons of examples of doing other things you might find interesting or relevent.
https://codex.wordpress/Rewrite_API
The problem was solved by just one line of code. I am thankful to @rifi2k for long discussion and proposing different solutions.
Here is the solutions:
Add following code to functions.php
function custom_rewrite_basic() {
add_rewrite_tag('%domain_name%', '([a-z]{1,60}\.[a-z]{2,})');
}
add_action('init', 'custom_rewrite_basic');
Then from permalink settings, select Custom Structure
and add newly created tag by only in our case its /%domain_name%/
save settings.