wordpress插件如何添加内容?

tab*_*ter 2 php wordpress plugins

这可能是一个奇怪的问题.当我添加像Facebook Like Button和Gigpress这样的插件时,它们提供了在每个单页博客帖子之前或之后插入内容的选项.例如,我将Gigpress和FB Like按钮设置为在我的帖子中添加文本下方的内容,这是有效的,不完美.类似按钮显示在帖子文本下方.

那么如何在后端实现这一目标呢?它看起来不像模板或其他php文件被插件改变,但似乎也没有任何明显的PHP代码将拉入数据.这种类型的功能是否以某种方式构建到"框架"中?

我问的原因是格式化原因......两个插件添加的内容冲突并且看起来很糟糕.我想弄清楚如何修改CSS.

谢谢

Obm*_*nen 7

他们通过过滤器,操作和挂钩来实现它.

在你的情况下 - 与the_content过滤器..

示例(来自codex):

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;
}
Run Code Online (Sandbox Code Playgroud)

一个更简单易懂的例子:

 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;
    }
Run Code Online (Sandbox Code Playgroud)

要查看此示例的实际操作,请将其放在functions.php中

这实际上是了解wordpress并开始编写插件的最重要的一步.如果您真的有兴趣,请阅读上面的链接.

另外,打开刚刚提到的插件文件,查找 过滤器操作 ...