Skip to:
Content
Pages
Categories
Search
Top
Bottom

Post submission hooks


  • s1r0n
    Participant

    @s1r0n

    HI all.

    I want to be able to add some fields to the BBpress post. So in addition to Topic Title, content,and Topic Tags I might also be able to add a custom spam field, custom meta tags, etc. Can anybody tell me what the hooks are to accomplish that?

Viewing 12 replies - 1 through 12 (of 12 total)

  • Robin W
    Moderator

    @robin-w

    yes look in the form templates which you’ll find in the plugin
    bbpress/templates/default/bbpress

    in particular

    bbpress/templates/default/bbpress/form-topic.php
    and
    bbpress/templates/default/bbpress/form-reply.php

    either

    a)( You’ll find several hooks in there which you can add_action to , or

    b) you can copy these templates to your theme and modify happily by

    create a directory on your theme called ‘bbpress’
    ie wp-content/themes/%your-theme-name%/bbpress

    where %your-theme-name% is the name of your theme

    then find
    wp-content/plugins/bbpress/templates/default/bbpress/xx.php

    where xx.php is say form-topic.php

    Make a copy of this file, and put in in the directory called bbpress that you created above, so you end up with
    wp-content/themes/%your-theme-name%/bbpress/xx.php
    where xx.php is say form-topic.php

    bbPress will now use this template instead of the original


    Robkk
    Moderator

    @robkk

    @s1r0n

    I want to be able to add some fields to the BBpress post. So in addition to Topic Title, content,and Topic Tags I might also be able to add a custom spam field

    if you are doing a honeypot type of spam protection please share how to achieve this when you have accomplished coding it , it might help other users …heck even this site.


    s1r0n
    Participant

    @s1r0n

    ok that’s a start. where can I find the hooks to deal with the post content just before it gets posted into the database. so somebody hits the submit button and I can intercept that submission with what action/filter.

    I tried searching the bbs code, but faster if somebody can just say


    s1r0n
    Participant

    @s1r0n

    ok i will share.


    Robin W
    Moderator

    @robin-w


    s1r0n
    Participant

    @s1r0n

    Ya ok i got something figured out. its very basic but you can easily add to this. here’s the code

    This basically ads a field call ‘lp_post_honey_post’ with the question “I am a robot”. if the user answers anything other than ‘no’ the post craps out with an error. currently the error message is that the post parameters are wrong, but maybe if somebody can tell me how to set the bbpress error message I can fix that.

    You can see the filter in action here

    http://www.thelightningpath.com/forums/room/lobby/pre-registration/

    There are a few tweaks you could make to this. if you have a plugin like piklist you can easily have a settings page where you could set both the question and the answer. And of course you can change the question in the hard code.

    I choose the wp_insert_post_empty_content filter because it fires early on in the wp_update_post routine, saving some CPU cycles.

    If somebody wants to help me set this up as a wordpress plugin we could make this a plugin pretty easily. I’m guessing there would be a lot of interest in this.
    incorrect code removed ! - correct version below


    s1r0n
    Participant

    @s1r0n

    oops. had a mistake in code. this one works better

    
    add_action('bbp_theme_after_topic_form_content','lp_add_honeypot');
    add_action('bbp_theme_after_reply_form_content','lp_add_honeypot');
    function lp_add_honeypot() {
    
        if (is_user_logged_in())
            return;
        ?>
    <p>
        <label for="lp_post_honey_pot">I am a robot (yes/no)</label><br />
        <input type="text" value="" tabindex="103" size="40" name="lp_post_honey_pot" id="lp_post_honey_pot"  />
    </p>
    <?php     
    }
    
    //https://core.trac.wordpress.org/browser/tags/4.1.1/src/wp-includes/post.php#L0
    //add_filter('wp_insert_post_empty_content', 'lp_check_honeypot', 10, 2);
    function lp_check_honeypot($is_empty, $postarr) {
        
    //   print_r($postarr);exit;
        if ($postarr['post_type'] == 'topic' || $postarr['post_type'] == 'reply') {//only do this for bbspress
            if ($postarr['bbp_post_as_submitted']['POST_lp_post_honey_pot'] == 'no') {
                return false; //treat this post as good
            }
    
            return true;
        }
        return false;
    }

    s1r0n
    Participant

    @s1r0n

    it will work now


    Robin W
    Moderator

    @robin-w

    great – thanks for posting back – edited and removed the wrong version code above – some people never read all the way through !

    Thanks again


    s1r0n
    Participant

    @s1r0n

    Ah you’ll have to do that again. the code above has a line commented out that shouldn’t be. sorry for the hassle

    add_action('bbp_theme_after_topic_form_content','lp_add_honeypot');
    add_action('bbp_theme_after_reply_form_content','lp_add_honeypot');
    function lp_add_honeypot() {
    
        if (is_user_logged_in())
            return;
        ?>
    <p>
        <label for="lp_post_honey_pot">I am a robot (yes/no)</label><br />
        <input type="text" value="" tabindex="103" size="40" name="lp_post_honey_pot" id="lp_post_honey_pot"  />
    </p>
    <?php     
    }
    
    //https://core.trac.wordpress.org/browser/tags/4.1.1/src/wp-includes/post.php#L0
    add_filter('wp_insert_post_empty_content', 'lp_check_honeypot', 10, 2);
    function lp_check_honeypot($is_empty, $postarr) {
        
         if (is_user_logged_in())
            return false;
        
    
        if ($postarr['post_type'] == 'topic' || $postarr['post_type'] == 'reply') {//only do this for bbspress
            if ($postarr['bbp_post_as_submitted']['POST_lp_post_honey_pot'] == 'no') {
                return false; //treat this post as good
            }
    
            return true;
        }
        return false;
    }

    I only need this to work for users who are not logged in, guests, so i’ve added a couple of lines to turn this off when users are logged in. if you want to test even registered users just leave those lines in.

    When I have a bit of time I’ll look into bbpress errors and find a way to set an appropriate error message.

    Have fun and let us know if you use it and it works for you or not


    Robkk
    Moderator

    @robkk

    @s1r0n

    i tested it out on your site and see its working better

    its not really a honeypot field though

    how a honeypot field is supposed to be is invisible to the users on your site and is not required for input for the user.

    its a hidden input field that if it has any type of dummy information from spam-bots injected , it should stop the next action that the spammer is trying to do.

    the way your code is arranged its close to these two plugins

    https://wordpress.org/plugins/growmap-anti-spambot-plugin/ ( probably no bbpress compatibility)
    https://github.com/studiohyperset/bpress-no-captcha-recaptcha ( still in development)

    you might want to go to the bbpress nocatpcha plugin (when its done being developed) if you think it works better for what you need.

    since you/robin found half the code im going to try to make a honeypot using these two plugins for the other half

    wordpress.org/plugins/registration-honeypot/
    wordpress.org/plugins/zero-spam/ (i dont know about bbpress compatibility yet)

    also @s1r0n what do you mean by custom meta tags, like explain exactly how you want it so i can see if a normal seo plugin cant do this already.


    Robkk
    Moderator

    @robkk

    @s1r0n

    try this edited honeypot plugin , based on registration honeypot

    its shows on topic/reply forms , registration/login/lost-password forms, and comment forms (normal users dont see it though.)

    it should help some to fight automated spambots

    https://gist.github.com/robkk/ca8e71e2c944f17e5bbe

Viewing 12 replies - 1 through 12 (of 12 total)
  • You must be logged in to reply to this topic.
Skip to toolbar