Skip to content

Instantly share code, notes, and snippets.

@nickdavis
Created February 21, 2019 21:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nickdavis/2d052d77464bd16b3fe20a5df37c1351 to your computer and use it in GitHub Desktop.
Save nickdavis/2d052d77464bd16b3fe20a5df37c1351 to your computer and use it in GitHub Desktop.
Adding Carbon Fields to Yoast SEO content analysis
/**
* Adds specified Carbon Field to the Yoast SEO content analysis.
*
* @link https://return-true.com/adding-content-to-yoast-seo-analysis-using-yoastseojs/
* @link https://kb.yoast.com/kb/can-i-add-data-to-the-page-analysis/
* @link https://github.com/Yoast/YoastSEO.js/blob/master/README.md
* @link https://github.com/Yoast/YoastSEO.js/issues/181#issuecomment-163162489
*/
(function($) {
var MyYoastPlugin = function()
{
YoastSEO.app.registerPlugin('carbonFieldIntroText', {status: 'loading'});
this.getData();
};
MyYoastPlugin.prototype.getData = function()
{
var _self = this,
$text = $('#carbon-field-7');
_self.custom_content = $text.val();
YoastSEO.app.pluginReady('carbonFieldIntroText');
YoastSEO.app.registerModification('content', $.proxy(_self.getCustomContent, _self), 'carbonFieldIntroText', 5);
};
MyYoastPlugin.prototype.getCustomContent = function (content)
{
return this.custom_content + content;
};
$(window).on('YoastSEO:ready', function ()
{
new MyYoastPlugin();
});
})(jQuery);
<?php
/**
* Carbon Fields and Yoast SEO modifications
*
* @package NickDavis\Theme\Admin
* @since 1.0.0
* @author Nick Davis
* @link https://iamnickdavis.com
* @license GNU General Public License 2.0+
*/
namespace NickDavis\Theme\Admin;
add_action( 'admin_enqueue_scripts', __NAMESPACE__ . '\enqueue_yoast_seo_analysis_carbon_fields_script' );
/**
* Adds selected Carbon Fields to Yoast SEO content analysis.
*
* @since 1.0.0
*
* @link https://return-true.com/adding-content-to-yoast-seo-analysis-using-yoastseojs/
* @link https://kb.yoast.com/kb/can-i-add-data-to-the-page-analysis/
* @link https://github.com/Yoast/YoastSEO.js/blob/master/README.md
* @link https://github.com/Yoast/YoastSEO.js/issues/181#issuecomment-163162489
*/
function enqueue_yoast_seo_analysis_carbon_fields_script() {
wp_enqueue_script( 'yoast-seo-carbon-fields', CHILD_THEME_URL . '/assets/js/admin/yoast-seo-carbon-fields.js', array( 'jquery', 'yoast-seo-admin-script' ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment