Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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
1  
If you've solved this problem (as stated in comments blow), please post your solution as an answer for others. –  EAMann Feb 7 '12 at 15:26

3 Answers 3

@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.org/ticket/15311#comment:13

share|improve this answer

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'); }
share|improve this answer

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.

share|improve this answer
    
I want to add content at the bottom of every post of my site, and add_filter did it, but add_filter() action only process when the plugin is activated, but how about if I want it to process after the submit button was click? Is apply_filters() will work? I already read the documentaion about apply_filters() and I didn't find the solution to my question. –  ronniel Feb 7 '12 at 14:15
    
I just want to say that I solved this already. I separate the addContent function from submit function and put a line of code inside submit function that calls the function to add contents. something like this: function addContent($content = '') { $content .= "My content"; return $content; } add_filter('the_content', 'addContent'); if(isset($_POST['btn_submit'])) { $content .= addContent(); } this works for me.. thanks a lot. –  ronniel Feb 7 '12 at 14:31
    
If you want it to only show up once a button has been clicked and then go away on the next page chance (once the post data is gone), then what you're doing is an appropriate method. If you want to change the behavior permanently when the button is clicked, you will need to store the option in the database. –  m0r7if3r Feb 7 '12 at 14:31

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.