Cant make my wordpress url_rewrite work - wordpress

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.

Related

How to save SVG files in Wordpress from an external plugin

I am creating a plugin for Wordpress in the administrative section to manage content, but I need to upload .SVG files but they are not being saved in the UPLOADS folder, I have tried many ways but none of them solves my problem:
Try this:
function custom_mtypes( $m ){
$m['svg'] = 'image/svg+xml';
$m['svgz'] = 'image/svg+xml';
return $m;
}
add_filter( 'upload_mimes', 'custom_mtypes' );
Try this:
function theme_name_mime_types($mimes) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter('upload_mimes', 'theme_name_mime_types');
add_filter('mime_types', 'theme_name_mime_types');
and other options but without results, someone who can help me?
I've run into similar issues before in that WordPress has changed the way that works in the code over time. So your solution may worked at some point, but is now out of date. Use a plugin like this https://wordpress.org/plugins/svg-support/ or https://wordpress.org/plugins/safe-svg/ . That way if it changes again, you'll always be up to date with the correct way to do this.

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

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.

Can't create rewrite rule

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

Wordpress Plug-ins: How-to add custom URL Handles

I'm trying to write a Wordpress Plug-in but can't seem to figure out how you would modify how a URL gets handled, so for example: any requests made for:
<url>/?myplugin=<pageID>
will get handled by a function in my plug-in. I'm sure this is a very simple to do, but I'm pretty new to working with Wordpress and couldn't find it in the documentation.
In order to handle just a specific URL use the code below:
add_action('parse_request', 'my_custom_url_handler');
function my_custom_url_handler() {
if(isset($_GET['myplugin']) && $_SERVER["REQUEST_URI"] == '/custom_url') {
echo "<h1>TEST</h1>";
exit();
}
}
add_action('parse_request', 'my_custom_url_handler');
function my_custom_url_handler() {
if( isset($_GET['myplugin']) ) {
// do something
exit();
}
}
That should set you on the right direction. parse_request happens before WordPress runs any of the complicated WordPress queries used to get the posts for the current URL.

Resources