✅ Как использовать:
- Вставь код в
functions.php темы.
- Перейди в браузере на
https://your-site.com/?generate_descriptions=1.
- Описания будут сгенерированы и сохранены (и выведены на экран).
- После использования удали или закомментируй код, чтобы не вызывался при каждом заходе.
add_action('init', function () {
if (!is_admin() && isset($_GET['generate_descriptions']) && $_GET['generate_descriptions'] === '1') {
$args = [
'post_type' => 'models',
'posts_per_page' => -1,
'post_status' => 'publish',
];
$query = new WP_Query($args);
$spintax = ******************************;
while ($query->have_posts()) {
$query->the_post();
$post_id = get_the_ID();
$name = get_the_title($post_id);
$description = generateFromSpintax($spintax, $name);
update_post_meta($post_id, 'rank_math_description', $description); // или другое поле
echo "<p><strong>$name</strong>: $description</p>";
}
wp_reset_postdata();
exit;
}
});
// Спинтакс-генератор
function generateFromSpintax($spintax, $name) {
if (empty($spintax)) {
return "Ошибка: пустой спинтакс";
}
$processSpintax = function ($text) use (&$processSpintax) {
while (preg_match('/{([^{}]*)}/', $text, $matches)) {
$options_str = $matches[1];
$options = array_filter(explode('|', trim($options_str)), 'strlen');
$replacement = $options ? $options[array_rand($options)] : '';
$text = str_replace($matches[0], $replacement, $text);
}
return $text;
};
return str_replace('NAME', $name, $processSpintax($spintax));
}