0

Built this little function to give me a different method of rendering custom taxonomy archive links. But for whatever reason it's removing the underscores from the output. E.g., a $taxonomy with the name resource_type becomes resourcetype. How can I prevent this?

function my_get_term_link( $term, $taxonomy = '' ) {
    global $wp_rewrite;

    if ( !is_object($term) ) {
        if ( is_int( $term ) ) {
            $term = get_term( $term, $taxonomy );
        } else {
            $term = get_term_by( 'slug', $term, $taxonomy );
        }
    }

    if ( !is_object($term) )
        $term = new WP_Error('invalid_term', __('Empty Term'));

    if ( is_wp_error( $term ) )
        return $term;

    $taxonomy = $term->taxonomy;

    $slug = $term->slug;

    $termlink = "resources/?fwp_$taxonomy=$slug";
    $termlink = home_url($termlink);

    return apply_filters( 'term_link', $termlink, $term, $taxonomy );
}
2
  • This is not something inherent to WordPress. Have you enabled debugging? Have you disabled all your plugins? Have you switched to the default theme?
    – Howdy_McGee
    Jan 12, 2017 at 23:00
  • Tried disabling plugins, same result. Tried debug mode, no errors. But here's something interesting: if I try the same function in the default Twenty Fifteen and Twenty Sixteen themes, it changes the underscores to hyphens. So something, somewhere is altering the link. Jan 13, 2017 at 16:25

1 Answer 1

-1

WordPress sees underscore names as relating to post types and dashes as relating to taxonomies. Perhaps this has something to do with it? Also, have you flushed your permalinks recently (change the permalink structure to something else and then back)? It might be stuck in your database with the "sanitized" taxonomy name. This has happened to me numerous times with both taxonomies and post types.

2
  • WordPress sees underscore names as relating to post types and dashes as relating to taxonomies. Do you have any reference for this? I've never had an issue creating a taxonomy with an underscore.
    – Howdy_McGee
    Jan 12, 2017 at 22:59
  • I have tried flushing the permalinks to no avail. I haven't head of underscores and dashes being associated with post types and taxonomies, respectively. Which seems odd, because get_term_link() in WP core works with underscores. Jan 13, 2017 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.