Skip to content

Instantly share code, notes, and snippets.

@psaikali
Created June 4, 2015 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psaikali/ec21bd5264f3b1f90591 to your computer and use it in GitHub Desktop.
Save psaikali/ec21bd5264f3b1f90591 to your computer and use it in GitHub Desktop.
Ajouter des champs aux produits WooCommerce : les fonctions helper à disposition pour générer les inputs. Résultat : http://img.saika.li/bWk6
/*************************************************************************************************
* On ajoute des champs de test pour se repérer, dans l'onglet Général
* Résultat visuel : http://img.saika.li/bWk6
*************************************************************************************************/
function msk_add_test_field_data() {
echo '<div style="background:#f8fbca; padding:1em;">';
echo '<h4>Testons les différents types de champs</h4>';
// Champ de type text
woocommerce_wp_text_input(
array(
'id' => 'input_text',
'label' => __('Champ de type "text"', 'msk'),
'placeholder' => __('Placeholder du champ text', 'msk'),
'description' => __('La description peut apparaître dans une infobulle si "desc_tip" est sur "true".', 'msk'),
'desc_tip' => true // Si "true", la description s'affichera en infobulle
)
);
// Champ de type hidden
woocommerce_wp_hidden_input(
array(
'id' => 'input_hidden',
'label' => __('Champ de type "hidden"', 'msk'),
'value' => 'valeur-du-champ-hidden',
)
);
// Champ de type textarea
woocommerce_wp_textarea_input(
array(
'id' => 'input_textarea',
'label' => __('Champ de type "textarea"', 'msk'),
'class' => 'widefat',
'placeholder' => __('Placeholder du champ textarea', 'msk'),
'description' => __('<br>La description n\'apparaîtra pas dans une infobulle si "desc_tip" est sur "false" ou inexistant.', 'msk'),
'custom_attributes' => array( // Un tableau d'attributs personnalisés qui seront ajoutés au champ en question
'data-test' => 50,
'data-other-test' => 'Lorem ipsum'
)
)
);
// Champ de type checkbox
woocommerce_wp_checkbox(
array(
'id' => 'input_checkbox',
'label' => __('Champ de type "checkbox"', 'msk'),
'value' => 'yes', // La valeur enregistrée de la checkbox
'cbvalue' => 'yes', // La valeur de la checkbox en question. Si elle est la même que "value", la checkbox sera cochée
)
);
// Champ de type select
woocommerce_wp_select(
array(
'id' => 'input_select',
'label' => __('Champ de type "select"', 'msk'),
'options' => array( // Un tableau avec les options (value et nom) du select
'value1' => 'Am',
'value2' => 'Stram',
'value3' => 'Gram',
),
'value' => 'value2', // La valeur enregistrée, choisie, de la select
)
);
// Champ de type radio
woocommerce_wp_radio(
array(
'id' => 'input_radio',
'label' => __(' ', 'msk'),
'options' => array( // Un tableau avec les options (value et nom) du radio
'value4' => 'Pif',
'value5' => 'Paf',
'value6' => 'Pouf',
),
'value' => 'value5', // La valeur enregistrée, choisie, de la radio
)
);
echo '</div>';
}
add_action('woocommerce_product_options_general_product_data', 'msk_add_test_field_data');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment