Notifications
Clear all

[Solved] AddComment hook

9 Posts
2 Users
0 Reactions
1,621 Views
PatrickD
(@patrickd)
Member Customer
Joined: 6 years ago
Posts: 18
Topic starter  

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


   
Astghik
(@astgh)
Illustrious Member Admin
Joined: 7 years ago
Posts: 6147
 

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
(@astgh)
Illustrious Member Admin
Joined: 7 years ago
Posts: 6147
 

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: 6 years ago
Posts: 18
Topic starter  

@astghik

Cool will look into this soon πŸ™‚
Thanks for the quick reply and follow up!


   
PatrickD
(@patrickd)
Member Customer
Joined: 6 years ago
Posts: 18
Topic starter  

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: 6 years ago
Posts: 18
Topic starter  

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
(@astgh)
Illustrious Member Admin
Joined: 7 years ago
Posts: 6147
 

@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: 6 years ago
Posts: 18
Topic starter  

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: 6 years ago
Posts: 18
Topic starter  

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:
Scroll to top