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');
Related
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>';
});
I need help altering the htaccess file of my WordPress site to all me to add new variables to the end of every url.
Like i want to add /es/ to the end of my WordPress URLs
Please guide they way i can do it
i tried
custom paramlink like below but it did not worked
/%post_id%/es/
As was pointed out in the comments, you cannot accomplish this with .htaccess.
It looks like what you want to do is add an endpoint to your posts:
https://codex.wordpress.org/Rewrite_API/add_rewrite_endpoint
add_rewrite_endpoint( 'es', EP_PERMALINK | EP_PAGES );
The above code will add the es endpoint to your posts and pages. Make sure you flush your rewrite rules (by visiting the permalinks settings page) after adding that line, otherwise it won't have any affect.
You could then check if the endpoint is in use like this:
global $wp_query;
if(isset( $wp_query->query_vars['es'] )) {
...
}
If your end goal is to localize your site, I recommend you using something like WPML instead.
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¶m2=222
/api-page/?param1=aaa¶m2=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¶m2=$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¶m1=$matches[1]¶m2=$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');
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.
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).