Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2014-06-18 02:10:09

jdueck
Plugin Author
From: Minneapolis, MN
Registered: 2004-02-27
Posts: 147
Website

Modifying the contents of comments

Apologies if this seems simplistic, but I’ve tried looking for the answer to no avail so far.

I’m writing a very simple plugin: if a commenter uses “@” as the first character in their “website” address, I want the value to be written to the DB as the web address of their twitter account. E.g., if a commenter puts “@joeld” for their website, it gets stored and presented as “https://twitter.com/joeld”. So I want my plugin’s callback to be able to actually change the comment before it gets stored.

This was my first crack:

register_callback('jad_comment_twitterhandle','comment.save');

function jad_comment_twitterhandle($event='') {
	$form = array();

	$form = getComment();
	$w = $form['web'];

	if($w[0] == '@') {
		$form['web'] = "https://twitter.com/" . substr($w, 1, strlen($w) - 1);
	}
}

The thing is, the value returned by getComment() obviously doesn’t affect the actual comment, it’s just a copy meant for use by spam evaluators and what have you. I haven’t come across any plugins that actually change the comments in a callback in any way, so I’ve got nothing to go off of. Are there any hacks/facilities for this?

Offline

#2 2014-06-18 09:17:48

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: Modifying the contents of comments

There is no actual way to modify the comment contents, other than directly altering the HTTP query string values before the comment data is extracted and comment.save event. The getComment() function itself merely returns a copy of that raw data. Something along the lines of:

/* Trigger the function on pretext_end event */

register_callback('jad_comment_twitterhandle', 'pretext_end');

/* Modify the raw POST data when 'parentid' and 'submit' query string parameters are set */

function jad_comment_twitterhandle()
{
    if (gps('parentid') && gps('submit')) {
        if (strpos(ps('web'), '@') === 0) {
            $_POST['web'] = 'https://twitter.com/'.substr(ps('web'), 1);
        }
    }
}

Offline

#3 2014-06-20 02:16:28

jdueck
Plugin Author
From: Minneapolis, MN
Registered: 2004-02-27
Posts: 147
Website

Re: Modifying the contents of comments

Ah thanks! That code just so happens to do the job exactly, as-is… except it doesn’t show properly in the preview. But when I add (gps('submit') || gps('preview')) then it actually changes the text in the “website” box after viewing the preview. So now I’m considering some other solution, like testing for the @ in the comment form itself, without any callbacks…hmm we’ll see.

Offline

#4 2014-06-20 09:09:56

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: Modifying the contents of comments

Is JS an option?

<script>
	document.getElementById("web").addEventListener("input", function(event) {
		if(this.value.substring(0,1) == '@') this.value = 'https://twitter.com/'+this.value.substring(1);
	});
</script>

at the end of comment_form.

Offline

Board footer

Powered by FluxBB