Quick WP Tip: edit_term_link()

I’ve started optimizing tags, categories and custom taxonomies a lot more lately now that I can do all these neat things with them in my WordPress SEO plugin, and since I’m a busy guy I want to be able to do that quickly. So I wanted to edit my templates for those taxonomies to include an edit link. Looking through the WordPress source I could only find an edit_tag_link function, and I wanted it to work on custom taxonomies too. Turns out there’s nothing in core for that just yet.

Because I have to stay true to my own mentality expressed in another post: if you can fix it, do it, and submit a patch. So I made an edit_term_link function, created a patch and attached that to an already existing issue 9702 in the WordPress Trac that asked for something like that. We’ll see if it makes into core, I thought you might find it cool. My patch just got committed into core, so in WordPress 3.1, you’ll have edit_term_link and get_edit_term_link!

To be sure your site doesn’t die when you upgrade, I wrapped it in a if function_exists wrapper. You can just copy the code into your theme’s functions.php file, and then call edit_term_link() in your category, tag & taxonomy pages. Here’s the code:

if ( !function_exists('edit_term_link') ) {
  function edit_term_link( $link = '', $before = '', 
      $after = '', $term = null ) {
  
    if ( $term == null ) {
      global $wp_query;
      $term = $wp_query->get_queried_object();
    }
  
    $tax = get_taxonomy( $term->taxonomy );
    if ( !current_user_can($tax->cap->edit_terms) )
      return;

    if ( empty($link) )
      $link = __('Edit This');

    $link = '<a href="' . get_edit_tag_link( $term->term_id, 
        $term->taxonomy ) . '" title="' . $link . '">' . $link . '</a>';
    echo $before . 
       apply_filters( 'edit_term_link', $link, $term->term_id ) . $after;
  }
}

Yoast.com runs on the Genesis Framework

Genesis theme frameworkThe Genesis Framework empowers you to quickly and easily build incredible websites with WordPress. Whether you're a novice or advanced developer, Genesis provides you with the secure and search-engine-optimized foundation that takes WordPress to places you never thought it could go.

Read our Genesis review or get Genesis now!

9 Responses

  1. Derek JonesBy Derek Jones on 14 October, 2010

    I love the tech detail, but for new WP users and future coders like myself, could you include the answer to why this is important or how I use it?

    Thanks, it would help me learn WP.

    Derek

    • Joost de ValkBy Joost de Valk on 14 October, 2010

      Simply add the following code on any category, tag or taxonomy template:

      <?php edit_term_link(); ?>

      And if you’re logged in you’ll get an “Edit this” link, that takes you straight to that category, tags or taxonomy’s edit page :)

  2. davidBy david on 15 October, 2010

    Awesome! this function exists with Drupal about time it existed with wordpress!

    Good work Joost!

  3. maxiBy maxi on 15 October, 2010

    Awesome ! coolll

  4. Peer HundigeBy Peer Hundige on 17 October, 2010

    I agree – nice work Joost! I’ve just started to work with WP (I’ve been a Joomla fan for ages :-)) You are a great ressource – thanks!

    Best regards,
    Peer

  5. Med NBy Med N on 17 October, 2010

    I love the Tip, very usefull

  6. Denver Painting ContractorBy Denver Painting Contractor on 18 October, 2010

    Very nice! Thanks building code to work on custom taxonomies.

  7. Bill BennettBy Bill Bennett on 20 October, 2010

    Great stuff Joost. Can you explain WHY you would want to optimise categories and tags? Also, by optimise do you mean simplify?

Trackbacks