Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sabrysuleiman/b730a0efa7fb4dfc55d1d4f0532b7534 to your computer and use it in GitHub Desktop.
Save sabrysuleiman/b730a0efa7fb4dfc55d1d4f0532b7534 to your computer and use it in GitHub Desktop.
integrate Carbon Fields with Rank Math
# Load Your JS File Properly
add_action( 'admin_enqueue_scripts', function( $hook ) {
if ( $hook === 'post.php' || $hook === 'post-new.php' ) {
wp_enqueue_script(
'my-rankmath-carbon-js',
plugin_dir_url( __FILE__ ) . 'js/rankmath-carbon.js',
[ 'wp-hooks' ],
null,
true
);
}
});
# rankmath-carbon.js
(function ($) {
console.log("hi");
wp.hooks.addFilter('rank_math_content', 'nileCruisesDeals/carbonfields', function (content) {
console.log("Original content:", content);
let combinedContent = '';
if (typeof tinymce !== 'undefined' && tinymce.editors.length > 0) {
tinymce.editors.forEach(function (editor) {
if (editor && editor.id && editor.id.startsWith('cf-')) { // cf- is default prefix by Carbon Fields
const instance = tinymce.get(editor.id);
if (instance && typeof instance.getContent === 'function') {
const text = instance.getContent({ format: 'text' });
console.log('WYSIWYG content from', editor.id, ':', text);
combinedContent += ' ' + text;
}
}
});
}
return content + ' ' + combinedContent.trim();
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment