Skip to content

Instantly share code, notes, and snippets.

@rahilwazir
Last active May 2, 2016 08:59
Show Gist options
  • Save rahilwazir/6026cbe1bca7cb6c75553cc198a4b20c to your computer and use it in GitHub Desktop.
Save rahilwazir/6026cbe1bca7cb6c75553cc198a4b20c to your computer and use it in GitHub Desktop.
Yoast Subtitle Analyzer
/**
* Source: https://wordpress.org/plugins/meta-box-yoast-seo
*/
/* global jQuery, DS1_fields */
// My custom metabox subtitle input id
var DS1_fields = ['acf-field_54da15f9ad3c2'];
(function (fields) {
var module = {
// Initialize
init: function() {
jQuery(window).on('YoastSEO:ready', module.load);
},
// Load plugin and add hooks.
load: function() {
YoastSEO.app.registerPlugin('ds1_subtitle', {status: 'ready'});
YoastSEO.app.registerModification('title', module.addContent, 'ds1_subtitle');
module.bind();
},
// Add content to Yoast SEO Analyzer.
addContent: function(content) {
fields.map(function(fieldId) {
content += ' ' + document.getElementById(fieldId).value;
});
return content;
},
// Update Yoast SEO analyzer when fields are updated.
// Use debounce technique, which triggers refresh only when keys stop being pressed.
bind: function() {
var timeout;
function refresh() {
clearTimeout(timeout);
timeout = setTimeout(function() {
YoastSEO.app.refresh();
}, 250);
}
fields.map(function(fieldId) {
document.getElementById(fieldId).addEventListener('keyup', refresh);
});
}
};
module.init();
})(DS1_fields);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment