Created
July 28, 2025 14:14
-
-
Save sabrysuleiman/b730a0efa7fb4dfc55d1d4f0532b7534 to your computer and use it in GitHub Desktop.
integrate Carbon Fields with Rank Math
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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