且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

wordpress插件如何添加内容?

更新时间:2023-12-06 08:08:04

他们正在通过过滤器操作并与它们关联.

They are achieving it with Filters, Actions and Hooking into them.

以您的情况为准-使用 the_content 过滤器..

In your case - with the_content filter ..

示例(来自法典):

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

to see this example in action , put it in your functions.php

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

This is actually the single most important step to understanding wordpress, and starting to write plugins. If you really are interested , read the links above.

此外,打开您刚才提到的插件文件,然后查找 过滤器

Also , Open the plugin files you have just mentioned and look for the Filters and Actions...