Un cliente mi ha chiesto questa funzionalità al quanto insolita ma allo stesso tempo interessante. Voleva che ogni per articolo creato sul post standard di wordpress avesse il prefisso news nello slug (post_title). Ho fatto una ricerca ed ho trovato una soluzione sul forum di toolset, che riporto qui con la speranza che torni utile a qualcuno.
function sg_update_post_name($post_id, $post, $update)
{
remove_action('save_post', 'sg_update_post_name', 30, 3);
if ($post->post_type == 'post') {
wp_update_post(array(
'ID' => $post_id,
'post_name' => "news-" . sanitize_title($post->post_title)
));
}
add_action('save_post', 'sg_update_post_name', 30, 3);
}
add_action('save_post', 'sg_update_post_name', 30, 3);
function sg_change_post_name_slug($permalink, $post_id, $title, $name, $post)
{
if (defined('DOING_AJAX') && DOING_AJAX and $_REQUEST['action'] == 'sample-permalink') {
if ($post->post_type == 'post') {
$permalink[1] = "news-" . sanitize_title($title);
}
}
return $permalink;
}
add_filter('get_sample_permalink', 'sg_change_post_name_slug', 10, 5);