Ajouter plusieurs champs personnalisés à la page des paramètres généraux
2 réponses
- votes
-
- 2014-01-10
Ehbien,le deuxièmebit de codeesttechniquement labonnefaçon de lefaire. Cependant,à lafin de
add_settings_field()
,vouspouvezpasser des arguments.Veuillez consulter la référence de lafonction WordPress Add_Settings_Field . Cela vous aidera àmieux comprendre lefonctionnement de lafonction
add_settings_field()
.Cela dit,vouspouvez utiliser unefonction 'partagée' pour votre rappel. Commeje lefais dansmapage d'options lorsqueje développe desthèmes.
Voici unexemple de lafaçon dontje lefais.
// My Example Fields add_settings_field( 'tutorial_display_count', 'Tutorial Display Count', 'ch_essentials_textbox_callback', 'ch_essentials_front_page_option', 'ch_essentials_front_page', array( 'tutorial_display_count' // $args for callback ) ); add_settings_field( 'blog_display_count', 'Blog Display Count', 'ch_essentials_textbox_callback', 'ch_essentials_front_page_option', 'ch_essentials_front_page', array( 'blog_display_count' // $args for callback ) ); // My Shared Callback function ch_essentials_textbox_callback($args) { $options = get_option('ch_essentials_front_page_option'); echo '<input type="text" id="' . $args[0] . '" name="ch_essentials_front_page_option[' . $args[0] . ']" value="' . $options['' . $args[0] . ''] . '"></input>'; }
Ilfaudra unpeu depersonnalisationpour répondre à vosbesoins,maisfaire unefonctionpartagéepour vos rappels économiserabeaucoup d'espaceen termes de code. Àpart cela,vous lefaites correctementtel quel.
--Modifier--
Ok,voici ce que ça devrait êtrepourtoi ..il suffit demodifier le code aubesoin,je l'ai écrit à la volée .. Je l'aitestépour vérifier,et cela afonctionné. Il vous suffit demodifier le ou les
add_settings_field
en fonction de vosbesoins. Si vous avezbesoin d'en ajouter d'autres,copiez-collez-en unet modifiez-le. Assurez-vous deregister_setting
sinon celane fonctionnerapas.add_action('admin_init', 'my_general_section'); function my_general_section() { add_settings_section( 'my_settings_section', // Section ID 'My Options Title', // Section Title 'my_section_options_callback', // Callback 'general' // What Page? This makes the section show up on the General Settings Page ); add_settings_field( // Option 1 'option_1', // Option ID 'Option 1', // Label 'my_textbox_callback', // !important - This is where the args go! 'general', // Page it will be displayed (General Settings) 'my_settings_section', // Name of our section array( // The $args 'option_1' // Should match Option ID ) ); add_settings_field( // Option 2 'option_2', // Option ID 'Option 2', // Label 'my_textbox_callback', // !important - This is where the args go! 'general', // Page it will be displayed 'my_settings_section', // Name of our section (General Settings) array( // The $args 'option_2' // Should match Option ID ) ); register_setting('general','option_1', 'esc_attr'); register_setting('general','option_2', 'esc_attr'); } function my_section_options_callback() { // Section Callback echo '<p>A little message on editing info</p>'; } function my_textbox_callback($args) { // Textbox Callback $option = get_option($args[0]); echo '<input type="text" id="'. $args[0] .'" name="'. $args[0] .'" value="' . $option . '" />'; }
Well the second bit of code is technically the correct way to do it. However, at the end of the
add_settings_field()
you can pass arguements.Please view the WordPress Add_Settings_Field function reference. This will help you in getting the best understanding of how the
add_settings_field()
function really works.Now, with that said, you could use a 'shared' function for your callback. Such as I do in my options page when I develop themes.
Here is an example of how I do it.
// My Example Fields add_settings_field( 'tutorial_display_count', 'Tutorial Display Count', 'ch_essentials_textbox_callback', 'ch_essentials_front_page_option', 'ch_essentials_front_page', array( 'tutorial_display_count' // $args for callback ) ); add_settings_field( 'blog_display_count', 'Blog Display Count', 'ch_essentials_textbox_callback', 'ch_essentials_front_page_option', 'ch_essentials_front_page', array( 'blog_display_count' // $args for callback ) ); // My Shared Callback function ch_essentials_textbox_callback($args) { $options = get_option('ch_essentials_front_page_option'); echo '<input type="text" id="' . $args[0] . '" name="ch_essentials_front_page_option[' . $args[0] . ']" value="' . $options['' . $args[0] . ''] . '"></input>'; }
It will take a little bit of customizing to fit your needs, but doing a shared function for your callbacks will save a lot of space in terms of code. Other than that, you are doing it correctly as is.
--Edit--
Ok, this is what it should be like for you.. just modify the code as needed, I wrote this on the fly.. I did test it to check, and it worked. You just need to modify the
add_settings_field
(s) to suit your needs. If you need to add more, just copy and paste one and edit it. Make sure toregister_setting
or it will not work.add_action('admin_init', 'my_general_section'); function my_general_section() { add_settings_section( 'my_settings_section', // Section ID 'My Options Title', // Section Title 'my_section_options_callback', // Callback 'general' // What Page? This makes the section show up on the General Settings Page ); add_settings_field( // Option 1 'option_1', // Option ID 'Option 1', // Label 'my_textbox_callback', // !important - This is where the args go! 'general', // Page it will be displayed (General Settings) 'my_settings_section', // Name of our section array( // The $args 'option_1' // Should match Option ID ) ); add_settings_field( // Option 2 'option_2', // Option ID 'Option 2', // Label 'my_textbox_callback', // !important - This is where the args go! 'general', // Page it will be displayed 'my_settings_section', // Name of our section (General Settings) array( // The $args 'option_2' // Should match Option ID ) ); register_setting('general','option_1', 'esc_attr'); register_setting('general','option_2', 'esc_attr'); } function my_section_options_callback() { // Section Callback echo '<p>A little message on editing info</p>'; } function my_textbox_callback($args) { // Textbox Callback $option = get_option($args[0]); echo '<input type="text" id="'. $args[0] .'" name="'. $args[0] .'" value="' . $option . '" />'; }
-
Donc,ce queje ne comprendspas,ce sont les 4èmeet 5èmeparamètres dans add_settings_field ().Je sais que lepremierest l'ID,le deuxièmeest lenom,letroisièmeest le rappelpour l'afficher,mais à quoi servent les suivants? Vous avez ch_essentials_front_page_optionpour les deux,j'aijuste 'general' aumêmeendroit,le suivantest videet le dernierestmaintenant letableau args. Alorsmaintenant,dans le rappel,vous avezget_option avec cette valeur,maisje ne saispas quoimettre là dansmon cas.So what I dont understand is the 4th and 5th parameter in the add_settings_field(). I know the first one is the ID, second is the name, third is the callback to display it, but what are the next ones for? You have ch_essentials_front_page_option for both of them, I have just 'general' in the same place, next one is empty and the last one is now the args array. So now in the callback you have get_option with that value, but I dont know what to put there in my case.
- 0
- 2014-01-10
- Richard Mišenčík
-
Unemodification a étéfaite,cela devrait vouspermettre de démarrer à 100%.Faites-moi savoir si vous avez desproblèmes ou des questions.Je l'aibeaucoup commenté.Edit has been made, that should get you 100% going. Let me know if you have any problems or questions. I did heavily comment it.
- 2
- 2014-01-11
- MrJustin
-
@MrJusting Mercibeaucoup.Enfait,je l'aifaitfonctionneren regardant votreprofilet en vérifiant une question à laquelle vous avez répondu,"Mettreen œuvre les onglets sur lapage dumenupersonnalisé".C'étaittrèsbien commenté,alorsj'ai comparémon codeet finalement compris comment celafonctionne.Pourmoi,lesparamètres de lapageet de la section étaient déroutants,je pensais que c'était comme une section de lapageet non la section desparamètres.Alorsmaintenant,j'ai combiné les deuxet ajouté une autre valeur à $ args,donc lepremierest l'identifiant du champet le secondest la description,puisj'ai ajouté une autre ligne à lafonction de rappelpourfaire écho à la description avec args [1] :)@MrJusting Thanks a lot. I actually got it working by looking at your profile and checking a questioin that you answered, "Implementing-tabs-on-custom-menu-page". It was very well commented, so then I compared my code and finally understood how it works. For me the page and section parameters were confusing, I though it was like section of the page and not the section of settings. So now I combined both together and added another value to $args, so first is the field id and the second is description, and then added another line to the callback function to echo the description with args[1] :)
- 0
- 2014-01-12
- Richard Mišenčík
-
Super,je suis heureux que vous ayez compris,si vous avez des questions,faites-lemoi savoir.Great, I'm glad you have figured it out, if you have any questions let me know.
- 0
- 2014-01-13
- MrJustin
-
Jepense queje vaisessayermaintenant de créer unepage demenu séparéeet d'y ajouter les options,caren généralet enbas,c'est unpeuperdu.Jete ferai savoir comment ça s'estpasséI think Ill try now to create a separate menu page and add the options there, because in general and at the bottom its kind of lost. I'll let you know how did that go
- 0
- 2014-01-13
- Richard Mišenčík
-
Le code dans celui-ciest-il correct?http://wordpress.stackexchange.com/questions/127493/wordpress-settings-api-implementing-tabs-on-custom-menu-pageIs code in this one okay? http://wordpress.stackexchange.com/questions/127493/wordpress-settings-api-implementing-tabs-on-custom-menu-page
- 0
- 2014-01-13
- Richard Mišenčík
-
Merci @MrJustin.Juste unpoint ànoter,sur quelque chose queje n'aipas vu dans la réponse ...pour afficher l'option dans votrethème,utilisez simplement lafonction `get_option ()`.Exemple: ` Phpechoget_option ('option_1');?>`Thanks @MrJustin. Just a point of note, on something I didn't see in the answer...to display the option in your theme, just use the `get_option()` function. Example: ``
- 0
- 2014-01-14
- NW Tech
-
Cela aparfaitementfonctionnépourmoi.This worked perfectly for me.
- 0
- 2016-01-12
- Jake
-
Ça yest.Lameilleure réponse sur Internet!J'ai cherché de longues heures une solution.Justepour signaler àtous ceux quitraversent cet article,utilisez le commentaire éditéet ajouteztout ce dont vous avezbesoin!This is it. The best answer on the internet! I searched long hours for a solution. Just to point out to everyone crossing this article, use the edited comment, and add anything you need!
- 0
- 2016-09-23
- Mo Alsaedi
-
- 2014-01-09
Lameilleurefaçonest d'utiliser desplugins d'options wordpress.L'un desmeilleursest les champspersonnalisés avancés.
http://www.advancedcustomfields.com/
Si vous achetez unmodule complémentaire depage d'options,vouspouvez créer unepage d'optionsillimitée avec denombreusesfonctionnalités.S'il vousplaît,qu'est-ce qu'une vidéo.
http://www.advancedcustomfields.com/add-ons/options-page/
Pluginet addontrès utiles.
Better way is use a wordpress options plugins. One of the best is Advanced Custom Fields.
http://www.advancedcustomfields.com/
If you buy a option page addon, then you can create a unlimited option page with lot of features. Please what out a video.
http://www.advancedcustomfields.com/add-ons/options-page/
Very usefull plugin and addon.
-
Je veuxjuste ajouter quelques champs,donc unpluginjustepour cela seraitexcessifpourmoi,maismerci.I just want to add a few fields, so plugin just for this would be an overkill for me but thanks.
- 3
- 2014-01-10
- Richard Mišenčík
-
Sans oublier que celane résoutpas ce que l'OP voulait,à savoir ajouter des champs auxparamètresgénéraux.AFAIK,ACFne vouspermetpas d'ajouter des champs auxparamètresgénéraux.Not to mention this doesn't solve what the OP wanted, which was to add fields to the General Settings. AFAIK, ACF doesn't allow you to append fields tot he General Settings.
- 8
- 2014-01-14
- NW Tech
Ce queje voudraisfaire,c'est ajouter quelques champspersonnalisés auxparamètresgénéraux. C'est le code quej'utilise. Celafonctionnebien maisje ne saispas comment ajouterplus de champs.
Je voudrais créer deux champspour l'instant,unpour lenuméro detéléphoneet le secondpour l'adresse:
La seulefaçon dontj'ai réussi àfairefonctionnerplusieurs champs était detout dupliquer.
Alors ça ressemblerait à ceci:
Mais cen'estprobablementpas lameilleurefaçon de lefaire,j'aiessayé de créer une
settings_section
mais celan'atout simplementpasfonctionné oun'apasenregistré,etc. C'estjustetrès déroutant.