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
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.Β
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.Β
@astghik
Cool will look into this soon π
Thanks for the quick reply and follow up!
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
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
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) {
Β Β Β Β Β Β Β });
Β Β Β }
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
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)