0

There.Hope all of you have a wonderful weekend. I am currently developing a wp plugins and having problems of how to save array of data using its settings API.I know i can do this

<select name = my_plugin_options[mode]>

but how can I do when my_plugin_options[mode] = array('high','low')?Can I do like below? And I try it but it seems doesn't work.Anyone could advise me would be appreciated.Cheers,Jun

<select name = my_plugin_options[mode][high]>
  • What does "doesn't work" mean? How is the array structured when you are receiving it? – fuxia May 13 '18 at 5:35
  • Hi,@fuxia♦ .It can work and before i made a mistake. – jun May 16 '18 at 6:47
1

It would be good if you added your code to see more or less what you want to do.

You can not pass it that way, because when using the Settings API you must add fields of a single value and are registered with the function add_settings_field().

I do not understand very well what you want to do but if you need to create a two-dimensional or multidimensional array using the value in the name attribute in the following way with its options to save:

// You add the corresponding labels, if I'm not wrong, you should have it.
<select name="my_plugin_options[mode][low]">
    <option value="low">Low</option>
    // or true
    <option value="true">Low</option>
</select>

<select name="my_plugin_options[mode][high]">
    <option value="high">High</option>
    // or true
    <option value="true">High</option>
</select>  

You would obtain the value in the following way:

<?php

    $my_plugin_options = get_option( 'my_plugin_options' );
    var_dump( $my_plugin_options[ 'mode' ] );
    // array( 'high' => 'high' ) or array( 'high' => 'true' )
    // or
    // array( 'low' => 'low' ) or array( 'low' => 'true' )

    echo $my_plugin_options[ 'mode' ][ 'high' ]; // 'high' or 'true'
    echo $my_plugin_options[ 'mode' ][ 'low' ]; // 'low' or 'true'

    // Example:

    if( $my_plugin_options[ 'mode' ][ 'high' ] == 'high' ) { // Actions }
    if( $my_plugin_options[ 'mode' ][ 'high' ] == 'true' ) { // Actions }
    // or
    if( $my_plugin_options[ 'mode' ][ 'low' ] == 'low' )  { // Actions }
    if( $my_plugin_options[ 'mode' ][ 'low' ] == 'true' ) { // Actions }

?>  

If you could add your test code, it would be better for me to help you.

or

<select name="my_plugin_options[mode]">
    <option value="low">Low</option>
    <option value="high">High</option>
</select>

<?php

    $my_plugin_options = get_option( 'my_plugin_options' );
    var_dump( $my_plugin_options[ 'mode' ] ); // 'high' or 'low'

    echo $my_plugin_options[ 'mode' ]; // 'low' or 'high'

    // Example:

    if( $my_plugin_options[ 'mode' ] == 'high' ) { // Actions }
    if( $my_plugin_options[ 'mode' ] == 'low' )  { // Actions }

I recommend using the Options API with the function add_option() or update_option().

Example:

<?php  

// You receive the data and save it
update_option( 'my_plugin_options', $_POST[ 'mode' ] );

$my_plugin_options = get_option( 'my_plugin_options' );

echo $my_plugin_options[ 'mode' ]; // 'low' or 'high'

If you could add your test code, it would be better for me to help you.

  • Thanks @Gilbert Rodríguez.I have made a mistake before and two dimension array works well in the name attribute – jun May 16 '18 at 6:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.