[英]How do wordpress plugins add content?
這可能是一個奇怪的問題。 當我添加諸如Facebook Like Button和Gigpress之類的插件時,它們提供了在每個單頁博客文章之前或之后插入內容的選項。 例如,我同時設置了Gigpress和FB Like按鈕,以在帖子中的文本下方添加內容,並且這種方法是行之有效的。 帖子文本下方顯示“喜歡”按鈕。
那么如何在后端完成呢? 插件似乎並未更改模板或其他php文件,但似乎也沒有任何明顯的php代碼會提取數據。 這種功能是否以某種方式內置到“框架”中?
我要問的原因是出於格式化原因...兩個插件添加的內容沖突並且看起來很糟糕。 我試圖弄清楚如何修改CSS。
謝謝
他們通過使用Filters , Actions和Hooking來實現這一目標。
在您的情況下-使用the_content
過濾器..
示例(來自法典):
add_filter( 'the_content', 'my_the_content_filter', 20 );
/**
* Add a icon to the beginning of every post page.
*
* @uses is_single()
*/
function my_the_content_filter( $content ) {
if ( is_single() )
// Add image to the beginning of each page
$content = sprintf(
'<img class="post-icon" src="%s/images/post_icon.png" alt="Post icon" title=""/>%s',
get_bloginfo( 'stylesheet_directory' ),
$content
);
// Returns the content.
return $content;
}
一個簡單易懂的示例:
add_filter( 'the_content', 'add_something_to_content_filter', 20 );
function add_something_to_content_filter( $content ) {
$original_content = $content ; // preserve the original ...
$add_before_content = ' This will be added before the content.. ' ;
$add_after_content = ' This will be added after the content.. ' ;
$content = $add_before_content . $original_content . $add_after_content ;
// Returns the content.
return $content;
}
要查看此示例的實際操作,請將其放在您的functions.php中
實際上,這是了解wordpress和開始編寫插件的最重要的單個步驟。 如果您真的有興趣,請閱讀上面的鏈接。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.