Ajouter des widgets par programme aux barres latérales
-
-
Vous devriez y ajouter votre réponsepour répondre à votrepropre question :)You should add your answer down there to answer your own question :)
- 6
- 2011-08-22
- helenhousandi
-
Pour unbon aperçu des widgets de labarre latérale,consultez cet article: http://justintadlock.com/archives/2010/11/08/sidebars-in-wordpress.For a great rundown on sidebar widgets, check out this article: http://justintadlock.com/archives/2010/11/08/sidebars-in-wordpress.
- 0
- 2011-08-22
- Joshua
-
Surveillez leparamètre d'action de l'appel ajax quiesteffectué lorsqu'un widgetest ajouté,puis recherchez le code lié à cette action ajax hooket voyez comment cela sefait dans lenoyau.Facile!;)Monitor the action parameter of ajax call that is made when a widget is added, and then find the code related to that action ajax hook and see how its done in core. Simple! ;)
- 0
- 2012-04-09
- Ashfame
-
Veuillez renvoyer votre solutionen tant que réponseet l'accepter comme "la" réponse à votreproblème.Please re-post your solution as an answer and accept it as "the" answer to your problem.
- 5
- 2012-04-11
- EAMann
-
http://browse-tutorials.com/tutorial/how-create-widget-wordpresshttp://browse-tutorials.com/tutorial/how-create-widget-wordpress
- 0
- 2016-03-02
- ram4nd
-
2 réponses
- votes
-
- 2014-08-19
Tout d'abord,merci à @toschopour la réponse détaillée.
Voici unexemple simplepour ceux qui recherchent une solution simpleet des options de widgetpar défaut:
$active_sidebars = get_option( 'sidebars_widgets' ); //get all sidebars and widgets $widget_options = get_option( 'widget_name-1' ); $widget_options[1] = array( 'option1' => 'value', 'option2' => 'value2' ); if(isset($active_sidebars['sidebar-id']) && empty($active_sidebars['sidebar-id'])) { //check if sidebar exists and it is empty $active_sidebars['sidebar-id'] = array('widget_name-1'); //add a widget to sidebar update_option('widget_name-1', $widget_options); //update widget default options update_option('sidebars_widgets', $active_sidebars); //update sidebars }
Remarque 1: Vouspouvez obtenir
sidebar-id
en allant dans lemenu des widgetset eninspectant labarre latérale souhaitée. Lepremier<div id="widgets-holder-wrap">
<div>
enfant a lesidebar-id
.Note 2: Vouspouvez obtenir le
widget_name
en allant dans lemenu des widgetset eninspectant le widget recherché. Vous verrez quelque chose comme<div id="widget-6_widget_name-__i__" class="widget ui-draggable">
.Je souhaite que cela aide.
First of all, thanks to @toscho for the detailed answer.
This is a simple example for those who are searching for a simple solution and default widget options:
$active_sidebars = get_option( 'sidebars_widgets' ); //get all sidebars and widgets $widget_options = get_option( 'widget_name-1' ); $widget_options[1] = array( 'option1' => 'value', 'option2' => 'value2' ); if(isset($active_sidebars['sidebar-id']) && empty($active_sidebars['sidebar-id'])) { //check if sidebar exists and it is empty $active_sidebars['sidebar-id'] = array('widget_name-1'); //add a widget to sidebar update_option('widget_name-1', $widget_options); //update widget default options update_option('sidebars_widgets', $active_sidebars); //update sidebars }
Note 1: You can get
sidebar-id
going to widgets menu and inspecting the wanted sidebar. The first<div id="widgets-holder-wrap">
's<div>
child has thesidebar-id
.Note 2: You can get the
widget_name
going to widgets menu and inspecting the wanted widget. You'll see something like<div id="widget-6_widget_name-__i__" class="widget ui-draggable">
.I wish it helps.
-
- 2012-05-04
Voici commentprocéder:
(ATTENTION,celapourrait SUPPRIMERtous les widgetsprécédents si vousn'avezpas remis les widgets d'origine dans letableau
widgets
.)$widgets = array( 'middle-sidebar' => array( 'widget_name' ), 'right-sidebar' => array( 'widget2_name-1' ) ); update_option('sidebars_widgets', $widgets);
Le -numberpeut être utilisé si vous souhaitez ajouter ultérieurement des options au widget avec quelque chose comme ceci:
update_option('widget_widget_name', array( 1 => array( 'title' => 'The tile', 'number' => 4 ), '_multiwidget' => 1 ));
This is how you do it:
(WARNING, this could REMOVE all previous widgets if you did not put back the original widgets into the
widgets
array.)$widgets = array( 'middle-sidebar' => array( 'widget_name' ), 'right-sidebar' => array( 'widget2_name-1' ) ); update_option('sidebars_widgets', $widgets);
The -number can be used if you later want to add options to the widget with something like this:
update_option('widget_widget_name', array( 1 => array( 'title' => 'The tile', 'number' => 4 ), '_multiwidget' => 1 ));
-
NE SUIVEZ PAS CECI,JE NE PEUX PAS ÉVALUER CECITOUS MES WIDGETS ONT DISPARU APRÈS L'UTILISATION DE CE CODE.DON'T FOLLOW THIS, I CAN'T RATE THIS DOWN. ALL MY WIDGETS DISAPPEARED AFTER USING THIS CODE.
- 1
- 2014-07-11
- EresDev
-
Vous devez d'abord obtenir letableau de widgetsexistant,sinon vous leseffacereztous comme le commentaire ci-dessus.`$ widgets=get_option ('sidebars_widgets');`You need to get the existing widgets array first otherwise you'll erase them all like the above comment stated. `$widgets = get_option( 'sidebars_widgets' );`
- 0
- 2016-11-11
- cowgill
Je voudrais ajouterparprogramme des widgets àmes deuxbarres latérales quej'ai.Jen'aitrouvé aucunmoyen officiel de lefaire?
J'ai commencé à chercher dans labase de données.J'aitrouvé que c'est l'option 'sidebars_widgets' quiplace les widgets sur lesbarres latérales.Lorsque vous regardez les options,lesnoms des widgets ont unnuméro ajouté à lafin comme: widget_name-6.D'où vient cenombre?
Uneidée sur lafaçon de résoudre ceproblème?