• Hi all,

    I’m currently developing a testimonial plugin. The testimonials are to be stored in an array.
    I have an settings page where the admin is to add new testimonials. However it saves the form as an array into $option. I cant seem to find a way to push new data in that array it just save one array.

    He is the code for the setting page:

    `
    function JWidget_settings_init( ) {

    register_setting( ‘pluginPage’, ‘JWidget_settings’ );

    add_settings_section(
    ‘JWidget_pluginPage_section’,
    __( ‘Add a widget’, ‘wordpress’ ),
    ‘JWidget_settings_section_callback’,
    ‘pluginPage’
    );

    add_settings_field(
    ‘img’,
    __( ‘Image URL:’, ‘wordpress’ ),
    ‘JWidget_text_field_0_render’,
    ‘pluginPage’,
    ‘JWidget_pluginPage_section’
    );

    add_settings_field(
    ‘url’,
    __( ‘Name’, ‘wordpress’ ),
    ‘JWidget_text_field_1_render’,
    ‘pluginPage’,
    ‘JWidget_pluginPage_section’
    );

    add_settings_field(
    ‘rev’,
    __( ‘Rating:’, ‘wordpress’ ),
    ‘JWidget_select_field_2_render’,
    ‘pluginPage’,
    ‘JWidget_pluginPage_section’
    );

    add_settings_field(
    ‘txt’,
    __( ‘Review’, ‘wordpress’ ),
    ‘JWidget_textarea_field_3_render’,
    ‘pluginPage’,
    ‘JWidget_pluginPage_section’
    );

    }

    function JWidget_text_field_0_render( ) {

    $options = get_option( ‘JWidget_settings’ );
    ?>
    <input type=’text’ name=’JWidget_settings[img]’ value='<?php echo $options[‘img’]; ?>’>
    <?php

    }

    function JWidget_text_field_1_render( ) {

    $options = get_option( ‘JWidget_settings’ );
    ?>
    <input type=’text’ name=’JWidget_settings[url]’ value='<?php echo $options[‘url’]; ?>’>
    <?php

    }

    function JWidget_select_field_2_render( ) {

    $options = get_option( ‘JWidget_settings’ );
    ?>
    <select name=’JWidget_settings[rev]’>
    <option value=’1′ <?php selected( $options[‘rev’], 1 ); ?>>4/5</option>
    <option value=’2′ <?php selected( $options[‘rev’], 2 ); ?>>5/5/option>
    </select>

    <?php

    }

    function JWidget_textarea_field_3_render( ) {

    $options = get_option( ‘JWidget_settings’ );
    ?>
    <textarea cols=’40’ rows=’5′ name=’JWidget_settings[txt]’>
    <?php echo $options[‘txt’]; ?>
    </textarea>
    <?php

    }

    function JWidget_settings_section_callback( ) {

    echo __( ‘Add a new plugin to be randomly generated. All fields must be filled out before submiting.’, ‘wordpress’ );

    }

    function JWidget_options_page( ) {

    ?>
    <form action=’options.php’ method=’post’>

    <h2>JWidget</h2>

    <?php
    settings_fields( ‘pluginPage’ );
    do_settings_sections( ‘pluginPage’ );
    submit_button();
    ?>

    </form>
    <?php

    }

    ?>

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Yes, the Settings API assumes anything from a form is to override the previous values, not add to it. You’ll need to get rid of the settings fields and display the plugin’s menu item page as plain PHP/HTML and handle storing the values yourself. Then you can add values to the existing array so what’s stored is an array of arrays.

    The next problem is how to selectively delete or edit previous testimonials. Your plugin page will probably need a sort of pagination to populate the fields with different testimonials by clicking ‘next’ or ‘previous’ plus an ‘Add new’ button or similar. This could be done with javascript unless there are a lot of testimonials. Then some sort of AJAX interface is probably in order.

Viewing 1 replies (of 1 total)
  • The topic ‘Pushing data into the $options array from the settings page’ is closed to new replies.