3 * Bookmark Template Functions for usage in Themes
10 * The formatted output of a list of bookmarks.
12 * The $bookmarks array must contain bookmark objects and will be iterated over
13 * to retrieve the bookmark to be used in the output.
15 * The output is formatted as HTML with no way to change that format. However,
16 * what is between, before, and after can be changed. The link itself will be
19 * This function is used internally by wp_list_bookmarks() and should not be
22 * The defaults for overwriting are:
23 * 'show_updated' - Default is 0 (integer). Will show the time of when the
24 * bookmark was last updated.
25 * 'show_description' - Default is 0 (integer). Whether to show the description
27 * 'show_images' - Default is 1 (integer). Whether to show link image if
29 * 'show_name' - Default is 0 (integer). Whether to show link name if
31 * 'before' - Default is '<li>' (string). The html or text to prepend to each
33 * 'after' - Default is '</li>' (string). The html or text to append to each
35 * 'link_before' - Default is '' (string). The html or text to prepend to each
36 * bookmarks inside the <a> tag.
37 * 'link_after' - Default is '' (string). The html or text to append to each
38 * bookmarks inside the <a> tag.
39 * 'between' - Default is '\n' (string). The string for use in between the link,
40 * description, and image.
41 * 'show_rating' - Default is 0 (integer). Whether to show the link rating.
46 * @param array $bookmarks List of bookmarks to traverse
47 * @param string|array $args Optional. Overwrite the defaults.
48 * @return string Formatted output in HTML
50 function _walk_bookmarks($bookmarks, $args = '' ) {
52 'show_updated' => 0, 'show_description' => 0,
53 'show_images' => 1, 'show_name' => 0,
54 'before' => '<li>', 'after' => '</li>', 'between' => "\n",
55 'show_rating' => 0, 'link_before' => '', 'link_after' => ''
58 $r = wp_parse_args( $args, $defaults );
59 extract( $r, EXTR_SKIP );
61 $output = ''; // Blank string to start with.
63 foreach ( (array) $bookmarks as $bookmark ) {
64 if ( !isset($bookmark->recently_updated) )
65 $bookmark->recently_updated = false;
67 if ( $show_updated && $bookmark->recently_updated )
68 $output .= get_option('links_recently_updated_prepend');
71 if ( !empty($bookmark->link_url) )
72 $the_link = esc_url($bookmark->link_url);
74 $desc = esc_attr(sanitize_bookmark_field('link_description', $bookmark->link_description, $bookmark->link_id, 'display'));
75 $name = esc_attr(sanitize_bookmark_field('link_name', $bookmark->link_name, $bookmark->link_id, 'display'));
79 if ( '00' != substr($bookmark->link_updated_f, 0, 2) ) {
81 $title .= sprintf(__('Last updated: %s'), date(get_option('links_updated_date_format'), $bookmark->link_updated_f + (get_option('gmt_offset') * HOUR_IN_SECONDS)));
85 $alt = ' alt="' . $name . ( $show_description ? ' ' . $title : '' ) . '"';
88 $title = ' title="' . $title . '"';
90 $rel = $bookmark->link_rel;
92 $rel = ' rel="' . esc_attr($rel) . '"';
94 $target = $bookmark->link_target;
96 $target = ' target="' . $target . '"';
98 $output .= '<a href="' . $the_link . '"' . $rel . $title . $target . '>';
100 $output .= $link_before;
102 if ( $bookmark->link_image != null && $show_images ) {
103 if ( strpos($bookmark->link_image, 'http') === 0 )
104 $output .= "<img src=\"$bookmark->link_image\" $alt $title />";
105 else // If it's a relative path
106 $output .= "<img src=\"" . get_option('siteurl') . "$bookmark->link_image\" $alt $title />";
114 $output .= $link_after;
118 if ( $show_updated && $bookmark->recently_updated )
119 $output .= get_option('links_recently_updated_append');
121 if ( $show_description && '' != $desc )
122 $output .= $between . $desc;
125 $output .= $between . sanitize_bookmark_field('link_rating', $bookmark->link_rating, $bookmark->link_id, 'display');
127 $output .= "$after\n";
134 * Retrieve or echo all of the bookmarks.
136 * List of default arguments are as follows:
137 * 'orderby' - Default is 'name' (string). How to order the links by. String is
138 * based off of the bookmark scheme.
139 * 'order' - Default is 'ASC' (string). Either 'ASC' or 'DESC'. Orders in either
140 * ascending or descending order.
141 * 'limit' - Default is -1 (integer) or show all. The amount of bookmarks to
143 * 'category' - Default is empty string (string). Include the links in what
145 * 'category_name' - Default is empty string (string). Get links by category
147 * 'hide_invisible' - Default is 1 (integer). Whether to show (default) or hide
148 * links marked as 'invisible'.
149 * 'show_updated' - Default is 0 (integer). Will show the time of when the
150 * bookmark was last updated.
151 * 'echo' - Default is 1 (integer). Whether to echo (default) or return the
152 * formatted bookmarks.
153 * 'categorize' - Default is 1 (integer). Whether to show links listed by
154 * category (default) or show links in one column.
155 * 'show_description' - Default is 0 (integer). Whether to show the description
158 * These options define how the Category name will appear before the category
159 * links are displayed, if 'categorize' is 1. If 'categorize' is 0, then it will
160 * display for only the 'title_li' string and only if 'title_li' is not empty.
161 * 'title_li' - Default is 'Bookmarks' (translatable string). What to show
162 * before the links appear.
163 * 'title_before' - Default is '<h2>' (string). The HTML or text to show before
164 * the 'title_li' string.
165 * 'title_after' - Default is '</h2>' (string). The HTML or text to show after
166 * the 'title_li' string.
167 * 'class' - Default is 'linkcat' (string). The CSS class to use for the
170 * 'category_before' - Default is '<li id="%id" class="%class">'. String must
171 * contain '%id' and '%class' to get
172 * the id of the category and the 'class' argument. These are used for
173 * formatting in themes.
174 * Argument will be displayed before the 'title_before' argument.
175 * 'category_after' - Default is '</li>' (string). The HTML or text that will
176 * appear after the list of links.
178 * These are only used if 'categorize' is set to 1 or true.
179 * 'category_orderby' - Default is 'name'. How to order the bookmark category
180 * based on term scheme.
181 * 'category_order' - Default is 'ASC'. Set the order by either ASC (ascending)
182 * or DESC (descending).
184 * @see _walk_bookmarks() For other arguments that can be set in this function
185 * and passed to _walk_bookmarks().
186 * @see get_bookmarks() For other arguments that can be set in this function and
187 * passed to get_bookmarks().
188 * @link http://codex.wordpress.org/Template_Tags/wp_list_bookmarks
191 * @uses _walk_bookmarks() Used to iterate over all of the bookmarks and return
193 * @uses get_terms() Gets all of the categories that are for links.
195 * @param string|array $args Optional. Overwrite the defaults of the function
196 * @return string|null Will only return if echo option is set to not echo.
197 * Default is not return anything.
199 function wp_list_bookmarks($args = '') {
201 'orderby' => 'name', 'order' => 'ASC',
202 'limit' => -1, 'category' => '', 'exclude_category' => '',
203 'category_name' => '', 'hide_invisible' => 1,
204 'show_updated' => 0, 'echo' => 1,
205 'categorize' => 1, 'title_li' => __('Bookmarks'),
206 'title_before' => '<h2>', 'title_after' => '</h2>',
207 'category_orderby' => 'name', 'category_order' => 'ASC',
208 'class' => 'linkcat', 'category_before' => '<li id="%id" class="%class">',
209 'category_after' => '</li>'
212 $r = wp_parse_args( $args, $defaults );
213 extract( $r, EXTR_SKIP );
218 $cats = get_terms( 'link_category', array( 'name__like' => $category_name, 'include' => $category, 'exclude' => $exclude_category, 'orderby' => $category_orderby, 'order' => $category_order, 'hierarchical' => 0 ) );
219 if ( empty( $cats ) )
224 // Split the bookmarks into ul's for each category
225 foreach ( (array) $cats as $cat ) {
226 $params = array_merge($r, array('category'=>$cat->term_id));
227 $bookmarks = get_bookmarks($params);
228 if ( empty($bookmarks) )
230 $output .= str_replace(array('%id', '%class'), array("linkcat-$cat->term_id", $class), $category_before);
232 * Filter the bookmarks category name.
236 * @param string $cat->name The category name of bookmarks.
238 $catname = apply_filters( 'link_category', $cat->name );
240 $output .= "$title_before$catname$title_after\n\t<ul class='xoxo blogroll'>\n";
241 $output .= _walk_bookmarks($bookmarks, $r);
242 $output .= "\n\t</ul>\n$category_after\n";
245 //output one single list using title_li for the title
246 $bookmarks = get_bookmarks($r);
248 if ( !empty($bookmarks) ) {
249 if ( !empty( $title_li ) ){
250 $output .= str_replace(array('%id', '%class'), array("linkcat-$category", $class), $category_before);
251 $output .= "$title_before$title_li$title_after\n\t<ul class='xoxo blogroll'>\n";
252 $output .= _walk_bookmarks($bookmarks, $r);
253 $output .= "\n\t</ul>\n$category_after\n";
255 $output .= _walk_bookmarks($bookmarks, $r);
261 * Filter the bookmarks list before it is echoed or returned.
265 * @param string $output The HTML list of bookmarks.
267 $output = apply_filters( 'wp_list_bookmarks', $output );