root/bunny-tags/branches/wp-2.0/bunny-tags.php

Revision 4385, 8.5 kB (checked in by steph, 3 years ago)

Version 0.5 works with WordPress 2.0 and Pages and allows multiple word tags.

Line 
1 <?php
2 /*
3 Plugin Name: Bunny's Technorati Tags
4 Plugin URI: http://dev.wp-plugins.org/wiki/BunnysTechnoratiTags
5 Description: Allows easy addition to a post of a space-separated list of tags which can be displayed with adequate Technorati links in the template. Can display keywords instead if no tags are available.
6 Version: 0.5
7 Author: Stephanie Booth
8 Author URI: http://climbtothestars.org/
9
10
11   Copyright 2005, 2006  Stephanie Booth  (email : steph@climbtothestars.org)
12
13     This program is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; either version 2 of the License, or
16     (at your option) any later version.
17
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21     GNU General Public License for more details.
22
23     You should have received a copy of the GNU General Public License
24     along with this program; if not, write to the Free Software
25     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26
27 INSTRUCTIONS
28 ============
29
30 When editing a post, add a space-separated list of tags in the field below the textarea. You can now use tags with spaces by replacing the space by "+" -- e.g.: lake geneva lake+geneva beautiful switzerland
31
32 Add the following code to your template where you want your tags to appear:
33
34 <?php the_bunny_tags(); ?>             
35
36 The keywords will appear as links wrapped in <p class="tags">Tags: ...</p>. You may want to add CSS to style the list.
37
38 If you want other html, you can try things like the following (thanks, Scott):
39
40 <?php the_bunny_tags("<ul><li>", "</li></ul>", "</li><li>"); ?>
41                         
42 If you want keywords to be translated as tags for posts which do not have tags defined, change the setting below. Attention! your keyword list must be stored in a custom field 'keywords' and be a comma-separated list -- otherwise you will need to edit the
43
44  function get_bunny_tags_list().
45
46 WARNING: if your pages aren't utf-8 and you use non-ascii characters in your tags, your tags might not get indexed properly.
47
48 SETTINGS
49 ========
50
51 If you want the plugin to try and use a comma-separated "keywords" post_meta field if there is no space-separated "tags" field available, replace "true" below by "false": */
52
53 $bunny_strict=true;
54
55 /* If you want to use something else than the Technorati tagspace, just edit the plugin to replace all links to technorati.com by whatever you want.
56
57 If you want to change the separator to something else than a space, just edit the call to the function get_bunny_tags() in get_bunny_tags_list()
58
59
60 CHANGELOG
61 =========
62
63 0.1  - Initial release 18.01.2005
64 0.2  - Added "tags" textarea to post.php
65      - Modified the_bunny_keyword_tags(): uses 'tags' if available
66 0.21 - Added missing "Save Post" action hook (tags weren't saved on drafts)     
67 0.3  - Include tags in post content in feeds for better parsing by Technorati
68      - Replaced the_bunny_keyword_tags() by setting $bunny_strict=false
69 0.31 - Now encodes accented characters in tag URIs (accented tags were breaking Kevin's parser!)
70 0.32 - Minor tweaks to url (correct encoding function, removed trailing slash) which should *finally* mean the tags will get indexed correctly. See warning above, however.
71 0.4  - Courtesy of Scott Jarkoff, you can now wrap the list of tags in any HTML you want.
72 0.41 - Fixed bug messing with the feeds.
73 0.42 - Code tweaks, no change in functionality.
74 0.43 - Code tweaks 28.01.2005
75 0.5  - Now functions with Wordpress 2.0 (03.01.2006)
76      - Added fix for spaces in tags (use "+")
77      - Added support for Pages
78
79 CREDITS
80 =======
81
82 Many thanks in particular to Carthik ( http://blog.carthik.net/ ), who gave me the necessary nudge to start coding this plugin, and to Morgan ( http://doocy.net/ ), who helped me write my first action hooks, and who pointed me to his plugin MiniPosts; you'll recognize some of the code if you compare both plugins ;-)
83
84 Thanks also to Kevin Marks ( http://epeus.blogspot.com/ ) for encouraging me to play about with all the Technorati stuff and helping me iron out some tag url bugs.
85
86 Scott Jarkoff ( http://www.jarkolicious.com/ ) made the modification which allows passing HTML formatting of the list in the_bunny_tags($before, $after, $separator). Thanks!
87
88 <? this line just makes things pretty in my BBEdit
89 */
90
91
92 // retrieve tag values from post_meta
93 function get_bunny_tags($separator=" ", $meta_field)
94 {   
95     $tags = get_post_custom_values($meta_field);
96     if(!empty($tags[0]))
97     {
98         $tags_array = explode($separator, $tags[0]);
99     }
100     return($tags_array); // return an array of tags
101 }
102
103
104 // prepare formatted tag list from $tags_array
105 function output_bunny_tags($tags_array, $before, $after, $separator)
106 {   
107     if(!empty($tags_array))
108     {
109         $tags_list=$before; // HTML to display before the list of tags
110         foreach($tags_array as $tag) // go through all tags for the post
111         {
112             // no spaces in tags version
113             // $tag_link='<a href="http://technorati.com/tag/' . rawurlencode($tag) . '" title="' . __('See the Technorati tag page for', 'BunnyTags') . ' \'' . $tag . '\'." rel="tag">' . $tag . '</a>' . $separator; // make a link to the technorati tag page, with tag link text
114
115             // multiple word tags fix provided by ClaytonGray
116             $display_tag = urldecode($tag);
117             $display_tag = str_replace("+", " ", $display_tag);
118             $tag_link='<a href="http://technorati.com/tag/' . urlencode(urldecode($tag)) . '" title="' . __('See the Technorati tag page for', 'BunnyTags') . ' \'' . urldecode($tag) . '\'." rel="tag">' . $display_tag . '</a>' . $separator; // make a link to the technorati tag page, with tag link text
119
120             $tags_list.=$tag_link; // stick it on the end of the growing list
121         }
122         $chomp = 0 - strlen($separator);
123         $tags_list=substr($tags_list, 0, $chomp);// remove the last separator
124         $tags_list.=$after; // HTML to display after the list of tags
125     }
126     return($tags_list);  // return the nice little html-ized list of technorati tags
127 }
128
129 // prepare the formatted list of tags -- this is what you mess with if your custom field values aren't formatted like described in the instructions
130 function get_bunny_tags_list($before, $after, $separator)
131 {
132     global $bunny_strict;
133     
134     $technorati_tags=get_bunny_tags(' ', 'tags');
135     $label="Tags";
136     if(empty($technorati_tags)&&!$bunny_strict)
137     {
138         $technorati_tags=get_bunny_tags(', ', 'keywords');
139         $label="Keywords";
140     }
141     $technorati_tags_list=output_bunny_tags($technorati_tags, $before, $after, $separator);
142     return($technorati_tags_list);
143 }
144
145
146 // stick the list of tags at the end of the_content if we're in a feed
147 function append_tags($content)
148 {
149     global $feed;
150     if (!empty($feed)&&!is_single())
151     {
152         $tags_list=get_bunny_tags_list('<p class="tags">Tags: ', '</p>', ', ');
153         $content.=$tags_list;
154     }
155     return $content;   
156 }
157
158 // output textarea to easily add tags in admin menu (addition to the post form)
159 function add_tags_textinput() {
160     global $post;
161     
162     $tags = get_post_meta($post->ID, 'tags', true);
163     
164     echo '<fieldset id="posttags"><legend><a href="http://technorati.com/help/tags.html" title="Information about Technorati tags.">' . __('Tags', 'BunnyTags') . '</a></legend>';
165     echo '<div><input type="text" name="tags" id="tags" size="95" value="' . $tags . '" /><br />' . __('Separate tags with spaces', 'BunnyTags') . '</div></fieldset>';
166 }
167
168 // general custom field update function
169 function bt_update_tags($id)
170 {
171     $setting = $_POST['tags'];
172     $meta_exists=update_post_meta($id, 'tags', $setting);
173     if(!$meta_exists)
174     {
175         add_post_meta($id, 'tags', $setting);   
176     }
177 }
178
179 // start serious stuff
180 // localization disabled because it breaks in 1.2
181 // load_plugin_textdomain('BunnyTags');
182
183 // ACTION!
184
185 add_action('simple_edit_form', 'add_tags_textinput');
186 add_action('edit_form_advanced', 'add_tags_textinput');
187 add_action('edit_page_form', 'add_tags_textinput');
188 add_action('edit_post', 'bt_update_tags');
189 add_action('publish_post', 'bt_update_tags');
190 add_action('save_post', 'bt_update_tags');
191
192 add_filter('the_content', 'append_tags');
193
194 // TEMPLATE FUNCTION
195
196 // use this function to output as tags the space-separated content of the meta field "tags"; depending on your setting of $strict at the beginning of this file, the function may treat as tags the comma-separated content of the meta field "keywords" if "tags" doesn't exist. You may pass arguments $before, $after, and $separator to control the formatting.
197
198 function the_bunny_tags($before = '<p class="tags">Tags: ', $after = '</p>', $separator = ', ')
199 {
200     print(get_bunny_tags_list($before, $after, $separator));
201 }
202
203 ?>
204
Note: See TracBrowser for help on using the browser.