最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

filters - Can you use add_filter() inside other function?

programmeradmin7浏览0评论

I'm new to WordPress, and currently developing my first plugin and currently having difficulties.

How I can insert the add_filter action inside my submit function? I want the add_filter action to process after the user click the submit button.

I have try this but it didn't work.

if(isset($_POST['btn_submit'])) {

    function addContent($content = '') {            
        $content .= "My content";

        return $content;
    }

    add_filter('the_content', 'addContent');
}

Any help would be greatly appreciated. Thanks!

I'm new to WordPress, and currently developing my first plugin and currently having difficulties.

How I can insert the add_filter action inside my submit function? I want the add_filter action to process after the user click the submit button.

I have try this but it didn't work.

if(isset($_POST['btn_submit'])) {

    function addContent($content = '') {            
        $content .= "My content";

        return $content;
    }

    add_filter('the_content', 'addContent');
}

Any help would be greatly appreciated. Thanks!

Share Improve this question edited Feb 8, 2012 at 23:37 EAMann 32.2k9 gold badges88 silver badges147 bronze badges asked Feb 7, 2012 at 13:38 ronnielronniel 211 gold badge1 silver badge3 bronze badges 1
  • 1 If you've solved this problem (as stated in comments blow), please post your solution as an answer for others. – EAMann Commented Feb 7, 2012 at 15:26
Add a comment  | 

3 Answers 3

Reset to default 3

I think what you're ACTUALLY looking to do is to apply_filters(). add_filter() registers a new filter, whereas apply_filters() does the filters that have been registered.

If that's not what you're looking to do, then you need to be aware that add_filter() needs to be run every time you want the filter applied. This allows plugins to be removed without having to unregister all their filters and generally keeps a wordpress install pretty clean...it also helps with security. A better question might encompass a broader scope, where you state what you're trying to do, rather than having us try and troubleshoot your implementation of it.

You can also write in the following way:

    function addContent($content = '') {    
     $content .= "My content";    
     return $content; 
    }

   if(isset($_POST['btn_submit'])) {
    add_filter('the_content', 'addContent'); }

@Shaon, Thanks for your reply. I couldn't get that to work efefctively, but here is a very workable function I found: http://core.trac.wordpress/ticket/15311#comment:13

发布评论

评论列表(0)

  1. 暂无评论