wordpress插件如何添加内容? [英] How do wordpress plugins add content?

查看:228
本文介绍了wordpress插件如何添加内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个奇怪的问题.当我添加诸如Facebook Like Button和Gigpress之类的插件时,它们提供了在每个单页博客文章之前或之后插入内容的选项.例如,我同时设置了Gigpress和FB Like按钮,以在帖子中的文本下方添加内容,并且这种方法是不完美的,并且可以正常工作.帖子文字下方会显示喜欢"按钮.

This may be a weird question. When I add plugins like the Facebook Like Button and Gigpress, they offer options to insert content before or after each single-page blog post. For example, I have both Gigpress and the FB Like button set to add content below the text in my posts, and that is working, be it imperfectly. The like button shows up below the post text.

那么如何在后端完成呢?插件似乎并未更改模板或其他php文件,但似乎也没有任何明显的php代码会提取数据.这种功能是否以某种方式内置到框架"中?

So how is this accomplished on the back end? It doesn't look like the templates or other php files are being altered by the plugins, but there also doesn't seem to be any obvious php code that would be pulling in the data. Is this type of functionality somehow built into the "framework"?

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

The reason I'm asking is for formatting reasons...the content that is added by the two plugins conflicts and looks bad. I'm trying to figure out how to modify the css.

谢谢

推荐答案

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

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...

这篇关于wordpress插件如何添加内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