/** * * Fix taxonomy link outputs. * * @since 0.6 * @version 1.0 * * @param string $termlink * @param Object $term * @param Object $taxonomy * * @return string */ public function term_link($termlink, $term, $taxonomy) { /** @var WP_Rewrite $wp_rewrite */ global $wp_rewrite; if (!$wp_rewrite->permalink_structure) { return $termlink; } if (CPTP_Util::get_no_taxonomy_structure()) { return $termlink; } $taxonomy = get_taxonomy($taxonomy); if ($taxonomy->_builtin) { return $termlink; } if (empty($taxonomy)) { return $termlink; } $wp_home = rtrim(home_url(), '/'); if (in_array(get_post_type(), $taxonomy->object_type)) { $post_type = get_post_type(); } else { $post_type = $taxonomy->object_type[0]; } $front = substr($wp_rewrite->front, 1); $termlink = str_replace($front, '', $termlink); // remove front. $post_type_obj = get_post_type_object($post_type); if (empty($post_type_obj)) { return $termlink; } $slug = $post_type_obj->rewrite['slug']; $with_front = $post_type_obj->rewrite['with_front']; if ($with_front) { $slug = $front . $slug; } if (!empty($slug)) { $termlink = str_replace($wp_home, $wp_home . '/' . $slug, $termlink); } if (!$taxonomy->rewrite['hierarchical']) { $termlink = str_replace($term->slug . '/', CPTP_Util::get_taxonomy_parents_slug($term->term_id, $taxonomy->name, '/', true), $termlink); } return $termlink; }
/** * * register_taxonomy_rules * * @param string $taxonomy * @param array|string $object_type * @param array|WP_Taxonomy $args * * @return void */ public function register_taxonomy_rules($taxonomy, $object_type, $args) { global $wp_rewrite; /* for 4.7 */ $args = (array) $args; if (CPTP_Util::get_no_taxonomy_structure()) { return; } if (!empty($args['_builtin'])) { return; } if (false === $args['rewrite']) { return; } $post_types = $args['object_type']; foreach ($post_types as $post_type) { $post_type_obj = get_post_type_object($post_type); if (!empty($post_type_obj->rewrite['slug'])) { $slug = $post_type_obj->rewrite['slug']; } else { $slug = $post_type; } if (!empty($post_type_obj->has_archive) && is_string($post_type_obj->has_archive)) { $slug = $post_type_obj->has_archive; } if (!empty($post_type_obj->rewrite['with_front'])) { $slug = substr($wp_rewrite->front, 1) . $slug; } if ('category' == $taxonomy) { $taxonomy_slug = ($cb = get_option('category_base')) ? $cb : $taxonomy; $taxonomy_key = 'category_name'; } else { // Edit by [Xiphe] if (isset($args['rewrite']['slug'])) { $taxonomy_slug = $args['rewrite']['slug']; } else { $taxonomy_slug = $taxonomy; } // [Xiphe] stop $taxonomy_key = $taxonomy; } $rules = array(array('regex' => '%s/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&feed=\$matches[2]"), array('regex' => '%s/(.+?)/(feed|rdf|rss|rss2|atom)/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&feed=\$matches[2]"), array('regex' => '%s/(.+?)/date/([0-9]{4})/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]"), array('regex' => '%s/(.+?)/date/([0-9]{4})/page/?([0-9]{1,})/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&paged=\$matches[3]"), array('regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]"), array('regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&paged=\$matches[4]"), array('regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&day=\$matches[4]"), array('regex' => '%s/(.+?)/date/([0-9]{4})/([0-9]{1,2})/([0-9]{1,2})/page/?([0-9]{1,})/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&year=\$matches[2]&monthnum=\$matches[3]&day=\$matches[4]&paged=\$matches[5]"), array('regex' => '%s/(.+?)/page/?([0-9]{1,})/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]&paged=\$matches[2]"), array('regex' => '%s/(.+?)/?$', 'redirect' => "index.php?{$taxonomy_key}=\$matches[1]")); // no post_type slug. foreach ($rules as $rule) { $regex = sprintf($rule['regex'], "{$taxonomy_slug}"); $redirect = $rule['redirect']; add_rewrite_rule($regex, $redirect, 'top'); } if (get_option('add_post_type_for_tax')) { foreach ($rules as $rule) { $regex = sprintf($rule['regex'], "{$slug}/{$taxonomy_slug}"); $redirect = $rule['redirect'] . "&post_type={$post_type}"; add_rewrite_rule($regex, $redirect, 'top'); } } else { foreach ($rules as $rule) { $regex = sprintf($rule['regex'], "{$slug}/{$taxonomy_slug}"); $redirect = $rule['redirect']; add_rewrite_rule($regex, $redirect, 'top'); } } do_action('CPTP_registered_' . $taxonomy . '_rules', $object_type, $args, $taxonomy_slug); } }
public function setting_no_tax_structure_callback_function() { $no_taxonomy_structure = CPTP_Util::get_no_taxonomy_structure(); echo '<input name="no_taxonomy_structure" id="no_taxonomy_structure" type="checkbox" value="1" class="code" ' . checked(false, $no_taxonomy_structure, false) . ' /> '; $txt = __("If you check this, the custom taxonomy's permalinks will be <code>%s/post_type/taxonomy/term</code>.", 'custom-post-type-permalinks'); echo sprintf(wp_kses($txt, array('code' => array())), esc_html(home_url())); }