Can't create rewrite rule - wordpress

I try to create rewrite rule for my custom post type. I need it to be something like "/%post_type_name%/%taxonomy_slug/%post_slug%". Here's the code I used to add rule:
add_action('init', 'episodes_rewrites_init');
function episodes_rewrites_init(){
add_rewrite_rule(
'^episodes/([^/]*)/([^/]*)/?$',
'index.php?post_type=episodes&seasons=$matches[1]&episodes=$matches[2]', 'top'
);
}
It doesn't work and i can't figure out why. Please help.

Well mate, I have never used add_rewrite_rule, WP codex says that is It is most commonly used in conjunction with add_rewrite_tag().
So just I show you how veterans used to do this rewriting rules in the old times.
But moreover If you are building a plugin for the masses you must avoid use WP 3.5 features Much people still use WP2.*.
These set of actions can help you:
add_action('init', 'flushRewriteRules');
function flushRewriteRules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
add_filter('rewrite_rules_array', 'insertMyRewriteRules');
function insertMyRewriteRules($rules)
{
$newrules['^episodes/([^/]*)/([^/]*)/?$'] = 'index.php?post_type=episodes&seasons=$matches[1]&episodes=$matches[2]';
return $newrules + $rules;
}
add_filter('query_vars', 'insertMyRewriteQueryVars');
function insertMyRewriteQueryVars($vars)
{
array_push($vars, 'episodes', 'seasons', 'post_type');
return $vars;
}

Well, I've just installed plugin called "Custom Post Type Permalinks", added url template for my custom post like "/%seasons%/%postname%/" and now it rewrites urls like I want. But I still don't know how to do it in code though...

Related

Wordpress Rewriting Rules

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.

can't use get_query_var when including template in wordpress

I am using this function (in Wordpress's functions.php file) to redirect a specific url to a specific php script file:
add_action('init', function() {
$url_path = trim(parse_url(add_query_arg(array()), PHP_URL_PATH), '/');
if ( $url_path === 'manoa' ) {
$load = locate_template('inc/manoa.php', true);
if ($load) {exit();}
}});
(i know i can use rewrite rules, but i rather use that for a few reasons).
it works great. except i can't use get_query_var on that page. any ideas how to fix it?
I think you should use a different add_action rather than init.
Try to use add_action('wp') instead.

WordPress: Why Isn't add_rewrite_rule Working?

Goal
A visible URL of:
/products/building-automation/details/productSKU
Should point to the real WordPress page of:
/products/details/?SKU=productSKU
Code
public function setup_frontend() {
add_rewrite_rule(
'products/(building-automation|lighting-controls)/details/([^/]*)',
'products/details/?SKU=$matches[2]',
'top'
);
// Other code logic...
}
From what I can tell, the $matches points to the portions of the regex that are in (), so in the example, $matches[1] would be building-automation, and $matches[2] would be productSKU.
I have tried the flushing the rules, many times. This is being called from inside a class, but that doesn't appear to be the issue. I'm attaching the function to the init event.
What is Happening
I am getting the 404 template. What am I doing wrong?
Edit
Turns out, the second part of the parameter HAS to be index.php. Since I was trying to point to products/..., it broke. So I've updated to this:
add_rewrite_rule(
'products/(building-automation|lighting-controls)/details/([^/]*)',
'?pagename=products/details&SKU=$matches[2]',
'top'
);
It's not working and point to the right page and template. However, the $matches part is still not coming through. In fact, $_GET['SKU'] is coming through as '$matches[2]'.
The first problem is that it's pointing to something that doesn't start with index.php. It has to point to index.php?stuff, unfortunately. But you can still use the page name, as shown below.
add_rewrite_rule(
'products/(building-automation|lighting-controls)/details/([^/]*)',
'index.php?pagename=products/details&SKU=$matches[2]',
'top'
);
Second, you need to register your query string, which is SKU. Add this:
add_filter( 'query_vars', 'product_details_rewrite_filter' );
function product_details_rewrite_filter( $query_vars ){
$query_vars[] = 'SKU';
return $query_vars;
}
You will still have a problem. $_GET['SKU'] will be $matches[2], not RIBU1C. To fix this, change $matches[2] to $2. Then it will work:
add_rewrite_rule(
'products/(building-automation|lighting-controls)/details/([^/]*)',
'index.php?pagename=products/details&SKU=$2',
'top'
);
1) Try to update permalinks.
Go to Dashboard->Setting->Permalinks-> button "Save changes"
2) Add action to your function
add_action('init', 'setup_frontend');
function setup_frontend() {
add_rewrite_rule(
'products/(building-automation|lighting-controls)/details/([^/]*)',
'products/details/?SKU=$matches[2]',
'top'
);
// Other code logic...
}
It doesn't work in this way... You trying to achive impossible with this code. Rewrites Rules in wordpress work with index.php routing, but not external pathes. So
products/(building-automation|lighting-controls)/details/([^/]*)
should be "translatable" to "wordpress" language (something like)
index.php?post_type=products&category=$matches[1]&scu=$matches[1]
notice, scu should also be defined as extra var.
edit 1:
Thing (I think) you trying to achive is really looks like a htaccess rule pattern, in this case you can use mod_rewrite_rules filter (it filter content that goes to .htaccess) to change it as you like it.
edit 2:
Advertisment - you can also use my debug bar plugin called debug bar rewrite rules to basically debug this data. Notice: (currently it doesn't support rules list via add_rewrite_rule, so you can only visually inspect rule, and see if it matches your expectation).

Cant make my wordpress url_rewrite work

I am working on a simple wordpress plugin to manage events.
I have a link in my layout where I want to link to a event-page which is located in my plugin folder.
I dont want the long url like localhost/project/wp-content/plugins/event-manager/event-page.php?eventid=1.
I want it to be like localhost/project/event/1. Where the 1 is referred to the eventid get.
I've searched a lot and still I cant find the way to get it worked.
My code is as follow:
register_activation_hook(__FILE__, 'link_rewrite');
function link_rewrite() {
add_rewrite_rule('^event/^[0-9]', plugins_url('event-manager') . '/event-page.php? eventid=$matches[1]', 'top');
flush_rewrite_rules(false);
}
add_filter('query_vars', 'link_rewrite_query_vars');
function link_rewrite_query_vars($query_vars) {
$query_vars[] = 'eventid';
return $query_vars;
}
Hope someone have a solution.

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