[Spam] Re: [Spam] [wp-hackers] filter for comment form

Michael D. Adams mikea at turbonet.com
Mon May 29 23:13:32 GMT 2006


> Thanks for pointing this out. I've seen the comment_form action... as far
> as I can tell this can only be used to add lines within the form tags, and
> cannot be used to modify the form tags themselves. Am I mistaken?

Sorry for misreading your original message.

If you can depend on the client having Javascript, you can change the form
client side.  This is a little something I whipped up to do that.  I've
not debugged any of it.

In case the formatting gets messed up, it will be here for a while:
http://blogwaffe.com/wp-content/form.php.txt

<?php
/*
Plugin Name: Change Form
Description: An example of how to change the comment form using JS.  Changes the action attribute value to this file.
Author: MDAWaffe
Version: lame
Author URI: http://blogwaffe.com
*/

if ( isset($_POST['my_form_element']) ) {
        var_dump($_POST); // Do Something
        exit;
}

function my_comment_form() {
        echo "<input type='hidden' name='my_form_element' id='my_form_element' value='1' />";
}

function this_file() {
        return get_settings( 'siteurl' ) . strstr(__FILE__, '/wp-content');
}

function my_javascript() {
        if ( !is_single() && !is_page() )
                return;
        echo "<script type='text/javascript'>
        //Simon Willison http://simon.incutio.com/archive/2004/05/26/addLoadEvent
        function addLoadEvent(func) {
                var oldonload = window.onload;
                if (typeof window.onload != 'function') {
                        window.onload = func;
                } else {
                        window.onload = function() {
                                oldonload();
                                func();
                        }
                }
        }
        function changeCommentForm() {
                var myElement = document.getElementById('my_form_element');
                if ( !myElement )
                        return;
                // Can't guarantee that it's the only/first form on the page, so we have to crawl up the DOM
                var commentForm = myElement.parentNode;
                while( !commentForm.tagName.match(/form/i) )
                        commentForm = commentForm.parentNode;
                commentForm.action = '" . this_file() . "';
        }
        addLoadEvent(changeCommentForm);
</script>";
}

add_action( 'comment_form', 'my_comment_form' );
add_action( 'wp_head', 'my_javascript' ); // Or use wp_print_scripts if WP 2.1+
?>


More information about the wp-hackers mailing list