0

I'm a relative beginner, so anything that points me in the right direction is appreciated.

I'm trying to whitelist options from the admin panel. I have an array in another file (item-whitelist.php) that contains the whitelisted items, and I would like to be able to add to that array by entering new items on a custom options page. I created an options page with a textarea field for the user to input new items, but I'm unsure of how to go about adding input options to the array in the other file.

I'm assuming I would post the input items to the database and read from the database in the other file, but I've spent a while trying to figure out how to do this and haven't had much success.

functions.php :

<?php
add_action( 'admin_menu', 'mp_add_admin_menu' );
add_action( 'admin_init', 'mp_settings_init' );

function mp_add_admin_menu(  ) { 
    add_options_page( 'my-plugin', 'my-plugin', 'manage_options', 'my-plugin', 'mp_options_page' );
}

function mp_settings_init(  ) { 
    register_setting( 'pluginPage', 'mp_settings' );
    add_settings_section(
        'mp_pluginPage_section', 
        __( 'Your section description', 'wordpress' ), 
        'mp_settings_section_callback', 
        'pluginPage'
    );
    add_settings_field( 
        'mp_textarea_field_0', 
        __( 'Settings field description', 'wordpress' ), 
        'mp_textarea_field_0_render', 
        'pluginPage', 
        'mp_pluginPage_section' 
    );
}

function mp_textarea_field_0_render(  ) { 
    $options = get_option( 'mp_settings' );
    ?>
    <textarea cols='40' rows='5' name='mp_settings[mp_textarea_field_0]'> 
        <?php echo $options['mp_textarea_field_0']; ?>
    </textarea>
    <?php
}

function mp_settings_section_callback(  ) { 
    echo __( 'This section description', 'wordpress' );
}

function mp_options_page(  ) { 
    ?>
    <div class="wrap">
    <form action='options.php' method='post'>
        <h1>my-plugin Settings</h1>
        <?php
        settings_fields( 'pluginPage' );
        do_settings_sections( 'pluginPage' );
        submit_button();
        ?>
    </form>
    </div>
    <?php
}

?>
0

I think I understand what you're trying to accomplish, but correct me if this is off base.

You have an array that being specified in another file. You want users to be able to append values to that default array by using an options form.

Here's what I would do: When you're preparing this array in your other file, merge your default array with whatever the user sets, if anything. That way the user will never be able to modify your initial array, but can still contribute to it.

Next, you'll use the options form to store the user array to the database. The final product will look like this:

<?php
// Your preset array. These are your default values.
$whitelist = array( 'your items here' );
// Get the user array from your options. 
// This is where the options form stores these values.
$mp_settings = get_option( 'mp_settings', false );
// Check to see if you have any values, if not set an empty array.
$user_whitelist = isset( $mp_settings['mp_textarea_field_0'] ) ? $mp_settings['mp_textarea_field_0'] : array();
// Merging these array together gives you a unified whitelist.
$whitelist = array_merge( $whitelist, $user_whitelist );

Note: this what you'll add to your other file, the one you didn't share in this question.

  • 1
    Thanks! This is exactly what I was trying to accomplish. I should have posted the relevant section of my other file to make the question more clear, but I pretty much just had an array that I wanted the user to be able to append to using an options form. – tpsReports Mar 14 at 4:28

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.