Wordpress Rewriting Rules - wordpress

I have a Wordpress Multisite installation and I add custom post type named course in blog 1 and then I select the other blogs where I want see it.
In other blogs add a page that I use like single course template.
The url of this page is
http://example.com/blogName/course/?course-name=lorem-ipsum&course-id=xx
I would like to have a url like this
http://example.com/blogName/course/lorem-ipsum
and i would like to get the course-id parameter in template page.
I tried to use add_rewrite_rule but i'm not able to do what i want.
add_filter('query_vars', 'my_query_vars', 10, 1);
function my_query_vars($vars) {
$vars[] = 'course-name';
return $vars;
}
add_action( 'init', 'init_custom_rewrite' );
function init_custom_rewrite() {
add_rewrite_rule(
'^course/([^/]*)/?','index.php?course-name=$matches[1]','top');
}
How can I do this?
I need to add something to .htaccess?

Once you've executed the above hooks, find a way to call the flush rewrite function which will update the rewrite cache so it should start working.
flush_rewrite_rules( true );
Documentation for this function can be found on the developer docs site.
You can indirectly call that function too by just saving the Permalinks settings in the dashboard by going to Settings > Permalinks.

Related

Wordpress rewrites not working and shows 404 page

I want to load a page with this requests:
/foo/bar
/foo/bar/bar
But this isn't working and shows 404 page:
function custom_rewrite_rule() {
add_rewrite_rule('^foo\/','index.php?page_id=205533','top');
}
add_action('init', 'custom_rewrite_rule');
The same piece of code worked.
But the settings for the Permalinks needed to be saved again.
From WordPress Administration Screens, Select Settings -> Permalinks and just click Save Changes without any changes.
function wpd_foo_rewrite_rule() {
add_rewrite_rule(
'^foo/([^/]*)/?',
'index.php?page_id=$matches[1]&param=foo',
'top'
);
}
add_action( 'init', 'wpd_foo_rewrite_rule' );
This takes whatever comes after foo/ and sets that as pagename for the query, and then param gets the static value foo. If you need different URL patterns, you'll need extra rules for each unique pattern

WP Rewrite structure page with id parameter ?id=[id] to /id/[id]

Now I have a page Detail Field (slug: detail-field, template name: Detail field template)
Currently, I use parameter ?id=[id] for send data to this page: http://example.com/detail-field?id=69. Base on $_GET['id'] It will get data of field and show to front-end.
But this slug not good for SEO and have some problems when share to facebook social. So I want to change it to http://example.com/detail-field/id/69
I'm not good at rewrite and I don't know where to start to do this. Can you guys help me?
WordPress Rewrite Structure:
Add Below function in function.php
function myplugin_rewrite_tag_rule() {
add_rewrite_tag( '%detail-field%', '([^&]+)' );
add_rewrite_rule( '^detail-field/([^/]*)/?', 'index.php?detail-field=$matches[1]','top' );
}
add_action('init', 'myplugin_rewrite_tag_rule', 10, 0);
Then reset permalink.
Hope this works for you.

Wordpress add_rewrite_rule gives 404

