Support Forum
Support › Meta Box for Yoast SEO › MB for Yoast not counting words
Hey guys!
I have two different scenarios. On the first one, I load my metaboxes accordingly to the post being edited, grabbing from the theme settings which box should be loaded for each specific post type (it's part my my theme settings panel):
$post_id = null;
if ( isset( $_GET['post'] ) ) {
$post_id = intval( $_GET['post'] );
} elseif ( isset( $_POST['post_ID'] ) ) {
$post_id = intval( $_POST['post_ID'] );
}
$post_type = get_post_type($post_id);
After getting the post_id on the edit, screen, I query my THEME OPTIONS and load only the boxes assigned to the current CPT. Everything works like a charm, however, the 'add_to_wpseo_analysis' => true' gets ignored.
So, I moved to the second scenario and loaded a simple textarea metabox on the traditional way, as a text.
$cpt = 'post';
$meta_boxes[] = array(
'id' => 'meta-'.$cpt.'-test',
'title' => 'Test',
'pages' => array($cpt),
'context' => 'side',
'priority' => 'low',
'fields' => array(
array(
'id' => $cpt.'-textarea-test',
'type' => 'textarea',
'desc' => __('Ex: Estúdio Teia Local', 'tl'),
'add_to_wpseo_analysis' => true,
),
)
);
...and it is not counted as well.
Any ideas?
Hello again!
I found a tutorial on YOAST page that made the counter work for me:
/* global YoastSEO */
class MyCustomDataPlugin {
constructor() {
// Ensure YoastSEO.js is present and can access the necessary features.
if ( typeof YoastSEO === "undefined" || typeof YoastSEO.analysis === "undefined" || typeof YoastSEO.analysis.worker === "undefined" ) {
return;
}
YoastSEO.app.registerPlugin( "MyCustomDataPlugin", { status: "ready" } );
this.registerModifications();
}
/**
* Registers the addContent modification.
*
* @returns {void}
*/
registerModifications() {
const callback = this.addContent.bind( this );
// Ensure that the additional data is being seen as a modification to the content.
YoastSEO.app.registerModification( "content", callback, "MyCustomDataPlugin", 10 );
}
/**
* Adds to the content to be analyzed by the analyzer.
*
* @param {string} data The current data string.
*
* @returns {string} The data string parameter with the added content.
*/
addContent( data ) {
data += "Hello, I'm some additional data!";
return data ;
}
}
/**
* Adds eventlistener to load the plugin.
*/
if ( typeof YoastSEO !== "undefined" && typeof YoastSEO.app !== "undefined" ) {
new MyCustomDataPlugin();
} else {
jQuery( window ).on(
"YoastSEO:ready",
function() {
new MyCustomDataPlugin();
}
);
}
But I have no idea what should I put there on the line — addContent(data).
The example shows a hardcoded text. How can I put there a METABOX FIELD ID, for example?
Any suggestions?
Thanks!
Well...
Found a workaround using LOCALIZE SCRIPT and injecting the content of my metabox as a VAR inside the JS.
function my_admin_personal_scripts() {
if (in_array('wordpress-seo/wp-seo.php', apply_filters('active_plugins', get_option('active_plugins')))) {
global $post;
$countable_content = get_post_meta($post->ID, 'my-super-duper-metabox-id', true);
wp_register_script('yoast-counting-words', MY_JS_PATH.'/misc/injected-yoast-counter.js');
$content_found = array('metabox'=> $countable_content);
wp_localize_script( 'yoast-counting-words', 'content_found', $content_found );
wp_enqueue_script( 'yoast-counting-words' );
}
}
add_action('admin_enqueue_scripts', 'my_admin_personal_scripts');
And, inside the JS, I retrieve the content this way:
(...)
addContent( data ) {
data += content_found.metabox;
//console.log(content_found.metabox);
return data ;
}
(...)
It could be useful to someone else!
G.