[Solved] AddComment hook  

  RSS

PatrickD
(@patrickd)
Member Customer
Joined: 1 year ago
Posts: 6
13/07/2019 7:02 pm  

Hi there,

I am looking for a way to hook / filter the AddComment action.
What I want to do is add a audio queue to go along with it.

Something simple like

<?php $sound = get_stylesheet_directory_uri() . '/sounds/comment-sound.mp3'; ?>
<script type="text/javascript">
var sound = '<?= $sound ?>';
var audio = new Audio(sound);
var promise = audio.play();
if (promise) {
promise.catch(function(error) { console.error(error); });
}
</script>

Above is working on other parts ... I just want to be able to set it up for the add comment action in WPDiscuz.

I am quite certain I should be looking at; wpdAddComment or AddComment but I have no idea as to how to adress them correctly.

Any help would be greatly appreciated.

Best regards,
Patrick


Topic Tags
Astghik
(@astghik)
Support Team Admin
Joined: 2 years ago
Posts: 3476
13/07/2019 8:33 pm  

Hi Patrick,

Thank you for using wpDiscuz and for contacting us.

I'm going to ask this question to the developers of the plugin. I'll update the topic once I get a response from them. 


Astghik
(@astghik)
Support Team Admin
Joined: 2 years ago
Posts: 3476
15/07/2019 3:34 pm  

Hi @patrickd,

1. Create a new function and add your js code there, e.g function your_js_function_name()

2. Add the function in some of the js files of the active theme. 

3. Put the following code in active theme's functions.php file

add_filter('wpdiscuz_comment_post', function($messageArray){
$messageArray['callbackFunctions'][] = 'your_js_function_name';
return $messageArray;
});

4. Replace the red marked function name with the function name you've created in step 1. 


PatrickD
(@patrickd)
Member Customer
Joined: 1 year ago
Posts: 6
15/07/2019 8:13 pm  

@astghik

Cool will look into this soon Smile Thanks for the quick reply and follow up!


PatrickD
(@patrickd)
Member Customer
Joined: 1 year ago
Posts: 6
17/07/2019 1:10 am  

Sorry to pop in once again with this.
We seem very close but it keeps throwing; "comment_sound is not a function" in the console.
So let me check with you real quick if I overlooked something;

In my functions.php I have;

/**
* WPDiscuz - Comment Sound
*/
add_filter('wpdiscuz_comment_post', function($messageArray){
$messageArray['callbackFunctions'][] = 'comment_sound';
return $messageArray;
});

In one of the main js files which I include I have;

function comment_sound() {
var sound = 'https://www.nintendoreporters.com/storage/themes/nintendoreporters/sounds/comment-sound.mp3';
var audio = new Audio(sound);
var promise = audio.play();
if (promise !== undefined) {
promise.then(function() {
}).catch(function(error) {
});
}

Calling that function directly in console as comment_sound(); does work.
But when issued as a callbackFunctions it keeps saying that it is not a function if a comment is made.

Am I overlooking something here or did I do something weird syntax wise?

Any help would be greatly appreciated.

Best regards,
Patrick Dankers


PatrickD
(@patrickd)
Member Customer
Joined: 1 year ago
Posts: 6
27/07/2019 11:38 am  

Sorry to bump this but if someone could point me in to what I am doing wrong that would be very much appreciated.

Kind regards,

Patrick Dankers


Astghik
(@astghik)
Support Team Admin
Joined: 2 years ago
Posts: 3476
27/07/2019 4:59 pm  

@patrickd

Please try to replace the js function with this one: 

wpdiscuzAjaxObj.comment_sound = function () {
   var sound = ' https://www.nintendoreporters.com/storage/themes/nintendoreporters/sounds/comment-sound.mp3 ';
   var audio = new Audio(sound);
   var promise = audio.play();
   if (promise !== undefined) {
       promise.then(function() {
       }).catch(function(error) {
       });
   }

PatrickD
(@patrickd)
Member Customer
Joined: 1 year ago
Posts: 6
27/07/2019 5:39 pm  

Thanks for getting back to me.
Ah so I had  to change the way it was called .. ok that makes sense.
Will give this a try later on. 

Kind regards,
Patrick


PatrickD
(@patrickd)
Member Customer
Joined: 1 year ago
Posts: 6
27/07/2019 7:01 pm  

Awesome it works!

I would like to make a small note if other people attempt this;
Make sure your theme makes use of body classes that way you can easily keep this globally setup for all your CPT's with the following wrapped around it; 

if ($("body").hasClass("single")) {}

Otherwise wpdiscuzAjaxObj will not always be available 😉
(if you set it up to only load these assets on pages that have comments ... which ofcourse is more optimal)


Share:
Green
Default
Pear
Black Emo
{green}:grinning:
{green}:wink:
{green}:face:
{green}:inlove:
{green}:sweaty:
{green}:surprised:
{green}:lovekiss:
{green}:laugh:
{green}:formalsmile:
{green}:displeased:
{green}:cool:
{green}:resent:
{green}:nerd:
{green}:screaming:
{green}:amazed:
{green}:sad:
{green}:deceitful:
{green}:starryeyes:
{green}:evil:
{green}:shocked:
{green}:tears:
{green}:sulky:
{green}:smile:
{green}:vomited:
{green}:hi:
{green}:afraid:
{green}:crazy:
{green}:rabid:
{green}:fighting:
{green}:nonoise:
{green}:blushed:
{green}:idontknow:
{green}:scared:
{green}:razz:
{green}:kiss:
{green}:eat:
{green}:shutmouth:
{green}:gape:
{green}:suspicious:
{green}:laughingoutloud:
{green}:bruise:
{green}:crying:
{green}:pray:
{green}:serious:
{green}:excitement:
:)
:d
:wink:
:mrgreen:
:neutral:
:twisted:
:arrow:
:shock:
:???:
:cool:
:evil:
:oops:
:razz:
:roll:
:cry:
:eek:
:lol:
:mad:
:sad:
:!:
:?:
:idea:
:hmm:
:beg:
:whew:
:chuckle:
:silly:
:envy:
:shutmouth:
{pear}:happy:
{pear}:smile:
{pear}:laugh:
{pear}:laughingoutloud:
{pear}:crying:
{pear}:exhausted:
{pear}:nerd:
{pear}:surprised:
{pear}:veryhungry:
{pear}:wink:
{blackemo}:laughtertotears:
{blackemo}:gift:
{blackemo}:love:
{blackemo}:inlove:
{blackemo}:shamefaced:
{blackemo}:heart:
{blackemo}:crazy:
{blackemo}:anguished:
{blackemo}:bruise:
{blackemo}:easymoney:
{blackemo}:exhausted:
{blackemo}:vampire:
{blackemo}:shutmouth:
{blackemo}:wink:
{blackemo}:carnival:
{blackemo}:flowers:
{blackemo}:hotdrink:
{blackemo}:party: