Skip to content

Instantly share code, notes, and snippets.

@sc0ttkclark
Created February 9, 2022 17:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sc0ttkclark/4b050dc9586d21e2654f2822c4073285 to your computer and use it in GitHub Desktop.
Save sc0ttkclark/4b050dc9586d21e2654f2822c4073285 to your computer and use it in GitHub Desktop.
WordPress.org plugins and themes topic saved replies Tampermonkey script. Click 'raw' to install it.
// ==UserScript==
// @name WordPress.org plugins and themes topic saved replies
// @namespace https://skc.dev/
// @version 0.1
// @description Add saved replies to topic reply forms.
// @author Scott Kingsley Clark
// @match https://wordpress.org/support/topic/*
// @match https://*.wordpress.org/support/topic/*
// @grant none
// ==/UserScript==
(function( jQuery ) {
'use strict';
/*
* Custom list of saved replies should be defined here.
*
* Every reply needs a key and should provide a title and content.
*
* The saved replies list is automatically generated from this list.
*/
const savedReplies = {
siteHealth: {
title: 'Ask for Site Health Information',
content: 'Could you please provide your Site Health information to help us determine what might be causing this problem? To get that, you can go to your WordPress dashboard area and go to: Tools > Site Health > Info, then Press the "Copy site info to clipboard" button, and paste the results here.'
}
};
// Bypass this code if the bbPress form isn't found.
if ( ! jQuery( '.bbp-form' )[0] ) {
return;
}
// Add the Saved Replies select field above the reply content field.
jQuery( '.bbp-form div.bbp-the-content-wrapper' ).prepend( '<select id="skc-dotorg-saved-replies"><option value="">-- Choose a Saved Reply --</option></select>' );
const $savedReplies = jQuery( '#skc-dotorg-saved-replies' ),
$replyContentField = jQuery( '#bbp_reply_content' );
// Build the list of saved replies.
Object.keys( savedReplies ).forEach( key => {
$savedReplies.append( '<option value="' + key + '">' + savedReplies[ key ].title + '</option>' );
} );
// Handle adding the saved reply content to the reply textarea.
$savedReplies.on( 'change', function() {
let savedReply = $savedReplies.val();
if ( '' === savedReply || 'undefined' === typeof savedReplies[ savedReply ] ) {
return;
}
let savedReplyContent = savedReplies[ savedReply ].content;
// Add the reply to the content.
if ( '' === $replyContentField.val() ) {
$replyContentField.val( savedReplyContent );
} else {
$replyContentField.val( $replyContentField.val() + '\n\n' + savedReplyContent );
}
// Reset the select after adding content.
$savedReplies.val( '' );
} );
})( jQuery );
@sc0ttkclark
Copy link
Author

Please forgive my incredibly lazy jQuery stuff here. It's not meant to be perfect, it's just supposed to do the thing it needs to do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment