WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Saving Array in Options (3 posts)

  1. inactive
    Member
    Posted 4 years ago #

    Hi there
    I'm trying to build my own SIMPLE gallery script but i'm stuck at saving the options.

    Let me explain the structure of the plugin/gallery

    - one gallery equals one folder within the plugin directory. the folder contains all pictures (full resolution)
    - the metadata to describe the gallery resp. to give the gallery a nice name should be stored in ONE option.

    Now I know that you can read/save an array from/into an option. But how can I display the options array with the administration page options? I can build a form but on submitting the form, it fails...

    I don't know how to update the settings with a form... what am I missing?

    Heres the code

    add_action('admin_menu', 'shellm_gallery_administration');
    
    function shellm_gallery_administration() {
    add_options_page('Shellm Gallery', 'Shellm Gallery', 'manage_options', 'shellm-gallery-identifier', 'shellm_gallery_options');
    add_action('admin_init','register_shellm_gallery');
    }
    
    function register_shellm_gallery() {
    register_setting('shellm-gallery-settings-group','shellm_gallery_metadata');
    }
    function shellm_gallery_options() {
    settings_fields('shellm-gallery-settings-group');
    echo '<div class="wrap">';
    echo '<h2>Shellm Gallery Settings</h2>';
    //testing purpopses
    /*delete_option('shellm_gallery_metadata');
    add_option('shellm_gallery_metadata',
    array(
    "folder_name_1" => array("9th of May 2009","Party Pictures from New York"),
    )
    );*/
    
    if ('update' == $_POST['action']) {
    echo "shouldn't update take place here?";
    echo "<pre>";
    print_r($_POST);
    echo "</pre>";
    }
    
    echo '
    <form method="post" action="options.php">
    <table class="form-table">
    ';
    
    $mdf = get_option("shellm_gallery_metadata");
    $dirlist = opendir(SHELLM_GALLERY_DIR_FULLPATH);
    while ($file = readdir ($dirlist)) {
    	if ($file != '.' && $file != '..') {
    		echo '<tr><td>'.$file.'</td><td><input type="hidden" name="shellm_gallery_metadata[\'keys\'][]" value="'.$file.'"/><input type="text" name="shellm_gallery_metadata[\'date\'][]" value="'.$mdf[$file][0].'" /><input type="text" size="80" name="shellm_gallery_metadata[\'title\'][]" value="'.$mdf[$file][1].'" /></td></tr>';
    	}
    }
    foreach($mdf as $key=>$val) {
    }
    echo '
    </table>
    <input type="hidden" name="action" value="update" />
    <p class="submit">
    <input type="submit" class="button-primary" value="'.__('Save Changes').'" />
    </p>
    </form>
    ';
    
    echo '</div>';
    }

    Thanks a lot in advance for your help!
    Bye
    shellm

  2. Mark / t31os
    Moderator
    Posted 4 years ago #

    See the third parameter of register_setting.
    http://codex.wordpress.org/Function_Reference/register_setting

    Set the third parameter to a callback function, this is the function that accepts your incoming options to be saved, sanitizes them and returns them to WordPress for saving (this is beside the problem though, read on).

    This line prints the form nonce inputs..

    settings_fields('shellm-gallery-settings-group');

    It should be inside the form, not before it (else they don't get submitted along with your options, which is why they aren't saving).

    Example of placing inside form (based on your code above).

    echo '
    <form method="post" action="options.php">
    ';
    
    settings_fields('shellm-gallery-settings-group');
    
    echo '
    <table class="form-table">
    ';

    Hope that helps.. :)

  3. inactive
    Member
    Posted 4 years ago #

    Hey Mark

    Thanks a lot! I just managed to fix my script, it works now :)

    I also had to delete this line (was example code for WP < 2.7)
    <input type="hidden" name="action" value="update" />

Topic Closed

This topic has been closed to new replies.

About this Topic