WordPress 提供了一個(gè)非常簡(jiǎn)單方便的函數(shù)來顯示當(dāng)前文章的標(biāo)題,,那就是:the_title(),。
這個(gè)函數(shù)經(jīng)常被開發(fā)者在 header,post,,page,,loop,,footer 里使用,這幾乎是開發(fā)主題里最常用的Wordpress函數(shù)之一,,然而許多開發(fā)者并沒有意識(shí)到這里有個(gè)地方并不應(yīng)該使用此函數(shù),,那就是在 attributes 里,如:
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">繼續(xù)閱讀 <?php the_title(); ?></a>
很多開發(fā)者在 loop,,page,,post 里使用這樣的寫法設(shè)置一個(gè)超鏈接到指定的文章,看起來似乎并沒有什么問題,,但其實(shí)正確安全的寫法應(yīng)該把title=”<?php the_title();?>”改寫成title=”<?phpthe_title_attribute();?>”
為什么要這樣寫,,大家看看 WordPress 源文件中的相關(guān)函數(shù)核心文件便知了:
the_title() 源代碼:
function the_title($before = '', $after = '', $echo = true) { $title = get_the_title(); if ( strlen($title) == 0 ) return; $title = $before . $title . $after; if ( $echo ) echo $title; else return $title;}
這個(gè)函數(shù)并沒有提供給我們有效的信息,只是執(zhí)行了get_the_title()函數(shù),我們?cè)倏聪逻@個(gè)函數(shù)的相關(guān)文件.
function get_the_title( $post = 0 ) { $post = get_post( $post ); $title = isset( $post->post_title ) ? $post->post_title : ''; $id = isset( $post->ID ) ? $post->ID : 0; if ( ! is_admin() ) { if ( ! empty( $post->post_password ) ) { $protected_title_format = apply_filters( 'protected_title_format', __( 'Protected: %s' ) ); $title = sprintf( $protected_title_format, $title ); } else if ( isset( $post->post_status ) && 'private' == $post->post_status ) { $private_title_format = apply_filters( 'private_title_format', __( 'Private: %s' ) ); $title = sprintf( $private_title_format, $title ); } } return apply_filters( 'the_title', $title, $id );}
這個(gè)函數(shù)非常簡(jiǎn)單,,它用get_post()取回了post object,,然后把它傳遞給一個(gè)叫做the_title的filter,返回$post->post_title
這個(gè)函數(shù)最重要的地方就是apply_filters( ‘the_title’, $title, $id );
這個(gè) filter 可以提供給開發(fā)者自定義標(biāo)題的輸出形式,,比如添加額外的 html 標(biāo)簽。
the_title_attribute() 源代碼:
function the_title_attribute( $args = '' ) { $title = get_the_title(); if ( strlen($title) == 0 ) return; $defaults = array('before' => '', 'after' => '', 'echo' => true); $r = wp_parse_args($args, $defaults); extract( $r, EXTR_SKIP ); $title = $before . $title . $after; $title = esc_attr(strip_tags($title)); if ( $echo ) echo $title; else return $title;}
這個(gè)函數(shù)也使用了get_the_title()函數(shù)來取回文章的標(biāo)題,,但是最后返回的數(shù)據(jù)卻與the_title()函數(shù)不同,。這里過濾掉了許多轉(zhuǎn)義字符與html標(biāo)簽,能夠更加安全的在元素屬性里進(jìn)行使用,。
詳細(xì)例子:
假設(shè)你的$post->post_title是這樣的
<span class="title">這是有span標(biāo)簽的標(biāo)題</span>
當(dāng)你使用the_title()函數(shù),,輸出將保持不變,還是如下
<span class="title">這是有span標(biāo)簽的標(biāo)題</span>
但是當(dāng)你使用the_title_attribute(),,你的輸出是如下的
這是有span標(biāo)簽的標(biāo)題
注意這里的span標(biāo)簽已經(jīng)被移除掉了.
又假如如果你的標(biāo)題里有雙引號(hào),如下
這是一個(gè)帶 "雙引號(hào)" 的標(biāo)題
當(dāng)你使用the_title()函數(shù),,輸出如下
這是一個(gè)帶 "雙引號(hào)" 的標(biāo)題
但是當(dāng)你使用the_title_attrubute()函數(shù),輸出卻如下
這是一個(gè)帶 \"雙引號(hào)\" 的標(biāo)題
注意到這里自動(dòng)把雙引號(hào)替換成轉(zhuǎn)義字符了,,這樣就保證了html標(biāo)簽屬性的安全使用,。
如果我們?cè)趆tml標(biāo)簽屬性里使用the_title()函數(shù),則會(huì)破壞掉屬性原有的形式
<span title="<?php the_title(); ?>"><?php the_title(); ?></span>
輸出將會(huì)如下:
<span title="這是一個(gè)帶 "雙引號(hào)" 的標(biāo)題">這是一個(gè)帶”雙引號(hào)”的標(biāo)題</span>
注意到了這里的title屬性的引號(hào),html標(biāo)簽對(duì)引號(hào)的使用是非常嚴(yán)格的,,禁止這樣的形式出現(xiàn),,一旦出現(xiàn)將導(dǎo)致頁面嚴(yán)重的顯示問題.
經(jīng)過以上的分析,希望開發(fā)者們?cè)谝院蟮拈_發(fā)過程中能注意到這些小細(xì)節(jié),,在html標(biāo)簽屬性里一定要使用the_title_attribute()函數(shù)而不是the_title()函數(shù),!
所以正確的用法應(yīng)該是:
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">繼續(xù)閱讀 <?php the_title(); ?></a>
注:原文作者墨魚