Wordpress masking multiple urls to the single page - wordpress

I have a WordPress (4.0) site and the situation where multiple URLs are dynamically created. Something like this:
/api-page/111/222/
/api-page/aaa/bbb/
I need to point them to:
/api-page/?param1=111&param2=222
/api-page/?param1=aaa&param2=bbb
because only the "api-page" exists. On the "api-page" I have the shortcode that will handle the params.
I know how to redirect URLs with an htaccess file, but I need urls to stay as they are.
I manage to mask the urls with this line in an htaccess file:
RewriteRule ^api-page/(.+)/(.+)/$ /api-page/?param1=$1&param2=$2 [L]
But WordPress throws 404 errors.
How to accomplish this?

I found the solution. The solution not using htaccess. I put this snippet in the function.php file of the theme:
function api_rw_rules() {
global $wp;
$wp->add_query_var('param1');
$wp->add_query_var('param2');
add_rewrite_rule('api-page/(.+)/(.*)','index.php?pagename=api-page&param1=$matches[1]&param2=$matches[2]','top');
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action('init', 'api_rw_rules');
Last two lines in the function can be removed after setting rewrite rules
/*
global $wp_rewrite;
$wp_rewrite->flush_rules();
*/
The only downside of this solution is that variables are not available in the $_GET. They are available with get_query_var():
$param1 = get_query_var('param1');
$param2 = get_query_var('param2');

Related

Wordpress plugin URL rewrite

I have a wordpress plugin which enables me to add events to my website.
However the url structure is e.g.
domain.com/events/cat/event-name
I was looking into the plugin, however I was not able to locate the way how the plugin reacts to that request.
Thus comes my questions:
What different ways are there for a plugin to be called via a certain url structure (like above e.g. all urls which contain "/events/cat" belong to the plugin) ?
I was looking at my .htaccess file but it was unchanged, also i was looking if the plugin uses a
add_action('parse_request', 'handler_action');
But I couldnt find anything. Are there any other ways the plugin could be using?
Thanks a lot!
Its probably using a custom post type using register_post_type function,
this will then build a new URL rewrite rule
The other possible method is by using add_rewrite_rule
something like
add_action('init', 'your_plugin_rules');
function your_plugin_rules() {
add_rewrite_rule( "events/cat/(.+)/?$", 'index.php?events=$matches[1]', "top");
}
if you check and print the value of $wp_rewrite you can see all the rewrite rules
e.g.
add_action('wp_head', function() {
global $wp_rewrite;
echo '</pre>', print_r( $wp_rewrite, 1 ), '</pre>';
});

creating a URL rewrite in wordpress

I'm trying to create a custom URL rewrite in wordpress. Basically, I want users to be able set their browser to mysite.com/.well-known/stellar.toml and get the contents of the following file I uploaded as a media file: mysite.com/content/uploads/2016/11/stellar.toml.
I tried the following rewrite rule in functions.php, then saved the permalinks to flush the rewrite rules, but it still didn't work. Anyone see what I may have done incorrectly?
function custom_rewrite_basic() {
// add rewrite rule
add_rewrite_rule('.well-known/stellar.toml','wp-content/uploads/2016/11/stellar.toml','top');
}
add_action('init', 'custom_rewrite_basic');

Multi Param URL redirect issue in wordpress

I have a bit of an issue I cannot seem to get around.
On a WordPress install we have these special profile pages that were triggered by a URL structure such as this
/our-firm/profile/1/Some.Name
They wanted the name appended to the URL so the URL's were prettier. This looks like it was previously managed through some type of plugin before we got it. Now a recent WordPress update has corrupted that plugin, and this functionality no longer works. I have it where it at least works if you do not use the name, example:
/our-firm/profile/1/
But that of course is not what the client wants. I have attempted to remedy the issue with some .htaccess rewriting that follows:
RewriteRule ^our-firm/profile/([0-9]+)/([^+]+)?$ our-firm/profile?id=$1&name=$2 [L,QSA]
But I am not getting anywhere with it. What weirder is, and it may just be something I am over looking, but if I visit:
/our-firm/profile?id=1&profile_name=Some.Name
That even works fine.
Any help would be great on this. Thanks in advance!!
You can use add_rewrite_rule of WP (http://codex.wordpress.org/Rewrite_API/add_rewrite_rule)
Example:
function wptuts_add_rewrite_rules() {
add_rewrite_rule('^path-1/([^/]*)?$','index.php?page_id=PAGE_ID&nameSurname=$matches[1]','top');
flush_rewrite_rules();
}
add_action( 'init', 'wptuts_add_rewrite_rules' );
function add_query_vars($aVars) {
$aVars[] = "nameSurname";
return $aVars;
}
add_filter('query_vars', 'add_query_vars');
and You get the params in the page using $wp_query->query_vars["nameSurname"];

Creating pretty profiles in Wordpress

I have been working with wordpress for a while now, but one aspect I never tried, until now, is the rewrite rules. I can create a profiles page by using a template and catching a user's ID via GET, but I want to do something better.
That is, I want to rewrite the URLs to something like http://www.example.com/profiles/username
and this should hold for all the themes chosen. I think that's how Buddypress does it. Any ideas?
To change the author base, add the following to your functions.php file:
add_action( 'init', 'so16194116_new_author_base' );
function so16194116_new_author_base()
{
global $wp_rewrite;
$author_slug = 'profiles';
$wp_rewrite->author_base = $author_slug;
}
Visit the permalinks admin page after you implemented this, to flush the rewrite rules.

WordPress rewrite URL to access plugin page

Hi i am trying to access my plugin via a custome URL but i am unable to get the plugin to redirect, it just comes up 404 page not found, whereas what i want is page.php to be called instead. My plugin code is below (activate and deactivate functions of the plugin):
register_activation_hook(__FILE__,'activate');
register_deactivation_hook(__FILE__,'deactivate');
function setRewriteRules()
{
add_rewrite_rule( 'plugin-url/$', '/wp-content/plugins/my-plugin/page.php', 'top' );
}
function activate()
{
setRewriteRules();
global $wp_rewrite;
$wp_rewrite->flush_rules(true);
}
function deactivate()
{
global $wp_rewrite;
$wp_rewrite->flush_rules(true);
}
Any ideas what i am doing wrong?
Thanks in advance,
Chris
First off, giving the path that way is a bad idea, because if for any reason any of that changes, your plugin/theme breaks.
Ozh makes a great suggestion under the section Hardcoded Paths as far as those go...
Secondly, you're most likely going to be better off using *query_vars*. Have a look at http://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/ and possibly even http://planetozh.com/blog/2011/01/pretty-login-url-a-simple-rewrite-api-plugin-example/ for examples of how to do this.
Also checkout the WP_Rewrite Class as it has the best information on handling URL rewriting in WordPress.
Thirdly, you need to hook your setRewriteRules() function to the init hook, because the first time the rules are flushed, your rules will be removed.
So your code would look something like this (without any other corrections):
register_activation_hook(__FILE__,'activate');
register_deactivation_hook(__FILE__,'deactivate');
add_action('init', 'setRewriteRules');
function setRewriteRules()
{
add_rewrite_rule( 'plugin-url/$', '/wp-content/plugins/my-plugin/page.php', 'top' );
}
function activate()
{
setRewriteRules();
global $wp_rewrite;
$wp_rewrite->flush_rules(true);
}
function deactivate()
{
global $wp_rewrite;
$wp_rewrite->flush_rules(true);
}
Rewrite Rule Flushing
Here's some tips on flushing your rewrite rules...
Rewrite Rules are flushed automatically anytime you visit Settings > Permalink or anytime you make and save changes them in the Admin area.
Adding add_action('admin_init', 'deactivate'); might also be helpful so they are flushed anytime an Admin page is loaded.
Rewrite rules that don't redirect to the Wordpress index.php file are written to the .htaccess file. I don't think they are ever stored in the database. So, looking at your code, if your .htaccess file is not writable when the plugin is activated, then your rewrite rule is never added.
As Amereservant wrote you need to add your rewrite rule in an init (or perhaps generate_rewrite_rules) action, as otherwise it'll get removed anytime the rules are flushed (because as described they are not stored in the database).

Resources