Created
March 5, 2013 06:43
-
-
Save TCotton/5088476 to your computer and use it in GitHub Desktop.
A quick way to add a URL redirect feature to Wordpress categories
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace content; | |
/** | |
* Category_Add_Field | |
* | |
* redirect() method is called in the header.php file | |
* if the user specifies an external URL in the category field | |
* | |
* @package class for creating dynamic Wordpress metaboxes | |
* @author Andy Walpole | |
* @copyright A Walpole | |
* @version 2013 | |
* @access public | |
* @license the same as Wordpress | |
*/ | |
class Category_Add_Field { | |
/** | |
* Category_Add_Field::__construct() | |
*/ | |
public function __construct() { | |
add_action('category_add_form_fields', array(&$this, 'taxonomy_add_new_meta_field'), 10, 2); | |
add_action('category_edit_form_fields', array(&$this, 'taxonomy_edit_meta_field'), 10, 2); | |
add_action('edited_category', array(&$this, 'save_taxonomy_custom_meta'), 10, 2); | |
add_action('create_category', array(&$this, 'save_taxonomy_custom_meta'), 10, 2); | |
} | |
/** | |
* Category_Add_Field::taxonomy_add_new_meta_field() | |
* | |
* @return string | |
*/ | |
public function taxonomy_add_new_meta_field() { | |
$field = '<div class="form-field">'; | |
$field .= '<label for="term_meta[custom_term_meta_url]">'; | |
$field .= 'Enter a full URL here'; | |
$field .= '</label>'; | |
$field .= '<input type="text" name="term_meta[custom_term_meta_url]" id="term_meta[custom_term_meta_url]" value="">'; | |
$field .= '<p class="description">'; | |
$field .= 'Leave blank if no redirect required'; | |
$field .= '</p>'; | |
$field .= '</div>'; | |
echo $field; | |
} | |
/** | |
* Category_Add_Field::taxonomy_edit_meta_field() | |
* | |
* @param string $term | |
* @return string | |
*/ | |
public function taxonomy_edit_meta_field($term) { | |
// put the term ID into a variable | |
$t_id = $term->term_id; | |
// retrieve the existing value(s) for this meta field. This returns an array | |
$term_meta = get_option("taxonomy_$t_id"); | |
$field = '<tr class="form-field">'; | |
$field .= '<th scope="row" valign="top"><label for="term_meta[custom_term_meta_url]">'; | |
$field .= 'Enter a full URL here'; | |
$field .= '</label></th>'; | |
$field .= '<td>'; | |
$field .= '<input type="text" name="term_meta[custom_term_meta_url]" id="term_meta[custom_term_meta_url]" value="'; | |
$field .= esc_url($term_meta['custom_term_meta_url']) ? esc_url($term_meta['custom_term_meta_url']) : ''; | |
$field .= '">'; | |
$field .= '<br><span class="description">'; | |
$field .= 'Leave blank if no redirect required'; | |
$field .= '</span></td></tr>'; | |
echo $field; | |
} | |
/** | |
* Category_Add_Field::save_taxonomy_custom_meta() | |
* | |
* @param string $term_id | |
* @return | |
*/ | |
public function save_taxonomy_custom_meta($term_id) { | |
if (isset($_POST['term_meta'])) { | |
$t_id = $term_id; | |
$term_meta = get_option("taxonomy_$t_id"); | |
$cat_keys = array_keys($_POST['term_meta']); | |
foreach ($cat_keys as $key) { | |
if (isset($_POST['term_meta'][$key])) { | |
$term_meta[$key] = $_POST['term_meta'][$key]; | |
} | |
} | |
// Save the option array. | |
update_option("taxonomy_$t_id", $term_meta); | |
} | |
} | |
/** | |
* 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; | |
} | |
} | |
} // end class | |
new \content\Category_Add_Field; | |
// use this class by calling the redirect method at the top of the header file like so: | |
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 != '') { | |
\content\Category_Add_Field::redirect(get_query_var('cat')); | |
} | |
} else { | |
echo 'No class called Category_Add_Field has been found'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment