Created
January 29, 2013 12:12
-
-
Save franz-josef-kaiser/4663802 to your computer and use it in GitHub Desktop.
WordPress plugin that alters the (translated) title attribute for `wp_list_categories()` list items on the fly using the `gettext` or `category_description` filter.
This file contains hidden or 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 | |
| add_action( 'plugins_loaded', array( 'WCM_list_cat_filter', 'init' ), 5 ); | |
| /** | |
| * @package WCM Filterama | |
| * @subpackage Bootstrap | |
| */ | |
| class WCM_list_cat_filter | |
| { | |
| private static $instance; | |
| public $args; | |
| static function init() | |
| { | |
| null === self :: $instance AND self :: $instance = new self; | |
| return self :: $instance; | |
| } | |
| public function __construct() | |
| { | |
| add_filter( 'get_categories_taxonomy', array( $this, 'get_args' ), 10, 2 ); | |
| } | |
| /** | |
| * Attached from inside get_categories() | |
| * @param string $taxonomy | |
| * @param array $args | |
| * @return string | |
| */ | |
| public function get_args( $taxonomy, $args ) | |
| { | |
| $this->args = $args; | |
| add_filter( 'list_cats', array( $this, 'route_filters' ), 10, 2 ); | |
| return $taxonomy; | |
| } | |
| /** | |
| * Attached from inside walk_category_tree() | |
| * Intercepted inside Walker_Category::start_el() | |
| */ | |
| public function route_filters( $cat_name, $cat ) | |
| { | |
| if ( | |
| 0 === $this->args['use_desc_for_title'] | |
| OR empty( $cat_name ) | |
| ) | |
| { | |
| add_filter( 'gettext', array( $this, 'alter_title_attr' ), 10, 2 ); | |
| } | |
| else | |
| { | |
| add_filter( 'category_description', array( $this, 'alter_cat_attr' ), 10, 2 ); | |
| } | |
| return $cat_name; | |
| } | |
| public function alter_title_attr( $text, $domain ) | |
| { | |
| remove_filter( current_filter(), array( $this, __FUNCTION__ ) ); | |
| return $text = 'YOUR STRING HERE'; | |
| } | |
| public function alter_cat_attr( $cat_desc, $cat ) | |
| { | |
| return $cat_desc = 'YOUR STRING HERE'; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment