Stai cercando un modo per regolare le impostazioni di WordPress all'attivazione del tema? Questo snippet cambierà le impostazioni predefinite di WordPress quando il tema viene attivato. Questo snippet elimina anche il post, la pagina e il commento predefiniti che vengono creati quando WordPress viene installato.
Istruzioni:
- Tutto quello che devi fare è aggiungere questo codice al file functions.php del tuo tema.
- Modifica le impostazioni nello snippet per adattarle alle tue esigenze.
add_action( 'after_setup_theme', 'the_theme_setup' );
function the_theme_setup()
{
// First we check to see if our default theme settings have been applied.
$the_theme_status = get_option( 'theme_setup_status' );
// If the theme has not yet been used we want to run our default settings.
if ( $the_theme_status !== '1' ) {
// Setup Default WordPress settings
$core_settings = array(
'avatar_default' => 'mystery', // Comment Avatars should be using mystery by default
'avatar_rating' => 'G', // Avatar rating
'comment_max_links' => 0, // We do not allow links from comments
'comments_per_page' => 20 // Default to 20 comments per page
);
foreach ( $core_settings as $k => $v ) {
update_option( $k, $v );
}
// Delete dummy post, page and comment.
wp_delete_post( 1, true );
wp_delete_post( 2, true );
wp_delete_comment( 1 );
// Once done, we register our setting to make sure we don't duplicate everytime we activate.
update_option( 'theme_setup_status', '1' );
// Lets let the admin know whats going on.
$msg = '
<div class="error">
<p>The ' . get_option( 'current_theme' ) . 'theme has changed your WordPress default <a href="' . admin_url() . 'options-general.php" title="See Settings">settings</a> and deleted default posts & comments.</p>
</div>';
add_action( 'admin_notices', $c = create_function( '', 'echo "' . addcslashes( $msg, '"' ) . '";' ) );
}
// Else if we are re-activing the theme
elseif ( $the_theme_status === '1' and isset( $_GET['activated'] ) ) {
$msg = '
<div class="updated">
<p>The ' . get_option( 'current_theme' ) . ' theme was successfully re-activated.</p>
</div>';
add_action( 'admin_notices', $c = create_function( '', 'echo "' . addcslashes( $msg, '"' ) . '";' ) );
}
}
Nota: Se questa è la prima volta che aggiungi snippet di codice in WordPress, consulta la nostra guida su come copiare / incollare correttamente snippet di codice in WordPress, in modo da non rompere accidentalmente il tuo sito.
Se ti è piaciuto questo snippet di codice, prendi in considerazione la possibilità di consultare i nostri altri articoli sul sito come: Come creare correttamente una pagina di accesso personalizzata in WordPress e 11 migliori plugin WordPress per scrittori.
Cos'altro possiamo fare con questo?
Molto bello. Tuttavia, possiamo cambiare l'impostazione per la struttura dei permalink?