Esempio di un possibile file functions.php in tema Worpdress
require_once(__DIR__.'/helpers/migrate-seeds.php');
require_once(__DIR__.'/helpers/MKCatalogTheme_Walker_Nav_Menu.php');
//Agginta scripts e style
function mkcatalogotheme_scripts() {
wp_enqueue_style( 'bootstrap', get_template_directory_uri().'/assets/libs/bootstrap/bootstrap.min.css');
wp_enqueue_style( 'style', get_template_directory_uri().'/style.css');
wp_enqueue_script('jquery');
wp_enqueue_script('bootstrap', get_template_directory_uri().'/assets/libs/bootstrap/bootstrap.bundle.min.js', array(), '1.0', true);
wp_enqueue_script('mkcatalogtheme-custom-script', get_template_directory_uri().'/assets/js/scripts.js', array('jquery'), '1.0', true);
}
add_action('wp_enqueue_scripts', 'mkcatalogotheme_scripts');
function mkcatalogotheme_register_theme_support() {
//Title tag support
add_theme_support('title-tag');
//Registrazione del menu
register_nav_menus( [
'mkcatalogtheme_menu_header' => 'MK Menu Header',
'mkcatalogtheme_menu_footer' => 'MK Menu footer'
] );
//Registra logo
add_theme_support( 'custom-logo', [
'height' => 80,
'width' => 230,
'flex-width' => false,
'flex-height' => false,
'header-text' => '',
'unlink-homepage-logo' => false, // Dalla WP 5.5
] );
//Header home imagine
add_theme_support( 'custom-header', array(
'default-image' => '',
'random-default' => false,
'width' => 0,
'height' => 0,
'flex-height' => false,
'flex-width' => false,
'default-text-color' => '', // вызывается функций get_header_textcolor()
'header-text' => true,
'uploads' => true,
'wp-head-callback' => '',
'admin-head-callback' => '',
'admin-preview-callback' => '',
'video' => false, // с 4.7
'video-active-callback' => 'is_front_page', // с 4.7
) );
//Attivazione di miniatura dei post, page, CPT
add_theme_support('post-thumbnails');
}
add_action( 'after_setup_theme', 'mkcatalogotheme_register_theme_support' );
//Registrazione customizer
function mkcatalogotheme_customizer_init( WP_Customize_Manager $wp_customize ){
$panel = 'mkcatalogotheme_panel';
$section = 'mkcatalogotheme_section';
$transport = 'refresh';
$setting = 'mkcatalogotheme';
$wp_customize->add_panel($panel, [
'title' => 'Impostazioni del tema',
'priority' => 10,
'description' => 'Parametri del tema'
]);
$wp_customize->add_section( $section, [
'title' => 'Home page',
'priority' => 200,
'description' => 'Parametri del tema',
'panel' => $panel
] );
//Testo di slogan
$wp_customize->add_setting( $setting.'_slogan', [
'default' => 'Trova la tua auto per <br> il prossimo evento',
'transport' => $transport
] );
$wp_customize->add_control( $setting.'_slogan',
[
'label' => 'Slogan',
'type' => 'text',
'section' => $section,
'settings' => $setting.'_slogan'
]
);
//Titolo del buttone
$wp_customize->add_setting( $setting.'_titolo_buttone', [
'default' => 'Vedi Catalogo',
'transport' => $transport
] );
$wp_customize->add_control( $setting.'_titolo_buttone',
[
'label' => 'Titolo di buttone',
'type' => 'text',
'section' => $section,
'settings' => $setting.'_titolo_buttone'
]
);
//URL buttone
$wp_customize->add_setting( $setting.'_url_buttone', [
'default' => '#mk-catalogo',
'transport' => $transport
] );
$wp_customize->add_control( $setting.'_url_buttone',
[
'label' => 'URL buttone',
'type' => 'url',
'section' => $section,
'settings' => $setting.'_url_buttone'
]
);
//Descrizione biscotto 1
$wp_customize->add_setting( $setting.'_titolo_biscotto_1', [
'default' => 'Perche scegliere noi?',
'transport' => $transport
] );
$wp_customize->add_control( $setting.'_titolo_biscotto_1',
[
'label' => 'Titolo biscotto 1',
'type' => 'text',
'section' => $section,
'settings' => $setting.'_titolo_biscotto_1'
]
);
$wp_customize->add_setting( $setting.'_descrizione_biscotto_1', [
'default' => 'Abiamo parco auto eccelente con le auto ben tenute e piacevole da guidare.',
'transport' => $transport
] );
$wp_customize->add_control( $setting.'_descrizione_biscotto_1',
[
'label' => 'Descrizione biscotto 1',
'type' => 'textarea',
'section' => $section,
'settings' => $setting.'_descrizione_biscotto_1'
]
);
//Descrizione biscotto 2
$wp_customize->add_setting( $setting.'_titolo_biscotto_2', [
'default' => 'Cosa offriamo?',
'transport' => $transport
] );
$wp_customize->add_control( $setting.'_titolo_biscotto_2',
[
'label' => 'Titolo biscotto 2',
'type' => 'text',
'section' => $section,
'settings' => $setting.'_titolo_biscotto_2'
]
);
$wp_customize->add_setting( $setting.'_descrizione_biscotto_2', [
'default' => "Noleggio a breve termine. Quindi non sei obbligato a tenere l'auto per un lungo periodo.",
'transport' => $transport
] );
$wp_customize->add_control( $setting.'_descrizione_biscotto_2',
[
'label' => 'Descrizione biscotto 2',
'type' => 'textarea',
'section' => $section,
'settings' => $setting.'_descrizione_biscotto_2'
]
);
//Descrizione biscotto 3
$wp_customize->add_setting( $setting.'_titolo_biscotto_3', [
'default' => 'Dove ci troviamo?',
'transport' => $transport
] );
$wp_customize->add_control( $setting.'_titolo_biscotto_3',
[
'label' => 'Titolo biscotto 3',
'type' => 'text',
'section' => $section,
'settings' => $setting.'_titolo_biscotto_3'
]
);
$wp_customize->add_setting( $setting.'_descrizione_biscotto_3', [
'default' => 'Siamo in provincia di Verona, poco distante dal centro.',
'transport' => $transport
] );
$wp_customize->add_control( $setting.'_descrizione_biscotto_3',
[
'label' => 'Descrizione biscotto 3',
'type' => 'textarea',
'section' => $section,
'settings' => $setting.'_descrizione_biscotto_3'
]
);
//Descrizione section
$wp_customize->add_setting( $setting.'_section_1', [
'default' => 'Catalogo',
'transport' => $transport
] );
$wp_customize->add_control( $setting.'_section_1',
[
'label' => 'Sezione 1',
'type' => 'text',
'section' => $section,
'settings' => $setting.'_section_1'
]
);
$wp_customize->add_setting( $setting.'_section_2', [
'default' => 'Contattaci',
'transport' => $transport
] );
$wp_customize->add_control( $setting.'_section_2',
[
'label' => 'Section 2',
'type' => 'text',
'section' => $section,
'settings' => $setting.'_section_2'
]
);
//Contact information
$wp_customize->add_setting( $setting.'_email', [
'default' => 'info@supercar.it',
'transport' => $transport
] );
$wp_customize->add_control( $setting.'_email',
[
'label' => 'Email',
'type' => 'text',
'section' => $section,
'settings' => $setting.'_email'
]
);
$wp_customize->add_setting( $setting.'_tel', [
'default' => '030 300 200 999',
'transport' => $transport
] );
$wp_customize->add_control( $setting.'_tel',
[
'label' => 'Telefono',
'type' => 'text',
'section' => $section,
'settings' => $setting.'_tel'
]
);
$wp_customize->add_setting( $setting.'_adress', [
'default' => 'via allegri 48, Verona (VR)',
'transport' => $transport
] );
$wp_customize->add_control( $setting.'_adress',
[
'label' => 'Indirizzo',
'type' => 'text',
'section' => $section,
'settings' => $setting.'_adress'
]
);
//Mappa
$wp_customize->add_setting( $setting.'_mappa', [
'default' => '<iframe src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d44802.53836667013!2d10.95133186961076!3d45.42630318378852!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x477f5f68699be0e3%3A0x53f85a636882595b!2sVerona%20VR!5e0!3m2!1sit!2sit!4v1772220050731!5m2!1sit!2sit" width="100%" height="500" style="border:0;" allowfullscreen="" loading="lazy" referrerpolicy="no-referrer-when-downgrade"></iframe>
',
'transport' => $transport
] );
$wp_customize->add_control( $setting.'_mappa',
[
'label' => 'Mappa',
'type' => 'text',
'section' => $section,
'settings' => $setting.'_mappa'
]
);
//footer
$wp_customize->add_section( $section.'_footer', [
'title' => 'Page footer',
'priority' => 200,
'description' => 'Informazioni in footer',
'panel' => $panel
] );
//Testo di slogan
$wp_customize->add_setting( $setting.'_footer', [
'default' => 'Copyright © 2026',
'transport' => $transport
] );
$wp_customize->add_control( $setting.'_footer',
[
'label' => 'Testo del footer',
'type' => 'textarea',
'section' => $section.'_footer',
'settings' => $setting.'_footer'
]
);
}
add_action( 'customize_register', 'mkcatalogotheme_customizer_init' );
//Custom Post Type Con Taxonomy
function mkcatalogotheme_register_name() {
$labels = array(
'name' => __( 'Items', 'mkcatalogotheme' ),
'singular_name' => __( 'Item', 'mkcatalogotheme' ),
'add_new' => _x( 'Add New Item', 'mkcatalogotheme', 'mkcatalogotheme' ),
'add_new_item' => __( 'Add New Item', 'mkcatalogotheme' ),
'edit_item' => __( 'Edit Item', 'mkcatalogotheme' ),
'new_item' => __( 'New Item', 'mkcatalogotheme' ),
'view_item' => __( 'View Item', 'mkcatalogotheme' ),
'search_items' => __( 'Search Items', 'mkcatalogotheme' ),
'not_found' => __( 'No Items found', 'mkcatalogotheme' ),
'not_found_in_trash' => __( 'No Items found in Trash', 'mkcatalogotheme' ),
'parent_item_colon' => __( 'Parent Item:', 'mkcatalogotheme' ),
'menu_name' => __( 'Items', 'mkcatalogotheme' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'description',
'taxonomies' => array(),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_admin_bar' => true,
'menu_position' => null,
'menu_icon' => null,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => true,
'capability_type' => 'post',
'supports' => array(
'title',
'editor',
'thumbnail',
'excerpt'
),
);
register_post_type( 'mkitem', $args );
}
add_action( 'init', 'mkcatalogotheme_register_name' );
//Taxonomy
function mkcatalogotheme_taxonomies_name() {
$labels = array(
'name' => _x( 'Catalogo', 'Catalogo', 'mkcatalogotheme' ),
'singular_name' => _x( 'Catalogo', 'Catalogo', 'mkcatalogotheme' ),
'search_items' => __( 'Search Catalogo', 'mkcatalogotheme' ),
'popular_items' => __( 'Popular Catalogo', 'mkcatalogotheme' ),
'all_items' => __( 'All Catalogo', 'mkcatalogotheme' ),
'parent_item' => __( 'Parent Catalogo', 'mkcatalogotheme' ),
'parent_item_colon' => __( 'Parent Catalogo', 'mkcatalogotheme' ),
'edit_item' => __( 'Edit Catalogo', 'mkcatalogotheme' ),
'update_item' => __( 'Update Catalogo', 'mkcatalogotheme' ),
'add_new_item' => __( 'Add New Catalogo', 'mkcatalogotheme' ),
'new_item_name' => __( 'New Catalogo', 'mkcatalogotheme' ),
'add_or_remove_items' => __( 'Add or remove Catalogo', 'mkcatalogotheme' ),
'choose_from_most_used' => __( 'Choose from most used Catalogo', 'mkcatalogotheme' ),
'menu_name' => __( 'Catalogo', 'mkcatalogotheme' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'show_in_nav_menus' => true,
'show_admin_column' => false,
'hierarchical' => true,
'show_tagcloud' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => true,
'query_var' => true,
'capabilities' => array(),
);
register_taxonomy( 'mkcatalogo', array( 'mkitem' ), $args );
}
add_action( 'init', 'mkcatalogotheme_taxonomies_name', 5 );
//Aggiungere meta a taxonomy mkcatalogo
add_action('mkcatalogo_edit_form_fields', function($term) {
$headerimage = get_term_meta($term->term_id, 'headerimage', true);
$headertitle = get_term_meta($term->term_id, 'headertitle', true);
$headerdescription = get_term_meta($term->term_id, 'headerdescription', true);
?>
<tr class="form-field">
<th>
<label for="headerimage">Header Immagine</label>
</th>
<td>
<input type="text" name="headerimage" id="headerimage"
value="<?php echo $headerimage; ?>">
</td>
</tr>
<tr class="form-field">
<th>
<label for="headertitle">Header Title</label>
</th>
<td>
<input type="text" name="headertitle" id="headertitle"
value="<?php echo $headertitle; ?>">
</td>
</tr>
<tr class="form-field">
<th>
<label for="headerdescription">Header Descrizione</label>
</th>
<td>
<textarea rows="10" name="headerdescription" id="headerdescription"><?php echo $headerdescription; ?></textarea>
</td>
</tr>
<?php
}, 50);
add_action('edited_mkcatalogo', function($term_id) {
if (isset($_POST['headerimage'])) {
update_term_meta(
$term_id,
'headerimage',
sanitize_text_field($_POST['headerimage'])
);
}
if (isset($_POST['headertitle'])) {
update_term_meta(
$term_id,
'headertitle',
sanitize_text_field($_POST['headertitle'])
);
}
if (isset($_POST['headerdescription'])) {
update_term_meta(
$term_id,
'headerdescription',
sanitize_text_field($_POST['headerdescription'])
);
}
});
//Metabox del post_type = mkitem
function mkcatalogotheme_theme_add_metabox() {
add_meta_box(
'attribut_id_metabox',
$title = 'Immagine di testata',
$callback='mkcatalogotheme_add_metabox_callback',
$screen = array('mkitem'),
$context = 'advanced',
$priority = 'default',
$callback_args = null
);
}
function mkcatalogotheme_add_metabox_callback($post, $meta) {
$screens = $meta['args'];
$headerimage = get_post_meta($post->ID, 'headerimage', true);
wp_nonce_field( 'my_nonce_action', 'my_nonce_name' );
?>
<tr class="form-field">
<th>
<label for="headerimage">URL di Immagine</label>
</th>
<td>
<input type="text" name="headerimage" id="headerimage" style="width: 400px;" value="<?=$headerimage?>">
</td>
</tr>
<?php
}
add_action('add_meta_boxes', 'mkcatalogotheme_theme_add_metabox');
function mkcatalogotheme_save_postdata( $post_id ) {
if ( ! isset( $_POST['headerimage'] ) )
return;
if ( ! wp_verify_nonce( $_POST['my_nonce_name'], 'my_nonce_action') )
return;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
return;
if( ! current_user_can( 'edit_post', $post_id ) )
return;
$my_data = sanitize_text_field( $_POST['headerimage'] );
// Update data in Database
update_post_meta( $post_id, 'headerimage', $my_data );
}
add_action( 'save_post', 'mkcatalogotheme_save_postdata' );
//Form ajax chiamate
function wp_ajax_send_mail() {
$subject = get_bloginfo('name');
$to = get_bloginfo('admin_email');
$message = '<h2>Nuova richiesta</h2>';
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'From: Sito Web <'.$to.'>'
);
$message .= '<p><strong>Nome:</strong> '.$_POST['nome'].'</p>';
$message .= '<p><strong>Email:</strong> '.$_POST['email'].'</p>';
$message .= '<p><strong>Telefono:</strong> '.$_POST['telefono'].'</p>';
$message .= '<p><strong>Città:</strong> '.$_POST['citta'].'</p>';
$message .= '<p><strong>Modello:</strong> '.$_POST['modello'].'</p>';
$message .= '<p><strong>Anni Patente:</strong> '.$_POST['patente_anni'].'</p>';
$message .= '<p><strong>Inizio Noleggio:</strong> '.date('d/m/Y', strtotime($_POST['inizio'])).'</p>';
$message .= '<p><strong>Fine Noleggio:</strong> '.date('d/m/Y', strtotime($_POST['fine'])).'</p>';
$message .= '<p><strong>Messaggio:</strong> '.$_POST['nota'].'</p>';
//echo json_encode(['res' => $message]);exit();
if(wp_mail($to, $subject, $message, $headers)) {
echo json_encode(['res' => 'success']);
}
else {
echo json_encode(['res' => 'error']);
}
wp_die();
}
add_action('wp_ajax_nopriv_send_mail', 'wp_ajax_send_mail');
add_action('wp_ajax_send_mail', 'wp_ajax_send_mail');