Creating pretty profiles in Wordpress - 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.

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

altering the htaccess file of my WordPress

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.

Can I edit my .htaccess to write some WorldPress URL's (custom rewrites)?

So here's the problem: We don't like the fact that WordPress doesn't allow duplicate slugs, even for sub categories meaning we cannot have urls like:
product-1/guides
product-1/articles
product-2/guides
product-2/articles
That's very annoying! One solution we are considering is setting up our slugs like this:
product-1/product-1-guides
product-1/product-1-articles
product-2/product-2-guides
product-2/product-2-articles
But in our htaccess - can we use it to pick up such urls and rewrite them as prettier urls which have the product name removed from the sub folder? We don't mind hard coding these as we'll only ever have 5-10 products on the site.
This would keep the WordPress install happy with unique slugs, but the SEO tick in the box with better looking urls.
I just need a hand with the syntax please?
EDIT 1:
After looking at the WordPress Rewrite API, I'm failing to get anywhere with what I think is a really simple test. I have the following code in my functions.php which is running as I tested an echo, but no rewriting is taking place?
add_action( 'init', 'productRewrites' );
function productRewrites() {
add_rewrite_rule('^wordpress/james?','index.php?author_name=jwilson','top');
}
Nothing happens when I hit:
mysite.com/wordpress/james
Edit 2:
Cool I realise I now have to click save each time. The problem I now have is the following does not work not when I use $matches[1] - it only works if I hard code the author_name value (to jwilson for example):
function productRewrites() {
add_rewrite_rule(
"writer/([^/]+)/?",
"index.php?author_name=$matches[1]",
"top");
}
When I use $matches[1] it just returns everything! So clearly isn't using ([^/]+) in the url?!
you have to reset permalink structure
in order to do that, move to Settings -> Permalinks and press Save changes button

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