0

I'm creating my first Wordpress Plugin and want to let the user configure multiple urls which he can name and safe as "channels".

I've set up a settings page and wanted to store the data through add_option() as json like this:

[
{
    "name": "my first saved url",
    "url": "www.example.com"
},
{
    "name": "my second saved url",
    "url": "www.another-example.com"
}
]

I was considering json because there will be more values in the future than only these two.

I'd like to have just some input fields with name and url which i can validate and put together with existing ones and store them away.

By using the Settings-api from wordpress i have the feeling to be quite limited as far as the settings field correspond with only one option.

I also thought about not using the settings-api at all and just define a custom form with input fields and safe them in the end with update_option().

Does anyone have an idea on how to achieve what i want?

my full code is below i'm happy for any thoughts..

class MySettingsPage{

public function __construct() {
add_action( 
  'admin_menu', 
  array( $this, 'init_settings_page' ) 
);

    add_action(
        'admin_init',
        array( &$this, 'init_settings' )
    );
}

public function init_settings_page(){

add_option( 'channels' );

$page_title = 'Settings channels';
$menu_title = 'Channels';
$capability = 'manage_options';
$slug = 'my_saved_urls';
$callback = array( $this, 'plugin_settings_page_content' );
$icon = 'dashicons-admin-plugins';
$position = 30.5;

add_menu_page( $page_title, $menu_title, $capability, $slug, $callback, $icon, $position );

}

public function plugin_settings_page_content() { ?>
<div class="wrap">
    <h2>Channels</h2>
    <form method="post" action="options.php">
        <?php
            settings_fields( 'my_saved_urls' );
            do_settings_sections( 'my_saved_urls' );
            submit_button();
        ?>
    </form>
</div> <?php
}

public function init_settings() {


    register_setting(
        'my_saved_urls',
        'urls_as_json',
        array(
            'type'                => 'string',
        )
    );

    add_settings_section(
        'urls_section_preview',
        'Configured channels',
        function () {
        },
        'my_saved_urls'
    );

    add_settings_field(
        'urls_as_json',
        'channel URLs',
        function () {
            echo '<input type="text" name="urls_as_json" value="' . get_option( 'urls_as_json' ) . '" 
 class="regular-text"/>';
        },
        'my_saved_urls',
        'urls_section_preview'
    );


}
}
2
  • Haven't worked with the settings API in some time, but doesn't WP just serialize arrays? So there should be no need to do the JSON yourself, it should automatically work already. – kero Mar 11 at 18:24
  • 1
    oh gosh.. you're right. this simplifys alot :D I could just pass the array to update_option() and thats it.. thanks! – blackbart Mar 12 at 11:39

Your Answer

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

Browse other questions tagged or ask your own question.