libreform / wp-libre-form

Easy native HTML5 forms for WordPress. Version 1.5 is unmaintained, but works without issue. 2.0 has been rewritten from the ground, and can be found at https://github.com/libreform/libreform
https://wordpress.org/plugins/wp-libre-form
GNU General Public License v3.0
67 stars 27 forks source link

Add a global settings page #139

Closed luizbills closed 5 years ago

luizbills commented 5 years ago

A global settings page for WP Libre Form it will be a good idea now that we have a plugins section. So all plugins can add their settings on this single page.

Optional: a simple settings api to easily add new fields.

k1sul1 commented 5 years ago

There already is one. It lives in the Plugins subpage under Forms.

Here's how WP Libre Formbuilder does it (it doesn't do anything with it yet):

  add_action("plugins_loaded", function () {
    $wplf = wplf();
    $builder = WP_Libre_Formbuilder::instance();
    $wplf->plugins->register([
      "name" => "Formbuilder",
      "description" => "Visual editor for the HTML impaired.",
      "link" => "https://github.com/k1sul1/wp-libre-formbuilder",
      "version" => WPLFB_VERSION,
      "instance" => $builder,
      "settings_page" => [$builder, "renderSettingsPage"],
    ]);
  });

It doesn't care how you save or render data, but it allows plugin authors to add a settings page. If we ever need a settings page in the core plugin, it can be added to the plugin class.

timiwahalahti commented 5 years ago

IMO we do not need separate settings page or availability to do so.

luizbills commented 5 years ago

I was thinking in something like woocommerce settings page. A central of all plugins settings with a friendly API to easily add new settings.

something like this:


$my_prefix = 'wpfl_plugin';

// each section would have its own tab
// or we can create default tabs like: general, customization, email, etc
add_filter( 'wplf_settings_sections', function ( $sections )  use ( $my_prefix ) {
    return array_merge( $sections, [
        //slug => Section Name
        $my_prefix => 'Custom Plugin',
    ] );
});

add_action( "wplf_${my_prefix}_display_settings", function ()  use ( $my_prefix ) {
    // section title
    echo '<h2>My Plugin Settings</h2>';

    // render a text field
    wplf_settings_text_field( [
        'id' => "${my_prefix}_foo", // [name] field attr
        'default' => 'bar',
        // etc
    ] );
});

add_action( "wplf_${my_prefix}_save_settings", function () use ( $my_prefix ) {
    // render a text field
    if ( ! empty( $_POST["${my_prefix}_foo"] ) ) {
        update_option( "${my_prefix}_foo", $_POST["${my_prefix}_foo"] ); 
    }
});
luizbills commented 5 years ago

So we can recommend all plugin creators to use our Settings API to avoid having multiple settings pages in wp-admin.

k1sul1 commented 5 years ago

We're already "recommending" authors to use wplf()->plugins->register(), so they won't have to pollute the menu with useless options pages. How said authors save data doesn't concern us. Some may want to use post_meta, and someone else might need to use custom tables or wp_options.

luizbills commented 5 years ago

We're already "recommending" authors to use wplf()->plugins->register(), so they won't have to pollute the menu with useless options pages. How said authors save data doesn't concern us. Some may want to use post_meta, and someone else might need to use custom tables or wp_options.

Oh sorry. I thought every author had to create his settings page if he used wplf()->plugins->register().

k1sul1 commented 5 years ago

153 will introduce a settings page to bring backwards compatibility. There's no way to add fields to it though, other than editing our core code.