Often Schema is already in the standard functionality of any SEO plugin, which is additionally installed on the site. Small customizations, and everything is ready. But there is a nuance! Firstly, on the free version of the plugin, the ability to edit the scheme is not available, and secondly, it uses the most standard (common) types of schemes Organization, Article, Product, etc.
In that case, it makes sense to make our own Schemu, which will contain all the necessary information we think we need.
For Schema generation there is a low-functional service from Google Markup-helper, but in our case I actively use Chatgpt, it generates the necessary type of Schema markup very well.
Where to start?
Add the following lines to the functions.php file. The All in One SEO plugin is taken as an example.
add_filter( 'aioseo_schema_disable', 'aioseo_disable_schema' );
function aioseo_disable_schema( $disabled ) {
return true;
}
An additional field will appear in the body of the page and article where Schema markup should be added.
function add_custom_schema_metabox() {
add_meta_box(
'custom_schema_metabox',
'Field for new Schema',
'render_custom_schema_metabox',
array('post', 'page'), // Post types to which the metabox is attached
'normal', // Metabox position (normal, advanced, side)
'high' // Metabox priority (high, core, default, low)
);
}
add_action('add_meta_boxes', 'add_custom_schema_metabox');
function render_custom_schema_metabox($post) {
// Retrieve the meta field value if it already exists
$schema_code = get_post_meta($post->ID, '_custom_schema_code', true);
// Display the input field for the schema code
echo '';
}
function save_custom_schema_code($post_id) {
// Check if the request is an autosave
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
return;
}
// Verify user permissions
if (!current_user_can('edit_post', $post_id)) {
return;
}
// Check if the field value was submitted
if (isset($_POST['custom_schema_code'])) {
// Save the field value in the meta field, allowing safe HTML tags
update_post_meta($post_id, '_custom_schema_code', wp_kses_post($_POST['custom_schema_code']));
}
}
add_action('save_post', 'save_custom_schema_code');
function output_custom_schema_json() {
// Get the current post object
global $post;
// Retrieve the meta field value with the schema code
$schema_code = get_post_meta($post->ID, '_custom_schema_code', true);
// Check if a value exists
if ($schema_code) {
// Output the JSON code in a ';
}
}
add_action('wp_head', 'output_custom_schema_json');
General scheme for all pages of the site can be used basic types that cover the main aspects of the site and its content, for example WebSite, WebPage, Organization.
The following code adds a common schema for the Russian and Ukrainian version of the site (add the code again in functions.php)
For your convenience, we have placed a Schema generator on our website, with its most popular types.
function add_schema_script() {
if (function_exists('icl_object_id')) {
$current_language = apply_filters('wpml_current_language', NULL);
// Russian version
if ($current_language == 'ru') {
?>