Wordpress url rewriting - php

I have a problem concerning url-rewriting in wordpress. I am currently working on a language plugin (nearly finished also) and as a last thing, I would like to see every url altered so that it contains the current language that has been selected by the user (or a default language if the user hasn't changed the language).
I don't have a problem altering the links, the problem lies with the rewriting done by the server. Below you can find how I change the links.
public function register_filters()
{
add_filter('page_link', array(get_class(),'alter_permalink'));
add_filter('post_link', array(get_class(),'alter_permalink'));
}
public function alter_permalink($permalink)
{
$permalink = str_replace(get_option('home'), '', $permalink);
$permalink = trim($permalink, '/');
//The next line is actually a method that is being called,
//but it will return a string like this.
$lang = 'EN';
return get_option('home') . '/' . $lang . '/' . $permalink;
//This returns a link that looks something like this:
//http://somedomain.com/EN/permalink-structure
}
So as you can see, I have no problems creating the links, the problem lies with the url-rewriting on the server itself.
I have tried this method: http://shibashake.com/wordpress-theme/wordpress-permalink-add
but I didn't get that one to work either. The problem is that I just don't seem to understand how these rewriting-rules work and that I can't seem to find a decent tutorial on the subject either.
Any help would be greatly appreciated.

the filter to listen to for handling url is request.
function request_handler($vars) {
//modified $vars here
return $vars;
}
add_filter('request', 'request_handler', 11);
i have written a plugin for customize url in wp. check out the source and see how i handle it.
http://wordpress.org/extend/plugins/auto-url/

Related

Wordpress rewrite with custom parameter doesn't work

I'm currently developing a plugin which takes a parameter from URL (example: example.com/product_id/1234 is converted to example.com) and passes it in WP_Query (supposedly this should be used instead of GET or POST) for later use. My code so far looks like this:
function shortcode_callback() {
echo get_query_var('product_id','not found<br>');
}
function prepare_rewrite() {
add_filter('query_vars','rewrite_add_var');
add_rewrite_rule('^product_id/([^/]*)/?$','index.php?page_id=18&product_id=$matches[1]','top');
flush_rewrite_rules();
}
function rewrite_add_var($q_vars) {
$q_vars[] = 'product_id';
return $q_vars;
}
add_action('init','prepare_rewrite');
add_shortcode('shortcode', 'shortcode_callback');
Unfortuantely, even though redirect works, the custom parameter does not exist. I've been trying to put this piece of code in functions.php file, but to no avail. I've been also trying to solve this issue by using add_rewrite_tag, but as far as I can tell it's only useful when it comes to post selection.

Multiple pages with same URL in WordPress

I have a WordPress site that has a standard Page called Places with URL
example.com/places/
And I have several child Pages called by cities
example.com/places/city-1
example.com/places/city-2
Now, I have a custom post type Place that should indicate a single place and should have permalink like
example.com/places/place-1
But then if I go to one of the previous links with city-1, city-2 I get 404 obviously because there is no place with that permalink.
Is there a way for WordPress to drop to previous permalink. So if there is no place with that name, look for a page with it.
You could probably use the the REFERER-value from the PHP server variable $_SERVER, but it´s not very reliable and can be altered.
I am using the plugin "Permalink Finder" in one of the pages I am maintaining and that works quite well for finding changed URL. You could give it a try and see if it works for you, too.
In case somebody ever having a similar problem, it can be done by using verbose page rules. This is an example I found at WordPress Stack Exchange
https://wordpress.stackexchange.com/questions/22438/how-to-make-pages-slug-have-priority-over-any-other-taxonomies-like-custom-post
add_action( 'init', 'wpse16902_init' );
function wpse16902_init() {
$GLOBALS['wp_rewrite']->use_verbose_page_rules = true;
}
add_filter( 'page_rewrite_rules', 'wpse16902_collect_page_rewrite_rules' );
function wpse16902_collect_page_rewrite_rules( $page_rewrite_rules )
{
$GLOBALS['wpse16902_page_rewrite_rules'] = $page_rewrite_rules;
return array();
}
add_filter( 'rewrite_rules_array', 'wspe16902_prepend_page_rewrite_rules' );
function wspe16902_prepend_page_rewrite_rules( $rewrite_rules )
{
return $GLOBALS['wpse16902_page_rewrite_rules'] + $rewrite_rules;
}

giving custom argument vlue in 'q' variable

**also posted on druapl.stackexchange
https://drupal.stackexchange.com/questions/150500/giving-custom-argument-vlue-in-q-variable
**
apologies if question is ambiguous . the scenario is as follows:
in drupal 7 , we want to use a custom template page when a specific value is given for the q variable in url .
for example if we give http://localhost/drupal/?q=xyz/123 , we want to use a custom template page say page-xyz.tpl.php ..
have a hunch that hooks and template.php file may be the key components here but not sure what to exactly do..
any help appreciated.
you could implement theme_preproccess_page() (or node, or html) to control this in your template.php
function YOURTHEME_preproccess_page(&$vars) {
if (isset($_GET['q']) && $_GET['q'] == 'xyz/123') {
$vars['theme_hook_suggestions'][] = 'page_xyz';
}
}
that should work, but I would like to recommend not use the '?q=xyz' solution, but do an preproccess that should work to all your pages, like this.
function YOURTHEME_preproccess_page(&$vars) {
$title = strreplace(' ','_', $vars['node']->title);
//if you use the transliteration module, instead of strreplace
//use transliteration_get($vars['node']->title);
$vars['theme_hook_suggestions'][] = 'page_'.$title;
}
now that should work for every page that you want to make a custom template. Just add the file and clear the chaches. If you don't have the page template to the specific page, it's ok, drupal will use the default.

opencart get the product seo url from within admin

I am extending the newsletter area so that I can send out an email with a list of products but I can't find out how to get the seo url for each product?
When I do this I actually end up with the admin URL.
$this->url->link('product/product', 'product_id=' . $row['product_id'])
This same code in the store front works fine and returns the seo url if it exists.
The only way I can get it to work is if I manually build the URL, obviously no seo url.
HTTP_CATALOG .'index.php?route=product/product&product_id='.$row['product_id']
Looking into this further I see that the admin area is missing the following code but I can't figure out how this actually works and ties in with $this->url->link so that I can modify it to work for me.
$controller->addPreAction(new Action('common/seo_url'));
UPDATE - in the end the easiest solution was to add my own method like this:
public function getUrl($route, $key, $value){
$url = "index.php?route={$route}&{$key}={$value}";
if($this->config->get('config_seo_url')){
$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "url_alias WHERE `query` = '" . $this->db->escape($key . '=' . (int)$value) . "'");
if($query->row['keyword']){
$url = $query->row['keyword'];
}
}
return $url;
}
Front-end links
The best way to make a front-end link from the Admin area without modifying core files is to create your own URL object:
$url = new Url(HTTP_CATALOG, $this->config->get('config_secure') ? HTTP_CATALOG : HTTPS_CATALOG);
To create URLs you then use $url instead of $this->url, so your example would be:
$url->link('product/product', 'product_id=' . $row['product_id']);
SEO URLs
Getting SEO URLs is a bit harder and unfortunately requires copying some code or a bit of bodging.
Copied Code
Basically, copy the entire rewrite($link) function from catalog/controller/common/seo_url.php and add it to the controller class that you're creating. Then add the following line after the new $url line mentioned above:
$url->addRewrite($this);
The current controller is then used as a rewriter and all URLs get rewritten. You might be able to use a separate class, but then there are dependencies to get the database access. This seemed the most direct way of doing it, even if it is ugly.
Your code should then be:
<?php
class ControllerMyController extends Controller {
public function index() {
...
$url = new Url(HTTP_CATALOG, $this->config->get('config_secure') ? HTTP_CATALOG : HTTPS_CATALOG);
if ($this->config->get('config_seo_url')) {
$url->addRewrite($this);
}
$url->link('product/product', 'product_id=' . $row['product_id']);
...
}
public function rewrite($link) {
... [stuff from seo_url.php] ...
}
}
and you'll get SEO'd URLs from the admin area.
Bodging
If you're happy with an arbitrary require statement, then you can alternatively do the following and use the existing SEO code (which means that it'll stay up-to-date if it changes, but will fail if the file moves).
To create SEO URLs this way your code needs to be:
$url = new Url(HTTP_CATALOG, $this->config->get('config_secure') ? HTTP_CATALOG : HTTPS_CATALOG);
if ($this->config->get('config_seo_url')) {
// Require the SEO file directly - path is relative to /admin/index.php
require_once('../catalog/controller/common/seo_url.php');
$rewriter = new ControllerCommonSeoUrl($this->registry);
$url->addRewrite($rewriter);
}
$url->link('product/product', 'product_id=' . $row['product_id']);
Administration part do not need the preAction for SEO, as there are no SEO links in administration.
The problem You are getting is because on frontend the constant HTTP[S]_SERVER points to http[s]://mydomain.com/ while in administration the same constant points to http[s]://mydomain.com/admin/.
What are You looking for is maybe an edit of url class (system/library/url.php) and adding a new method frontend_link by copying link method and replacing HTTP[S]_SERVER constant in the method body by HTTP[S]_CATALOG constant that is present only in administration...
Expanding a little on #IBBoard's very informative answer: in Opencart 3.0.2 and above, seo_url.php is no longer in the catalog/common folder. It was moved to catalog/startup.
Also, it is highly recommended to run the filename in the require_once statement through opencart's modification function, like:
require_once modification(DIR_CATALOG . 'controller/startup/seo_url.php');

