WordPress rewrite URL to access plugin page - wordpress

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).

Related

Home page url rewriting Wordpress

I want to do a URL rewrite in the WordPress home page
I want to change my URL http://mysite.loc/?pays=senegal to look like http://mysite.loc/senegal.
The problem is that I am on the WordPress home page, so it will be confused with the URL of another page like http://transfert.loc/page-example.
I have already tried several optins but am completely blocked.
Here is my code example:
public function rewrite_urls(){
add_rewrite_tag( '%pays%','([^&]+)' );
add_rewrite_rule(
'([^/]+)',
'index.php?pays=$matches[1]',
'top'
);
}
Can someone help me please!
Thanks
Two problems I see- rewrite rules need to set query vars that will result in a successful main query. Setting just a custom var like slide doesn't parse to anything WordPress can load. Additionally, slide needs to be added to the recognized query vars for it to get parsed within a rule.
So, what would a rule look like that would load the front page posts in the main query? That's a good question- the posts page is a special case, the absence of any other query vars. I haven't found a way to do that with a rule, though it may exist.
An easier way to do this is with a rewrite endpoint:
function wpd_endpoint(){
add_rewrite_endpoint( 'page-example', EP_ROOT );
}
add_action( 'init', 'wpd_endpoint' );
Keep in mind that if you have code accessing values via $_GET, this still won't work, because WordPress doesn't put query vars there when rules are parsed. You can change the code to use get_query_var, or just assign it before the code tries to access it:
$_GET['page-example'] = get_query_var('page-example');

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>';
});

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"];

WordPress hard code URL structure

OK so I am creating a WordPress plugin that requires constant URL structures. For example, the plugin has a regular cron to run and a web hook so I want the following URLs
examplesite.com/plugin/webhook/
examplesite.com/plugin/cronjob/
Is there any way I can code this into the template so that these links will always be the same throughout multiple installations?
Thanks,
Chris
OK so eventually got a solution for this and thought I should share it for future reference. It is possible to create custom rewrite rules using the generate_rewrite_rules action reference.
To achieve this, I have created the action referencing a method
add_action('generate_rewrite_rules', 'plugin_generate_rewrite_rules')
The plugin method is -
function plugin_generate_rewrite_rules($wp_rewrite) {
// create plugin rewrite rules
$rewrite_rules = array (
'PLUGIN_NAME/cronjob/' => 'wp-content/plugins/PLUGIN_NAME/cronjob.php',
'PLUGIN_NAME/webhook/' => 'wp-content/plugins/PLUGIN_NAME/webhook.php',
);
// apply new rewrite rules
$wp_rewrite->non_wp_rules += $rewrite_rules;
}
This will work, other than the fact that the rewrite rules need to be flushed for these changes to be applied. This can be created by adding a WordPress register activation hook for when the plugin is activated.
function plugin_activate() {
// flush rewrite rules
global $wp_rewrite;
$wp_rewrite->flush();
}
register_activation_hook( __FILE__, 'plugin_activate' );
I hope this helps some people!

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.

Resources