Give an ID automatically for my widget, and save it in a metabox WordPress

I created a widget called “info widget”, which has a field, here is the code of the widget:

<?php

add_action('widgets_init','info_init');

//register my widget

function info_init()
{
    register_widget("info_widget");
}

// widget class

class info_widget extends WP_widget 
{
    // Widget Settings

    function info_widget()
    {
        $option = array
        (
            "classname" => "style1",
            "description" => "widget information"
        );
        $this->WP_widget("info-widget","widget d'information",$option);
    }

     // display the widget

    function widget($args,$instance)
    {
        extract($args);
        echo $before_widget;
        echo $before_title.$instance["name"].$after_title;
        echo $after_widget;
    }

     //the form for the widget options

        function form($instance)
    {
        $def = array
        (
            "name" =>"Enter your name"
        );

        $instance = WP_parse_args($instance,$def);

        echo '<p><label for="'.$this->get_field_id("name").'">name :</label>
            <input value="'.$instance["name"].'" type="text" name="'.$this->get_field_name("name").'" id="'.$this->get_field_id("name").'">
        </p>';
    }


     // update the widget  
        function update($new,$old)
    {
        return $new;
    }
}

in the “widget” menu when I slide my widget to the sidebar the field of widget get automatically an ID, and every time I slide again my widget, the field takes a new ID ( the ID is incremented)

Read More

for me I created a metabox, and I got my widget in the metabox with his field. Code of metabox :

<?php

add_action('add_meta_boxes','dar_meta_create');

function dar_meta_create()
{
    add_meta_box(  
        'dar_meta', // $id  
        'metabox dardar', // $title   
        'dar_meta_function', // $callback  
        'page', // $page  
        'advanced', // $context  
        'high');
}
// callback
function dar_meta_function($post)
{
    wp_nonce_field(basename(__FILE__), 'dar_nonce');

        //Display My widget

    global $wp_widget_factory;

    $widget = new info_widget(); // create my widget
    $widget->id = 'temp';
    $widget->number = '2'; // give an ID automatically ?????
    ob_start();
    $widget->form(array());
    $form = ob_get_clean();

    // Convert the widget field naming into ones that panels uses
    $exp = preg_quote($widget->get_field_name('____'));
    $exp = str_replace('____', '(.*?)', $exp);
    $form = preg_replace('/'.$exp.'/', "2", $form);

    $widget->form = $form;
    var_dump($widget->form);

}

}
// save meta box
function dar_meta_save($id,$post)
{
// check the autosave
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
        return $id; 

            //Save My Widget here ???

}
add_action('save_post','dar_meta_save',1,2);

In the “ADD new page” menu the output of my metabox is :

string '<p><label for="widget-info-widget-2-name">Nom :</label>
            <input value="Enter your name" type="text"
name="widgets[2][name]" id="widget-info-widget-2-nom">      </p>'
(length=164)

but I gave the ID manually ( ID = 2)

how can I automatically give an ID for each field of a widget and save it so that I can display it in the page ?? some one has an idea ?

I hope I don’t ask many Questions 🙂 and thanks.

Related posts

Leave a Reply