In this tutorial, you will learn how to integrate Custom fields present in your theme or plugin with the Rank Math SEO plugin.
Using this content analysis API, Rank Math will start analyzing data present in these fields.
Steps to register custom fields in Rank Math’s content analysis API:
1 Create a .js File
Create a new JS file in your Plugin or Theme and add the content analysis API related code in it. Make sure to use wp-hooks
and rank-math-analyzer
dependency.
wp_enqueue_script( 'rank-math-integration', DIR . '/js/rank-math-integration.js', [ 'wp-hooks', 'rank-math-analyzer' ], false, true );
2 Filters to analyze data
Rank Math supports 2 filters to analyze title and content. Following is a syntax to add filter:
wp.hooks.addFilter( 'rank_math_title', 'plugin-name', callback_function_to_fetch_title ) // This will hook into title analysis data.
wp.hooks.addFilter( 'rank_math_content', 'rank-math', this.getContent ) // This will hook into content analysis data.
3 Function to reload the data
– To reload the Title tests.rankMathEditor.refresh( 'title' )
– To reload the Content tests.rankMathEditor.refresh( 'content' )
You can also use this function to reload the analysis when any custom field data is changed.