jeudi 25 février 2016

WP Multiple Checklist Warnings & Errors

I've been trying to fix this error for a while with no luck. Maybe is the context of my program which makes it very challenging for me.

I created a simple plugin (settings API / Page Options) for Wordpress in which the user has access to multiple checkboxes to initialize the TinyMCE editor. It saves the selected checkboxes in an $options['array'] and then I use that data in my function to initialize the TinyMCE Editor. Everything works great with the exception of the Notices, Warnings outputted in my front end.

Here is how my Settings Page is built (it has multiple instances of this, but this should give an idea of the basic functionality.)

function mmm_select_providers(  ) {

    $options = get_option( 'mmm_settings' );

    $buttons = array(
        'Format' => 'formatselect',
        'Bold' => 'bold',
        'Italic' => 'italic',
        'Underline' => 'underline',
        'Superscript' => 'superscript',
        'Align Left' => 'alignleft',
        'Align Center' => 'aligncenter',
        'Align Right' => 'alignright',
        'Bullet List' => 'bulletlist',
        'Number List' => 'numberlist',
        'Link' => 'link',
        'Unlink' => 'unlink'
    );

    foreach ($buttons as $key => $value) { ?>
        <input name="mmm_settings[mmm_select_providers_check][]" type="checkbox" value='<?php echo $value; ?>' <?php if((isset($options['mmm_select_providers_check']) && in_array($value, $options['mmm_select_providers_check']))) { echo "checked"; } ?>/>
        <label for="<?php echo $value ?>"><?php echo $key; ?></label>
    <?php }
}

Keep in mind some "checkbox" won't be selected by the user. After saving the data I can then go to my TinyMCE function and gather the $options['mmm_select_providers_check'] and perform some basic string operations to format the output the way I need it:

$global_toolbar = implode(', ', array_values($options['mmm_select_providers_check']));

It gives me the correct output with no errors and it works in the TinyMCE function (it initializes it with the selected toolbar buttons). Here is the snippet:

$in['toolbar1'] = $providers_toolbar;

Locally, the plugin works fine but when I deploy to Dev -- it gives me a string of notices and warnings such as:

Notice: Undefined index: 

Warning: array_values() expects parameter 1 to be array, null given in

Warning: implode(): Invalid arguments passed in 

Warning: array_values() expects parameter 1 to be array, null given in

And so on ! Keep in mind I'm using a lot of instances of this sample code, gathering info for many WP Editors. The code posted is just to establish context.

Does this makes sense? I tried everything -- filter_array, isset and other suggestions -- but I can't seem to get rid of the errors. What I think is happening is that the checkbox is saving data even if the checks are not selected and then causing some conflict.

Please let me know if anybody can help! I'm not that new with PHP but I am new at WP Plugins / Settings API / Page Options

Thanks!




Aucun commentaire:

Enregistrer un commentaire