Stile incorporato per jquery-ui-datepicker

27 gen 2013, 15:44:47
Visualizzazioni: 25K
Voti: 12

Voglio utilizzare il datepicker incluso in WordPress nel frontend di un sito web. Ho accodato jquery-ui-datepicker ma il datepicker non è formattato (nessun errore js nella console). Esiste un corrispondente wp_enqueue_style per questo?

Ho utilizzato questo codice in functions.php

function rr_scripts() {
  wp_enqueue_script( 'jquery' );
  wp_enqueue_script( 'jquery-ui-datepicker', array( 'jquery' ) );

  wp_register_style( 'bootstrap_css', get_template_directory_uri() . '/assets/css/bootstrap.min.css' );
  wp_enqueue_style( 'bootstrap_css' ); # Sto usando Twitter Bootstrap come CSS (se può essere rilevante)
}
add_action( 'wp_enqueue_scripts', 'rr_scripts' );
0
Tutte le risposte alla domanda 2
2
22

Per quanto ne so, non esiste uno stile predefinito per il datepicker. Devi registrarne uno tuo. Il codice sarà quindi:

function rr_scripts() {
  wp_enqueue_script( 'jquery' );
  wp_enqueue_script( 'jquery-ui-datepicker', array( 'jquery' ) );

  wp_register_style( 'bootstrap_css', get_template_directory_uri() . '/assets/css/bootstrap.min.css' );
  wp_enqueue_style( 'bootstrap_css' ); // Sto usando Twitter Bootstrap come CSS (se è rilevante)

  wp_register_style('jquery-ui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css');
  wp_enqueue_style( 'jquery-ui' );   
}

add_action( 'wp_enqueue_scripts', 'rr_scripts' );
27 gen 2013 16:44:42
Commenti

+1 ma non è necessario registrare lo script e poi caricarlo separatamente. Combina tutto in un'unica operazione, come: wp_enqueue_style( 'bootstrap_css', get_template_directory_uri() . '/assets/css/bootstrap.min.css' );

random_user_name random_user_name
14 gen 2017 19:19:21

Ero bloccato nella parte CSS ma mi hai salvato la giornata. Grazie

Frank Frank
13 lug 2019 12:37:56
0

Per caricare script e stile, aggiungi il seguente codice al file functions.php del tuo tema.

function add_e2_date_picker(){
// File jQuery UI date picker
wp_enqueue_script('jquery-ui-datepicker');
// File CSS del tema jQuery UI
wp_enqueue_style('e2b-admin-ui-css','http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/base/jquery-ui.css',false,"1.9.0",false);
}
add_action('wp_enqueue_scripts', 'add_e2_date_picker'); 

Script per uso in back-end:

function add_e2_date_picker(){
// File jQuery UI date picker
wp_enqueue_script('jquery-ui-datepicker');
// File CSS del tema jQuery UI
wp_enqueue_style('e2b-admin-ui-css','http://ajax.googleapis.com/ajax/libs/jqueryui/1.9.0/themes/base/jquery-ui.css',false,"1.9.0",false);
}
add_action('admin_enqueue_scripts', 'add_e2_date_picker'); 

Devo agganciarmi a options-general.php per visualizzare in Impostazioni->Date Picker. Basta inserire questo codice nuovamente nel file functions.php sotto il codice precedente.

function register_datepiker_submenu() {
    add_submenu_page( 'options-general.php', 'Date Picker', 'Date Picker', 'manage_options', 'date-picker', 'datepiker_submenu_callback' );
}

function datepiker_submenu_callback() { ?>

    <div class="wrap">

    <input type="text" class="datepicker" name="datepicker" value=""/>

    </div>

    <script>
    jQuery(function() {
        jQuery( ".datepicker" ).datepicker({
            dateFormat : "dd-mm-yy"
        });
    });
    </script> 

<?php }
add_action('admin_menu', 'register_datepiker_submenu');

?>

Per maggiori dettagli, consulta Aggiungi un jQuery Date Picker a un tema o plugin WordPress

25 feb 2015 07:04:33