My Custom Permalink Variable Plugin
-
I developed a small plugin to be able to add a custom taxonomy as a string to a post. I’ve included the variable in a custom permalink string. This string is used by Apache to determine if external login is required. This part is working fine — the permalink is being changed, redirecting to Apache, and then causing the login to be prompted (it’s our institution’s flavor of PubCookie). I’ve tested other custom permalink structures, without variables, just to see that I had Permalinks set up correctly (mod_rewrite, etc.). So, in general, custom permalinks are functioning without 404 errors.
However, once I put that particular string into the permalink, I am getting a 404. Here is the code for the plugin.
//REGISTER THE TAXONOMY add_action('init','uvasom_restriction_init'); function uvasom_restriction_init() { if (!taxonomy_exists('uvasomrestriction')) { register_taxonomy( 'uvasomrestriction', 'post', array( 'hierarchical' => TRUE, 'label' => __('UVA SOM Restriction'), 'public' => TRUE, 'show_ui' => TRUE, 'query_var' => 'uvasomrestriction', 'rewrite' => true, 'capabilities'=>array( 'manage_terms' => 'manage_options',//or some other capability your clients don't have 'edit_terms' => 'manage_options', 'delete_terms' => 'manage_options', 'assign_terms' =>'edit_posts') ) ); } //ADD DEFAULT TERMS IF THEY ARE NOT THERE $parent_term = term_exists( 'uvasomrestriction', 'uvasomrestriction' ); // array is returned if taxonomy is given $parent_term_id = $parent_term['term_id']; // get numeric term id wp_insert_term( 'UVA Only', // the term 'uvasomrestriction', // the taxonomy array( 'description'=> 'Require UVA Netbadge credentials to read.', 'slug' => 'privatenbauth-uva-only', 'parent'=> $parent_term_id ) ); } //INSERT THE PROPER TERM INTO THE CUSTOM PERMALINK add_filter('post_link', 'uvasom_restriction_permalink', 10, 3); add_filter('post_type_link', 'uvasom_restriction_permalink', 10, 3); function uvasom_restriction_permalink($permalink, $post_id, $leavename) { if (strpos($permalink, '%uvasomrestriction%') === FALSE) return $permalink; // Get post $post = get_post($post_id); if (!$post) return $permalink; // Get taxonomy terms $terms = wp_get_object_terms($post->ID, 'uvasomrestriction'); if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0])) $taxonomy_slug = $terms[0]->slug; else $taxonomy_slug = 'unrestricted'; return str_replace('%uvasomrestriction%', $taxonomy_slug, $permalink); }
Debug is showing nothing. Here is the URL. Click on any post on the home page and you will get a 404:
https://news.med.virginia.edu/deansoffice/
I’m sure it’s something obvious to a more trained eye. Please let me know, and thanks.
- The topic ‘My Custom Permalink Variable Plugin’ is closed to new replies.