My last job at a large multi-national company before my contract expired was to code their Russian website. A major problem that their Russian team faced was not having the resources to translate everything on the English language website or to be able to keep abreast of updates if the content on the English language website changed.
Therefore, the site architecture involved a number of redirects to the English language website. Admittedly, I didn't find this a particularly user-friendly approach for Russian readers but this decision had already been made before I became involved in the project.
A large section of the site was based on categories. A top parent category would divide down into several child categories which would then separate into more child categories. Some of these would redirect to the English language version of the website.
I started off by using John Godley's excellent Redirection plugin, but the problem was that the user interface was too complicated for the layperson to update and change. I needed to add a field to individual categories so that it was just a simple act for the maintainer of the website to cut and paste a URL into the category form.
My starting point was the code found on the article called Adding Custom Meta Fields to Taxonomies. I won't reproduce the code here because it is quite a faithful use of their PHP expect that I changed it into a class.
This is how the category interface looks:
Image may be NSFW.
Clik here to view.
The key method that I added was this:
/** * Category_Add_Field::redirect() * * @param string $id * @return redirect */ public function redirect($id) { $term_meta = get_option("taxonomy_" . $id); if ($term_meta != false && (isset($term_meta['custom_term_meta_url']) && $term_meta['custom_term_meta_url'] != '')) { ob_start(); wp_redirect($term_meta['custom_term_meta_url'], 302); ob_end_flush(); exit; } }
It's very simple. If the category term has a redirect URL in the postmeta table then call the wp_redirect() function.
The method is called in the very top of the header.php file:
if (class_exists('\content\Category_Add_Field')) { $cat = get_query_var('cat'); // category redirection if external URL is set in the category page if ($cat != '') { contentCategory_Add_Field::redirect(get_query_var('cat')); } } else { echo 'No class called Category_Add_Field has been found'; }
Yes, I know that the best method is to add these URL redirects to the .htaccess file, but time was against me. This is a quick way of creating a Wordpress category redirect feature.
You can find the full class on this GitGist