In wordpress I am manipulation 404 response. I want to give the response before 404 error is given to google

May be the question in title is not explaining what exactly I want. So I will explain one logic and then ask what problem I am getting from that.
We are using wordpress.
We have 2 categories Option1(Having 64 different values) and Option2(Having 8 different values). We wanted url like ourdomain/Option1/Option2 depending on users location. And data on that url will be specific according to that Option1 and Option2. We dont wanted all this 64*8 actual wordpress pages to be created.
So after searching much on net we had one solution that I implemented one plugin for routing.
IN that code snippet was as follow.
function site_router() {
global $route,$wp_query,$window_title;
error_reporting(E_ALL);
if ( $wp_query->is_404 )
{
$wp_query->is_404 = false;
$cUrl="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$targetUrl="";
switch($cUrl)
{
/* According to condition I had written diffent cases like below.
*/
default :
{
include(get_template_directory() . "/home.php" );
$template =locate_template('pagepath/home.php');
$window_title = 'dynamically it will come';
if ($template) {
load_template($template);
die;
}
}
}
}
}
add_action( 'wp', 'site_router');
So by this my purpose was fulfilled successfully.
But now problem is that google is saying they are getting back a 404 error. I think obviosly it will give as after it gives the error we are doing all template loading and all stuff.
So Can any body guide me how can I do this before that 404 response is given to google.
I posted the question on wordpress stackexchange and got the answer over there. the link is as below.
https://wordpress.stackexchange.com/questions/70345/in-wordpress-i-am-manipulation-404-response-i-want-to-give-the-response-before

Categories