brasofilo

Workaround to use Custom Taxonomies in WordPress SEO titles

May 15th, 2012
878
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3.  * Workaround to use Custom Taxonomies in WordPress SEO titles
  4.  *
  5.  * @author: brasofilo
  6.  * @licence: GPL
  7.  *
  8.  * Drop this code in your theme's functions.php
  9.  *
  10.  * Instructions: adjust the function **brsfl_wpseo_taxonomy_title** with your custom values
  11.  *
  12.  * Usage: go to the page /wp-admin/admin.php?page=wpseo_titles
  13.  *        and set the tag of your CPT to %%cf__my_cat_title%%
  14.  *        ATTENTION to the double underscore after %%cf
  15.  *
  16.  * How it works: the Custom Field "_my_cat_title" is automatically populated with the current categories of the desired CPT
  17.  *               and the value of the field will be used for you custom SEO tag
  18.  *               ATTENTION: an underscore at the beginning of the Custom Field prevents it from showing up in the post editing screen
  19.  *
  20.  */
  21.  
  22. add_action ('save_post','brsfl_wpseo_taxonomy_title',10,2);
  23.  
  24. /*
  25.  * @desc Makes an automatic Custom Field with the current categories of a Custom Post Type
  26.  *
  27.  * ADJUST THIS 3 VALUES IN THE FUNCTION TO MATCH YOUR NEEDS
  28.  * @movie Name of the Custom Post Type
  29.  * @category Name of the Custom Taxonomy
  30.  * @_my_cat_title Name of the Custom Field
  31.  *
  32.  */
  33. function brsfl_wpseo_taxonomy_title($post_ID, $post) {
  34.     $type = get_post_type($post_ID);
  35.     if ( 'movie' != $type ) return;
  36.     $the_cats = brsfl_wpseo_get_terms($post_ID, 'category');
  37.     update_post_meta($post_ID, '_my_cat_title', $the_cats);
  38. }
  39.  
  40. /*
  41.  * @desc Grabs the categories associated with a post
  42.  *
  43.  * @returns Comma separated list of categories
  44.  *
  45.  */
  46. function brsfl_wpseo_get_terms($ID, $taxonomy) {
  47.     $terms = get_the_terms( $ID, $taxonomy);
  48.     $return = array();
  49.     foreach($terms as $t) {
  50.         $return[] = $t->name;
  51.     }
  52.     return join( ", ", $return );
  53. }
RAW Paste Data