I use wordpress and I have the following question,
I use the URL:
- www.mysite.com/page
- www.mysite.com/cat/page
- www.mysite.com/custompostype/post
The value of permalinks is %category%/%postname%, everything works fine except now I have to use two languages and I want to configure as follows:
www.mysite.com/EN/page
www.mysite.com/ES/cat/page
www.mysite.com/BR/custompostype/post
I want to change the structure of wordpress urls where the first rule is the language and to take it as variable value. Similar to /%lang%/%category%/%postname%/
i used the wp_rewrite and have had no success, any idea?
for now I just found a solution to my problem, which I handled sessions
<?php function init_sessions() {
if (!session_id()) {
session_start();
} } add_action('init', 'init_sessions'); ?> <?php function detecta_idioma() { if($_SESSION["IDIOMA"]["actual"]=="") {
$_SESSION["IDIOMA"]["actual"]="ES";
$_SESSION["IDIOMA"]["abreviatura"]="";
$_SESSION["IDIOMA"]["nombre"]="Español";
$_SESSION["IDIOMA"]["tag"]="ES"; } if($_GET["action"]=="idioma" &&
$_GET["lang"]) { unset($_SESSION["IDIOMA"]);
switch($_GET["lang"]) { case "EN":
$_SESSION["IDIOMA"]["actual"]="EN";
$_SESSION["IDIOMA"]["abreviatura"]="_en";
$_SESSION["IDIOMA"]["nombre"]="Ingles";
$_SESSION["IDIOMA"]["tag"]="EN"; break; case "ES": default:
$_SESSION["IDIOMA"]["actual"]="ES";
$_SESSION["IDIOMA"]["abreviatura"]="";
$_SESSION["IDIOMA"]["nombre"]="Español";
$_SESSION["IDIOMA"]["tag"]="ES"; break;
} } $IDIOMA=array("actual"=>$_SESSION["IDIOMA"]["actual"],"ab"=>$_SESSION["IDIOMA"]["abreviatura"],"nombre"=>$_SESSION["IDIOMA"]["nombre"],"tag"=>$_SESSION["IDIOMA"]["tag"]);
global $wp_rewrite;
$wp_rewrite->set_permalink_structure($_SESSION["IDIOMA"]["tag"].'/%category%/%postname%');
return $IDIOMA; } add_action('init', 'detecta_idioma'); ?>
works well on the route entries, eg www.misitio.com/en/post-demo, t not working with pages or categories or taxonomies, eg: www.misite.com/es/category/, www.site.com/es/page/subpage or www.site.com/br/taxonomy <----- Dont work, shows error 404 page
I can not use the plugin qTranslate and CMS plugin multulengual either they do not work well with the use of plugins Advanced custom fields and custom post types UI.
Thanks
Related
I want to give only two page (/home, /inbox) access to my user with role "Vendor", if user tries to access other pages than it will automatically redirect to "/inbox" page, I put the below code to achieve the functionality but after adding this to function.php, site again n again redirect and finally dies with message "Page isn't properly redirected". please suggest what is wrong with my tried code or any other solution.
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->slug,array("home","inbox"))) {
wp_safe_redirect('/inbox');
}
}
add_action('template_redirect', 'vendor_redirect');
The main issue the way I tried to get the page slug, the correct way to get the slug is "$post->post_name". also I put exit after wp_safe_redirect as well because codex suggest that:
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->post_name,array("home","inbox"))) {
wp_safe_redirect(get_permalink(get_page_by_path( 'inbox' )));
exit;
}
}
add_action('template_redirect', 'vendor_redirect');
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.
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...
Using Wordpress 3.1 and the latest Advanced Permalinks and Custom Post Type UI i have created a custom post type called 'people'. The url pattern for all children of this post type are people/jim, however when i view post i get stuck in an infinite redirect loop. This only happens when I use pretty permalinks, not when id's are used.
The permalinks structure used on Advanced Permalinks are:
Common Settings
Custom Structure: %postname%
Post
%postname%
Wordpress is redirecting the custom post type to itself instead of translating it as ?people=jim.
I have tried defining the post types myself in functions.php and then doing a flush but that doesn't seem to fix the issue as others have found. Greatly appreciate any fix!
After a lot of logging and debugging, I found that the function that was causing the infinite redirect was function the_posts($posts). If you comment out everything from if (is_single () && count ($posts) > 0) to just before remove_filter ('the_posts', array (&$this, 'the_posts'));, it ceases the infinite redirect, and seems to still function fine! The only side-effect is that if you go to /jim/ it will redirect you back to /people/jim/ (rather than just giving you a 404). For me, that was Good Enough, since it fixed this problem.
So, again, within advanced-permalinks.php, search for function the_posts and then comment it so it looks like the code below.
Why does this break it? Because the custom post type is_single and doesn't match any rules, apparently, which makes APL send it back for re-direction... endlessly. Blah. Perhaps there is a more clever way to do this, to check if it is a custom post type, or whatever, but just disabling it seemed to do fine by me.
/**
* Hook that is called when a post is ready to be displayed. We check if the permalink that generated this post is the
* correct one. This prevents people accessing posts on other permalink structures. A 301 is issued back to the original post
*
* #return void
**/
function the_posts ($posts)
{
/* DISABLED CODE BELOW:
// Only validate the permalink on single-page posts
if (is_single () && count ($posts) > 0)
{
global $wp, $wp_rewrite;
$id = $posts[0]->ID; // Single page => only one post
// Is this a migrated rule?
$migrate = get_option ('advanced_permalinks_migration_rule');
if ($migrate)
{
if (isset ($migrate[$wp->matched_rule]) && substr (get_permalink ($id), strlen (get_bloginfo ('home'))) != $_SERVER['REQUEST_URI'])
{
wp_redirect (get_permalink ($id));
die ();
}
}
else
{
// Get the permalink for the post
$permalink = $this->get_full_permalink ($id);
// Generate rewrite rules for this permalink
$rules = $wp_rewrite->generate_rewrite_rules ($permalink);
// THIS IS ESPECIALLY PROBLEMATIC PART FOR CUSTOM POSTTYPES
// If the post's permalink structure is not in the rewrite rules then we redirect to the correct URL
if ($wp->matched_rule && !isset ($rules[$wp->matched_rule]))
{
wp_redirect ( get_permalink ($id));
die ();
}
}
}
DISABLED CODE ABOVE */
remove_filter ('the_posts', array (&$this, 'the_posts'));
return $posts;
}
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.