root/trunk/wp-includes/general-template.php

Revision 1530, 61.3 kB (checked in by donncha, 2 hours ago)

WP Merge with revision 9808

Line 
1 <?php
2 /**
3  * General template tags that can go anywhere in a template.
4  *
5  * @package WordPress
6  * @subpackage Template
7  */
8
9 /**
10  * Load header template.
11  *
12  * Includes the header template for a theme or if a name is specified then a
13  * specialised header will be included. If the theme contains no header.php file
14  * then the header from the default theme will be included.
15  *
16  * For the parameter, if the file is called "header-special.php" then specify
17  * "special".
18  *
19  * @uses locate_template()
20  * @since 1.5.0
21  * @uses do_action() Calls 'get_header' action.
22  *
23  * @param string $name The name of the specialised header.
24  */
25 function get_header( $name = null ) {
26     do_action( 'get_header' );
27
28     $templates = array();
29     if ( isset($name) )
30         $templates[] = "header-{$name}.php";
31
32     $templates[] = "header.php";
33
34     if ('' == locate_template($templates, true))
35         load_template( get_theme_root() . '/default/header.php');
36 }
37
38 /**
39  * Load footer template.
40  *
41  * Includes the footer template for a theme or if a name is specified then a
42  * specialised footer will be included. If the theme contains no footer.php file
43  * then the footer from the default theme will be included.
44  *
45  * For the parameter, if the file is called "footer-special.php" then specify
46  * "special".
47  *
48  * @uses locate_template()
49  * @since 1.5.0
50  * @uses do_action() Calls 'get_footer' action.
51  *
52  * @param string $name The name of the specialised footer.
53  */
54 function get_footer( $name = null ) {
55     do_action( 'get_footer' );
56
57     $templates = array();
58     if ( isset($name) )
59         $templates[] = "footer-{$name}.php";
60
61     $templates[] = "footer.php";
62
63     if ('' == locate_template($templates, true))
64         load_template( get_theme_root() . '/default/footer.php');
65 }
66
67 /**
68  * Load sidebar template.
69  *
70  * Includes the sidebar template for a theme or if a name is specified then a
71  * specialised sidebar will be included. If the theme contains no sidebar.php
72  * file then the sidebar from the default theme will be included.
73  *
74  * For the parameter, if the file is called "sidebar-special.php" then specify
75  * "special".
76  *
77  * @uses locate_template()
78  * @since 1.5.0
79  * @uses do_action() Calls 'get_sidebar' action.
80  *
81  * @param string $name The name of the specialised sidebar.
82  */
83 function get_sidebar( $name = null ) {
84     do_action( 'get_sidebar' );
85
86     $templates = array();
87     if ( isset($name) )
88         $templates[] = "sidebar-{$name}.php";
89
90     $templates[] = "sidebar.php";
91
92     if ('' == locate_template($templates, true))
93         load_template( get_theme_root() . '/default/sidebar.php');
94 }
95
96 /**
97  * Display search form.
98  *
99  * Will first attempt to locate the searchform.php file in either the child or
100  * the parent, then load it. If it doesn't exist, then the default search form
101  * will be displayed.
102  *
103  * @since 2.7.0
104  */
105 function get_search_form() {
106     do_action( 'get_search_form' );
107
108     if ( '' != locate_template(array('searchform.php'), true) )
109         return;
110
111     $form = '<form method="get" id="searchform" action="' . get_option('siteurl') . '/" >
112     <label class="hidden" for="s">' . __('Search for:') . '</label>
113     <div><input type="text" value="' . the_search_query() . '" name="s" id="s" />
114     <input type="submit" id="searchsubmit" value="Search" />
115     </div>
116     </form>';
117
118     echo apply_filters('get_search_form', $form);
119 }
120
121 /**
122  * Display the Log In/Out link.
123  *
124  * Displays a link, which allows the user to navigate to the Log In page to log in
125  * or log out depending on whether or not they are currently logged in.
126  *
127  * @since 1.5.0
128  * @uses apply_filters() Calls 'loginout' hook on HTML link content.
129  */
130 function wp_loginout() {
131     if ( ! is_user_logged_in() )
132         $link = '<a href="' . wp_login_url() . '">' . __('Log in') . '</a>';
133     else
134         $link = '<a href="' . wp_logout_url() . '">' . __('Log out') . '</a>';
135
136     echo apply_filters('loginout', $link);
137 }
138
139 /**
140  * Returns the Log Out URL.
141  *
142  * Returns the URL that allows the user to log out of the site
143  *
144  * @since 2.7
145  * @uses wp_nonce_url() To protect against CSRF
146  * @uses site_url() To generate the log in URL
147  *
148  * @param string $redirect Path to redirect to on logout.
149  */
150 function wp_logout_url($redirect = '') {
151     if ( strlen($redirect) )
152         $redirect = "&redirect_to=$redirect";
153     
154     return wp_nonce_url( site_url("wp-login.php?action=logout$redirect", 'login'), 'log-out' );
155 }
156
157 /**
158  * Returns the Log In URL.
159  *
160  * Returns the URL that allows the user to log in to the site
161  *
162  * @since 2.7
163  * @uses site_url() To generate the log in URL
164  *
165  * @param string $redirect Path to redirect to on login.
166  */
167 function wp_login_url($redirect = '') {
168     if ( strlen($redirect) )
169         $redirect = "?redirect_to=$redirect";
170     
171     return site_url("wp-login.php$redirect", 'login');
172 }
173
174 /**
175  * Display the Registration or Admin link.
176  *
177  * Display a link which allows the user to navigate to the registration page if
178  * not logged in and registration is enabled or to the dashboard if logged in.
179  *
180  * @since 1.5.0
181  * @uses apply_filters() Calls 'register' hook on register / admin link content.
182  *
183  * @param string $before Text to output before the link (defaults to <li>).
184  * @param string $after Text to output after the link (defaults to </li>).
185  */
186 function wp_register( $before = '<li>', $after = '</li>' ) {
187
188     if ( ! is_user_logged_in() ) {
189         if ( get_option('users_can_register') )
190             $link = $before . '<a href="' . site_url('wp-login.php?action=register', 'login') . '">' . __('Register') . '</a>' . $after;
191         else
192             $link = '';
193     } else {
194         $link = $before . '<a href="' . admin_url() . '">' . __('Site Admin') . '</a>' . $after;
195     }
196
197     echo apply_filters('register', $link);
198 }
199
200 /**
201  * Theme container function for the 'wp_meta' action.
202  *
203  * The 'wp_meta' action can have several purposes, depending on how you use it,
204  * but one purpose might have been to allow for theme switching.
205  *
206  * @since 1.5.0
207  * @link http://trac.wordpress.org/ticket/1458 Explanation of 'wp_meta' action.
208  * @uses do_action() Calls 'wp_meta' hook.
209  */
210 function wp_meta() {
211     do_action('wp_meta');
212 }
213
214 /**
215  * Display information about the blog.
216  *
217  * @see get_bloginfo() For possible values for the parameter.
218  * @since 0.71
219  *
220  * @param string $show What to display.
221  */
222 function bloginfo($show='') {
223     echo get_bloginfo($show, 'display');
224 }
225
226 /**
227  * Retrieve information about the blog.
228  *
229  * Some show parameter values are deprecated and will be removed in future
230  * versions. Care should be taken to check the function contents and know what
231  * the deprecated blog info options are. Options without "// DEPRECATED" are
232  * the preferred and recommended ways to get the information.
233  *
234  * The possible values for the 'show' parameter are listed below.
235  * <ol>
236  * <li><strong>url<strong> - Blog URI to homepage.</li>
237  * <li><strong>wpurl</strong> - Blog URI path to WordPress.</li>
238  * <li><strong>description</strong> - Secondary title</li>
239  * </ol>
240  *
241  * The feed URL options can be retrieved from 'rdf_url' (RSS 0.91),
242  * 'rss_url' (RSS 1.0), 'rss2_url' (RSS 2.0), or 'atom_url' (Atom feed). The
243  * comment feeds can be retrieved from the 'comments_atom_url' (Atom comment
244  * feed) or 'comments_rss2_url' (RSS 2.0 comment feed).
245  *
246  * There are many other options and you should check the function contents:
247  * {@source 32 37}
248  *
249  * @since 0.71
250  *
251  * @param string $show Blog info to retrieve.
252  * @param string $filter How to filter what is retrieved.
253  * @return string Mostly string values, might be empty.
254  */
255 function get_bloginfo($show = '', $filter = 'raw') {
256
257     switch($show) {
258         case 'url' :
259         case 'home' : // DEPRECATED
260         case 'siteurl' : // DEPRECATED
261             $output = get_option('home');
262             break;
263         case 'wpurl' :
264             $output = get_option('siteurl');
265             break;
266         case 'description':
267             $output = get_option('blogdescription');
268             break;
269         case 'rdf_url':
270             $output = get_feed_link('rdf');
271             break;
272         case 'rss_url':
273             $output = get_feed_link('rss');
274             break;
275         case 'rss2_url':
276             $output = get_feed_link('rss2');
277             break;
278         case 'atom_url':
279             $output = get_feed_link('atom');
280             break;
281         case 'comments_atom_url':
282             $output = get_feed_link('comments_atom');
283             break;
284         case 'comments_rss2_url':
285             $output = get_feed_link('comments_rss2');
286             break;
287         case 'pingback_url':
288             $output = get_option('siteurl') .'/xmlrpc.php';
289             break;
290         case 'stylesheet_url':
291             $output = get_stylesheet_uri();
292             break;
293         case 'stylesheet_directory':
294             $output = get_stylesheet_directory_uri();
295             break;
296         case 'template_directory':
297         case 'template_url':
298             $output = get_template_directory_uri();
299             break;
300         case 'admin_email':
301             $output = get_option('admin_email');
302             break;
303         case 'charset':
304             $output = get_option('blog_charset');
305             if ('' == $output) $output = 'UTF-8';
306             break;
307         case 'html_type' :
308             $output = get_option('html_type');
309             break;
310         case 'version':
311             global $wp_version;
312             $output = $wp_version;
313             break;
314         case 'language':
315             $output = get_locale();
316             $output = str_replace('_', '-', $output);
317             break;
318         case 'text_direction':
319             global $wp_locale;
320             $output = $wp_locale->text_direction;
321             break;
322         case 'name':
323         default:
324             $output = get_option('blogname');
325             break;
326     }
327
328     $url = true;
329     if (strpos($show, 'url') === false &&
330         strpos($show, 'directory') === false &&
331         strpos($show, 'home') === false)
332         $url = false;
333
334     if ( 'display' == $filter ) {
335         if ( $url )
336             $output = apply_filters('bloginfo_url', $output, $show);
337         else
338             $output = apply_filters('bloginfo', $output, $show);
339     }
340
341     return $output;
342 }
343
344 /**
345  * Display or retrieve page title for all areas of blog.
346  *
347  * By default, the page title will display the separator before the page title,
348  * so that the blog title will be before the page title. This is not good for
349  * title display, since the blog title shows up on most tabs and not what is
350  * important, which is the page that the user is looking at.
351  *
352  * There are also SEO benefits to having the blog title after or to the 'right'
353  * or the page title. However, it is mostly common sense to have the blog title
354  * to the right with most browsers supporting tabs. You can achieve this by
355  * using the seplocation parameter and setting the value to 'right'. This change
356  * was introduced around 2.5.0, in case backwards compatibility of themes is
357  * important.
358  *
359  * @since 1.0.0
360  *
361  * @param string $sep Optional, default is '&raquo;'. How to separate the various items within the page title.
362  * @param bool $display Optional, default is true. Whether to display or retrieve title.
363  * @param string $seplocation Optional. Direction to display title, 'right'.
364  * @return string|null String on retrieve, null when displaying.
365  */
366 function wp_title($sep = '&raquo;', $display = true, $seplocation = '') {
367     global $wpdb, $wp_locale, $wp_query;
368
369     $cat = get_query_var('cat');
370     $tag = get_query_var('tag_id');
371     $category_name = get_query_var('category_name');
372     $author = get_query_var('author');
373     $author_name = get_query_var('author_name');
374     $m = get_query_var('m');
375     $year = get_query_var('year');
376     $monthnum = get_query_var('monthnum');
377     $day = get_query_var('day');
378     $title = '';
379
380     $t_sep = '%WP_TITILE_SEP%'; // Temporary separator, for accurate flipping, if necessary
381
382     // If there's a category
383     if ( !empty($cat) ) {
384             // category exclusion
385             if ( !stristr($cat,'-') )
386                 $title = apply_filters('single_cat_title', get_the_category_by_ID($cat));
387     } elseif ( !empty($category_name) ) {
388         if ( stristr($category_name,'/') ) {
389                 $category_name = explode('/',$category_name);
390                 if ( $category_name[count($category_name)-1] )
391                     $category_name = $category_name[count($category_name)-1]; // no trailing slash
392                 else
393                     $category_name = $category_name[count($category_name)-2]; // there was a trailling slash
394         }
395         $cat = get_term_by('slug', $category_name, 'category', OBJECT, 'display');
396         if ( $cat )
397             $title = apply_filters('single_cat_title', $cat->name);
398     }
399
400     if ( !empty($tag) ) {
401         $tag = get_term($tag, 'post_tag', OBJECT, 'display');
402         if ( is_wp_error( $tag ) )
403             return $tag;
404         if ( ! empty($tag->name) )
405             $title = apply_filters('single_tag_title', $tag->name);
406     }
407
408     // If there's an author
409     if ( !empty($author) ) {
410         $title = get_userdata($author);
411         $title = $title->display_name;
412     }
413     if ( !empty($author_name) ) {
414         // We do a direct query here because we don't cache by nicename.
415         $title = $wpdb->get_var($wpdb->prepare("SELECT display_name FROM $wpdb->users WHERE user_nicename = %s", $author_name));
416     }
417
418     // If there's a month
419     if ( !empty($m) ) {
420         $my_year = substr($m, 0, 4);
421         $my_month = $wp_locale->get_month(substr($m, 4, 2));
422         $my_day = intval(substr($m, 6, 2));
423         $title = "$my_year" . ($my_month ? "$t_sep$my_month" : "") . ($my_day ? "$t_sep$my_day" : "");
424     }
425
426     if ( !empty($year) ) {
427         $title = $year;
428         if ( !empty($monthnum) )
429             $title .= "$t_sep" . $wp_locale->get_month($monthnum);
430         if ( !empty($day) )
431             $title .= "$t_sep" . zeroise($day, 2);
432     }
433
434     // If there is a post
435     if ( is_single() ||  ( is_page() && !is_front_page() ) ) {
436         $post = $wp_query->get_queried_object();
437         $title = strip_tags( apply_filters( 'single_post_title', $post->post_title ) );
438     }
439
440     // If there's a taxonomy
441     if ( is_tax() ) {
442         $taxonomy = get_query_var( 'taxonomy' );
443         $tax = get_taxonomy( $taxonomy );
444         $tax = $tax->label;
445         $term = $wp_query->get_queried_object();
446         $term = $term->name;
447         $title = "$tax$t_sep$term";
448     }
449
450     if ( is_404() ) {
451         $title = __('Page not found');   
452     }
453     
454     $prefix = '';
455     if ( !empty($title) )
456         $prefix = " $sep ";
457
458      // Determines position of the separator and direction of the breadcrumb
459     if ( 'right' == $seplocation ) { // sep on right, so reverse the order
460         $title_array = explode( $t_sep, $title );
461         $title_array = array_reverse( $title_array );
462         $title = implode( " $sep ", $title_array ) . $prefix;
463     } else {
464         $title_array = explode( $t_sep, $title );
465         $title = $prefix . implode( " $sep ", $title_array );
466     }
467
468     $title = apply_filters('wp_title', $title, $sep, $seplocation);
469
470     // Send it out
471     if ( $display )
472         echo $title;
473     else
474         return $title;
475
476 }
477
478 /**
479  * Display or retrieve page title for post.
480  *
481  * This is optimized for single.php template file for displaying the post title.
482  * Only useful for posts, does not support pages for example.
483  *
484  * It does not support placing the separator after the title, but by leaving the
485  * prefix parameter empty, you can set the title separator manually. The prefix
486  * does not automatically place a space between the prefix, so if there should
487  * be a space, the parameter value will need to have it at the end.
488  *
489  * @since 0.71
490  * @uses $wpdb
491  *
492  * @param string $prefix Optional. What to display before the title.
493  * @param bool $display Optional, default is true. Whether to display or retrieve title.
494  * @return string|null Title when retrieving, null when displaying or failure.
495  */
496 function single_post_title($prefix = '', $display = true) {
497     global $wpdb;
498     $p = get_query_var('p');
499     $name = get_query_var('name');
500
501     if ( intval($p) || '' != $name ) {
502         if ( !$p )
503             $p = $wpdb->get_var($wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_name = %s", $name));
504         $post = & get_post($p);
505         $title = $post->post_title;
506         $title = apply_filters('single_post_title', $title);
507         if ( $display )
508             echo $prefix.strip_tags($title);
509         else
510             return strip_tags($title);
511     }
512 }
513
514 /**
515  * Display or retrieve page title for category archive.
516  *
517  * This is useful for category template file or files, because it is optimized
518  * for category page title and with less overhead than {@link wp_title()}.
519  *
520  * It does not support placing the separator after the title, but by leaving the
521  * prefix parameter empty, you can set the title separator manually. The prefix
522  * does not automatically place a space between the prefix, so if there should
523  * be a space, the parameter value will need to have it at the end.
524  *
525  * @since 0.71
526  *
527  * @param string $prefix Optional. What to display before the title.
528  * @param bool $display Optional, default is true. Whether to display or retrieve title.
529  * @return string|null Title when retrieving, null when displaying or failure.
530  */
531 function single_cat_title($prefix = '', $display = true ) {
532     $cat = intval( get_query_var('cat') );
533     if ( !empty($cat) && !(strtoupper($cat) == 'ALL') ) {
534         $my_cat_name = apply_filters('single_cat_title', get_the_category_by_ID($cat));
535         if ( !empty($my_cat_name) ) {
536             if ( $display )
537                 echo $prefix.strip_tags($my_cat_name);
538             else
539                 return strip_tags($my_cat_name);
540         }
541     } else if ( is_tag() ) {
542         return single_tag_title($prefix, $display);
543     }
544 }
545
546 /**
547  * Display or retrieve page title for tag post archive.
548  *
549  * Useful for tag template files for displaying the tag page title. It has less
550  * overhead than {@link wp_title()}, because of its limited implementation.
551  *
552  * It does not support placing the separator after the title, but by leaving the
553  * prefix parameter empty, you can set the title separator manually. The prefix
554  * does not automatically place a space between the prefix, so if there should
555  * be a space, the parameter value will need to have it at the end.
556  *
557  * @since 2.3.0
558  *
559  * @param string $prefix Optional. What to display before the title.
560  * @param bool $display Optional, default is true. Whether to display or retrieve title.
561  * @return string|null Title when retrieving, null when displaying or failure.
562  */
563 function single_tag_title($prefix = '', $display = true ) {
564     if ( !is_tag() )
565         return;
566
567     $tag_id = intval( get_query_var('tag_id') );
568
569     if ( !empty($tag_id) ) {
570         $my_tag = &get_term($tag_id, 'post_tag', OBJECT, 'display');
571         if ( is_wp_error( $my_tag ) )
572             return false;
573         $my_tag_name = apply_filters('single_tag_title', $my_tag->name);
574         if ( !empty($my_tag_name) ) {
575             if ( $display )
576                 echo $prefix . $my_tag_name;
577             else
578                 return $my_tag_name;
579         }
580     }
581 }
582
583 /**
584  * Display or retrieve page title for post archive based on date.
585  *
586  * Useful for when the template only needs to display the month and year, if
587  * either are available. Optimized for just this purpose, so if it is all that
588  * is needed, should be better than {@link wp_title()}.
589  *
590  * It does not support placing the separator after the title, but by leaving the
591  * prefix parameter empty, you can set the title separator manually. The prefix
592  * does not automatically place a space between the prefix, so if there should
593  * be a space, the parameter value will need to have it at the end.
594  *
595  * @since 0.71
596  *
597  * @param string $prefix Optional. What to display before the title.
598  * @param bool $display Optional, default is true. Whether to display or retrieve title.
599  * @return string|null Title when retrieving, null when displaying or failure.
600  */
601 function single_month_title($prefix = '', $display = true ) {
602     global $wp_locale;
603
604     $m = get_query_var('m');
605     $year = get_query_var('year');
606 <