I want to change the permalink structure for specific custom post type.i.e
http://test.com/brand/calvin-klien?mtype=tab2
^this is dynamic
To
http://test.com/brand/calvin-klien/mtype/tab2
^this is dynamic
Here is a piece of code I tried.
Registering add_rewrite_tag
function custom_rewrite_tag() {
add_rewrite_tag('%mtype%', '([a-z0-9\-]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);
add_action('init', 'wpse50530_journal_archive_rewrite', 10, 0);
Code1
function wpse50530_journal_archive_rewrite(){
add_rewrite_rule('brand/([a-z0-9\-]+)/([a-z0-9\-]+)/$','index.php?name=$matches[1]/?mtype=$matches[2]','top');
}
Code2
add_action('generate_rewrite_rules', 'work_list');
function work_list($wp_rewrite) {
$newrules = array();
$newrules['brand/([a-z0-9\-]+)/([a-z0-9\-]+)/$'] = 'index.php?name=$matches[1]&mtype=$matches[2]';
$wp_rewrite->rules = $newrules + $wp_rewrite->rules;
I have tried both above codes, flushed permalinks but still a 404. I dont know why it is creating $matches in htaccess as htacces doesnt know WHAT IS $matches
Also I have tried monkeyman-rewrite-analyzer plugin which is showing the correct matched result for my permalink but still word press showing 404. See attached screenshots for Code1 & Code2
The following code should help
add_action( 'init', 'so_27487849' );
function so_27487849() {
add_rewrite_rule(
'^brand/([^/]*)/mtype/([^/]*)/?',
'index.php?name=$matches[1]&mtype=$matches[2]',
'top');
}
Flush your permalinks and it should work.
I will continue Anand's code for some further changes. It was redirecting on name=$matches[1] and I need to stay at the same URL I hit, for this it must include the custom post type name.
add_action( 'init', 'so_27487849' );
function so_27487849() {
add_rewrite_rule('^brand/([^/]*)/([^/]*)/?','index.php?brand=$matches[1]&mtype=$matches[2]','top');
^--this
}
I got the Pretty URL... Yaaay!!!
BUT URL doesn't contain query string(i.e: mtype=tab1) and the rest of my code is useless, so we can achieve this by doing get_query_var('mtype') and I got the same value which was working in $_REQUEST['mtype'] and my code worked like a charm.
Also I deactivated the monkeyman pluggin
Just in case anyone has the same problem and checks out this page.
In the permalink settings page, check how the permalink structure is configured. The most common is to enter /%postname%/ in the custom structure field to let the rewrite_rules work properly.
before or after the call the function add_rewrite.. insert this code status_header(200);

rewrite rule wordpress not working

This quetsion has been ask al lot, but however I tried I get always a 404 page in WP
I have a templatepage (wp-content/themes/responsivewizzard.php cals: wizzard. ->
I Added a page in the admin sector. The name of the page = "tespage". The template I choose is "wizzard"
When in try example.com/wp/testpage -> I get my wizzard page. No worries.. but..
I like to add a subpage to my wizzardpage. For example:
example.com/wp/testpage/nice
When i try this I get a 404 page from WP
I added this code to my function.php file. This file is in wp-content/themes/responsive directory -> my default theme
function members_rewrite_rules()
{
add_rewrite_rule('testpage /(.+)/?$', 'index.php?pagename=testpage &subpage=$matches[1]', 'top');
}
add_action( 'init', 'members_rewrite_rules' );
function members_query_vars($query_vars)
{
$query_vars[] = 'subpage';
return $query_vars;
}
add_filter('query_vars', 'members_query_vars');
Please help me
I found out the soulution. You need to reset the permalinks in the setting menu..

Wordpress: Plugin for simple user profile pages

In WordPress, I am aware that tld.com/author/username exists for authors, but I am looking for a public user profile page for non-authors. I want to setup a simplistic "favorite's list" for members on my site. Users will create an account, and add posts they like. They don't need access to wp-admin.
I'm looking for something simple like tld.com/user/username -- not /user/?uid=1. Nice and "pretty". Just like how WordPress handles /author/admin, or /author/username.
I would also like to keep /authors preserved so that's accessible too.
I have tried many plugins like WordPress-Users, but it's not a "pretty" URL, also have tried complicated plugins like Members, profile-builder, wp-user-frontend.
I found the answer to this from #bybloggers answer found here. https://wordpress.stackexchange.com/a/58793/12920
I modified his code very slightly to tailor it to my needs, but this is the code that worked for me and was exactly what I was looking for:
// Create the query var so that WP catches the custom /member/username url
function userpage_rewrite_add_var( $vars ) {
$vars[] = 'member';
return $vars;
}
add_filter( 'query_vars', 'userpage_rewrite_add_var' );
// Create the rewrites
function userpage_rewrite_rule() {
add_rewrite_tag( '%member%', '([^&]+)' );
add_rewrite_rule(
'^member/([^/]*)/?',
'index.php?member=$matches[1]',
'top'
);
}
add_action('init','userpage_rewrite_rule');
// Catch the URL and redirect it to a template file
function userpage_rewrite_catch() {
global $wp_query;
if ( array_key_exists( 'member', $wp_query->query_vars ) ) {
include (TEMPLATEPATH . '/user-profile.php');
exit;
}
}
add_action( 'template_redirect', 'userpage_rewrite_catch' );
After this was in my functions.php file, I had to re-save my Permalinks.
Sometimes re-saving the permalinks didn't finish the job 100% and browsing to www.mysite.com/member/username would 404, so I had to manually flush the rules by putting this into my functions.php and loading my site once. Then removing it so I don't run it every time the site loads, since that's unnecessary overhead.
// Code needed to finish the member page setup
function memberpage_rewrite() {
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_action('init','author_rewrite');
I don't know if you will find this one, at least not for free. Have you checked out WPMU? I started writing a membership plugin a few months ago but never completed it and am now doing it in Symfony. Most WordPress membership plugins are either too complex to use or don't provide the features you need.
You should spec out what you need an get a local dveloper to build it for you, you might even be able to sell it if you do a good job.

Resources