Unyson icon indicating copy to clipboard operation
Unyson copied to clipboard

How can I add unyson option to widgets and retrieve their values?

Open danyj opened this issue 11 years ago • 80 comments

Could you please show me best possible way to add any unsyon option to widgets? I would like to add list of custom class names that user can pick to display different widgets style/ It is a simple select with list of class names.

This is what I have in mind but with unyson options http://prntscr.com/76c7vg

danyj avatar May 17 '15 19:05 danyj

@danyj at the widget_form_callback hook you can run fw()->backend->render_options($options, $values) and at the widget_update_callback you must save your data to the database. Hope this helps.

llemurya avatar May 18 '15 05:05 llemurya

thnx for the reply but is there any way that you can show it with code sample with one option so I can built on it.

danyj avatar May 18 '15 05:05 danyj

I am having trouble with default value and getting it back

danyj avatar May 18 '15 05:05 danyj

this is what I have

add_action('in_widget_form', 'thz_widget_fields');
add_action('widget_update_callback', 'thz_widget_fields_update_callback');
function thz_widget_fields($instance){

    $settings = $instance->get_settings();

    fw_print($settings);

    $options_array = array();


        $options_array[] = array(

            'widget_style' => array(
                'type'  => 'select',
                'label' => __('Select style','fw'),
                'value' => $settings['widget_style'],
                 'choices' => array(
                     'blue' => __('Blue', 'fw'),
                     'red' => __('Red', 'fw'),
                     'green' => __('Green', 'fw'),

                 ),
            ),
        );  


    echo fw()->backend->render_options($options_array, $data['widget_style'], array(
        'id_prefix' => $option["attr"]["id"] . '-',
        'name_prefix' => $option["attr"]["name"]
    ));


}

function thz_widget_fields_update_callback($instance, $new_instance) {
    $instance['widget_style'] = $new_instance['widget_style'];
    return $instance;
}

but it returns null , I think I missed the field names

http://prntscr.com/76hqyx

danyj avatar May 18 '15 06:05 danyj

@danyj I am working on make a demo for you please wait. I'll become with an answer.

llemurya avatar May 18 '15 06:05 llemurya

@danj I make an extension called danj I created 2 files : class-fw-extension-danj.php with the next content

<?php if ( ! defined( 'FW' ) ) {
    die( 'Forbidden' );
}

class FW_Extension_Danj extends FW_Extension {

    /**
     * Called after all extensions instances was created
     * @internal
     */
    protected function _init() {
        // TODO: Implement _init() method.
        add_action( 'widget_form_callback', array( $this, 'form_callback' ) );
        add_filter( 'widget_update_callback', array( $this, 'update_callback' ) );
    }

    public function form_callback( $instance ) {
        $options = array(
            'danj' => array(
                'type'  => 'text',
                'value' => 'default value'
            )
        );

        $values = array( 'danj' => isset( $instance['danj'] ) ? $instance['danj'] : $options['danj']['value'] );

        echo fw()->backend->render_options( $options, $values );
    }

    public function update_callback( $instance ) {
        $instance['danj'] = FW_Request::POST( 'fw_options/danj' );

        return $instance;
    }
}

and manifest.php with the next content

<?php if ( ! defined( 'FW' ) ) {
    die( 'Forbidden' );
}

$manifest = array();

$manifest['name']        = __( 'Danj', 'fw' );
$manifest['description'] = __( 'Danj demo.', 'fw' );
$manifest['version'] = '1.0.0';
$manifest['display'] = true;
$manifest['standalone'] = true;

Here is the video demo how it works. Please try. If it helps, please close the issue.

llemurya avatar May 18 '15 06:05 llemurya

you over extended yourself and thank you much for it. simple name hint would have been enough but this is much better. than you!

danyj avatar May 18 '15 06:05 danyj

@llemurya should name and id for the options be changed? if I use the extension as you made it I end up with multiple options and all with same name and ID

49 instances http://prntscr.com/76jg33

danyj avatar May 18 '15 10:05 danyj

Yes if you want you may change it, the code I wrote is a demo and you can change it how you want.

llemurya avatar May 18 '15 10:05 llemurya

ok , im getting a hang of it. thnx again

danyj avatar May 18 '15 10:05 danyj

@moldcraft @llemurya OK bud , I know you busy that is why I took the task of #542 on my self and it works very well so far . http://prntscr.com/76m08a

my question is this http://wordpress.stackexchange.com/questions/188648/how-to-get-options-of-all-active-widgets/188651#188651

