To create custom assessments for Yoast SEO, you can follow these steps:
1. Create a Custom Assessment Class:
- Extend the `Assessment` class provided by Yoast SEO.
- Implement the `getResult()` method, which takes a `Paper` object, a `Researcher` object, and an `i18n` object as parameters.
- This method should analyze the content and return an `AssessmentResult` object with a score and feedback text.
2. Register the Custom Assessment:
- Create a custom web worker that can hook into Yoast SEO.
- Use the `_worker.registerAssessment()` method to add the custom assessment to Yoast SEO's analysis.
3. Handle Errors:
- Wrap your code in a try-catch block to handle any unexpected errors that may occur during the assessment.
- Log the error to help with debugging.
- Provide a fallback assessment result with a low score and an error message to inform the user about the issue.
Here is a sample code snippet for a custom assessment:
javascript
import { AssessmentResult, Assessment } from 'yoastseo';
export default class CustomAssessment extends Assessment {
constructor(settings) {
super();
this.settings = settings;
}
getResult(paper, researcher, i18n) {
// Check for the occurrence of the word Yoast in the title.
const title = paper.getTitle();
const matches = (title.match(/Yoast/gi) || []).length;
const assessmentResult = new AssessmentResult();
const { score, text } = this.score(matches);
assessmentResult.setScore(score);
assessmentResult.setText(text);
return assessmentResult;
}
}
And here is how you can register the custom assessment:
javascript
const customWorker = new CustomWorker();
customWorker.register();
const customAssessment = new CustomAssessment(settings);
this._worker.registerAssessment("customAssessment", customAssessment, "MyCustomAssessmentPlugin");
By following these steps and handling any potential errors, you can create custom assessments that analyze specific aspects of a post, page, or term and provide actionable feedback to improve the content.
Citations:[1] https://developer.yoast.com/customization/yoast-seo/adding-custom-assessments/
[2] https://developer.yoast.com/customization/yoast-seo/adding-custom-data-analysis/
[3] https://yoast.com/help/customize-your-yoast-seo-experience/
[4] https://yoast.com/yoast-seo-snippets-to-customize-your-site/
[5] https://www.elegantthemes.com/blog/wordpress/yoast-seo-plugin