I have that loop and works fine but it just bugs me to loop that much , but now I found this way to get all widgets options

protected function _init() {
    // TODO: Implement _init() method.
    add_action( 'widget_form_callback', array( $this, 'form_callback' ) );
    add_filter( 'widget_update_callback', array( $this, 'update_callback' ) );
    add_filter( 'dynamic_sidebar_params', array($this, 'widget_params'));

}

public function widget_params( $params ) { 
        //fw_print(  $params  );

        $widget_id      = $params[0]['widget_id'];
        $widget_parts   = explode('-',$widget_id);
        $widget_base_id = $widget_parts[0];
        $widget_number  = $widget_parts[1];         
        $options        = get_option('widget_'.$widget_base_id);
        if($options){
            fw_print(  $options[$widget_number]  );
        }


     return $params;
}

and so far I see it works fine http://prntscr.com/76lzdt

but do you think it is a problem to use that filter or the way I extracted the widget options?

danyj avatar May 18 '15 14:05 danyj

I am almost done but there is no way that I can fix those id's and names , each of them must be unique otherwise the widgets dont display titles and their contents get messed up

the form must use in_widget_form

instead

widget_form_callback

but when I do that the widget is not updating

we need to use this somehow

 $instance->get_field_name('option_name');
$instance->get_field_id('option_name');

but I cant get it to work

can you please provide a valid example with options names and ids matching the widget instance

with this select

    $options = array(



        'view_type' => array(
            'type'  => 'select',
            'label' => false,
            'value' => 'hide',
             'choices' => array(
                 'hide' => __('Hide on selected pages', 'fw'),
                 'show' => __('Show on selected pages', 'fw'),
             ),
        ),


    );

danyj avatar May 18 '15 20:05 danyj

please anyone?

danyj avatar May 19 '15 08:05 danyj

unusable to do this as U extension, moved to plugin and everything works out of the box. made simple html options instead unyson options. sad , I wanted to make this unyson extension. http://prntscr.com/76x50a

thnx for the hellp

danyj avatar May 19 '15 10:05 danyj

@danyj I made a demo for you please wait 20 minutes to make a video cast.

llemurya avatar May 19 '15 10:05 llemurya

@llemurya , if you are doing it pay attention to double widget in same widget positon ,

for example the only issue from where everything went down is if you use same widget in same position

for example use text widget twice in same position and you are forced to go native html way because of the widget name and ID

danyj avatar May 19 '15 10:05 danyj

and I also tried with instance->number but you cant hook on it since when widget is firts moved to position that does not exist http://prntscr.com/76x992

just giving pointers where I had the problems

this one is what we need done right since we have to use it

    echo fw()->backend->render_options($options, $values, array(
        'id_prefix' => $data['id_prefix'] . $id . '-',  // native widget way
        'name_prefix' => $data['name_prefix'] . '[' . $id . ']', // native widget way
    ));

danyj avatar May 19 '15 10:05 danyj

@danyj please see this videocast If this is what you want I will share the code.

llemurya avatar May 19 '15 11:05 llemurya

yes , that is the one , all must be unique per each widget as seen in console

danyj avatar May 19 '15 12:05 danyj

@danyj my demo only works with simple option types that didn't have javascript, with javascript exists problems.

<?php if ( ! defined( 'FW' ) ) {
    die( 'Forbidden' );
}

class FW_Extension_Danj extends FW_Extension {

    /**
     * Called after all extensions instances was created
     * @internal
     */
    protected function _init() {
        // TODO: Implement _init() method.
        add_action( 'widget_form_callback', array( $this, 'form_callback' ) );
        add_filter( 'widget_update_callback', array( $this, 'update_callback' ) );
    }

    public function form_callback( $instance ) {

        $options = array(
            'demo1' => array(
                'type'  => 'text',
                'value' => 'default value'
            ),
            'demo2' => array(
                'type'=>'select',
                'choices' => array(
                    'tiger' => 'Tiger',
                    'puma' => 'Puma',
                    'lynx' => 'Lynx',
                    'pantera' => 'Pantera'
                )
            )
        );

        $values = isset( $instance['fw_options'] ) ? $instance['fw_options'] : array();

        echo fw()->backend->render_options( $options, $values );

        return $instance;
    }

    public function update_callback( $instance ) {

        fw()->backend->option_type('color-picker')->enqueue_static();

        $instance['fw_options'] = FW_Request::POST( 'fw_options' );

        return $instance;
    }
}

llemurya avatar May 19 '15 12:05 llemurya