WordPress Settings API PHP Class – It has never been easier

91 thoughts on “WordPress Settings API PHP Class – It has never been easier”

  1. I just wonder what is the proper way of building a plugin with such class, but do not require extra plugin with that wraper – so include it with that created plugin, but then what if i will have two or more such plugins. So IMO there should be some check for existence of the class and if more are present, use the most recent one (if that is even possible)

    1.  @kapler This PHP class is made for doing the repeatable task more simpler. If any/some plugin wants to use this class, then definitely there should be check like that class exists or not. Otherwise it’ll give a fatal error.

  2. Hello,
    Thanks for the plugin.
    But how to use the options.
    For example I want created a plugin which displays text entered in the text box in the header if yes is selected in the radio button. I have created the plugin code and settings page but how use the text entered in the text box and radio button option in the settings page???

      1.  @Tareq I have the same question, I’ll try to rephrase it.
         
        How do you access the data that’s stored in the options fields for use in the frontend of your theme or plugin?

  3. I really like it. I have a feature request however 🙂 I would like to have introduction texts or explanations from time to time, but apparently there is no “LABEL” control. I can create it myself though, but I think it would be nice to have in your code here.

  4.  @Tareq Legend, working perfectly. Thanks for the support and thanks for writing the plugin, very handy!

  5.  @Tareq I can do that, yes, but that would be really a plus to have a “label” as well, to introduce the options of the tabs, or maybe to be able to have an “about” tab.

  6. @Jordy As settings API is WordPress native, all the option handling is done by WordPress itself. And the label is also rendered by WordPress too.

  7. Great work mate! But I still have one question, how to sanitize options before saving them. I mean i can write a simple general function to sanitize all fields in section and call it from the third argument of “register_setting()”, but what if i need to sanitize each field in one section in it’s own way… for example some fields should accept simple html tags and some not… Have any idea?

    1. If you look at the WP source code, you’ll see the third parameter for register_setting( $option_group, $option_name, $sanitize_callback = '' ) is fires a filter like this: add_filter( "sanitize_option_{$option_name}", $sanitize_callback );. So you can externally add a filter and sanitize the settings, I hope that’ll work.

  8. Thank you. I have integrated your class into my plugin template sandbox. I have extended your class.settings-api.php to a settings.php.

    In this extended class I have included all the stuff you do in the big file above. So everytime I create a new plugin, I have only to change the settings.php file for the new options.

    I have added some other decorating stuff and it works great. If your plugin has a base class, make sure you create only one instance of the WeDevs_Settings_API. But it works just fine, thanks again.

  9. I am new to all of this, thanks for the headstart. I am having trouble with getting the values of the multicheck field. Any suggestions?

  10. Hi, I am just learning how to make my own plugins and i found this very useful.
    Thanks.
    I was able to create an options page and make it work in minutes. 🙂

    But, now i do not know how to be able to recover the saved options to use them on my plugin.
    For example:
    I want to use the text box from basic settings into the wp_head() function.
    How do i get the value to be writen on it?
    This is what i have tried but it gaves me an error:
    function wp_head() {
    $options = get_option('wedevs_basics');
    $meta = $options['textarea'];
    if ( $meta != '' ) {
    echo $meta, "\n";
    }
    }

    And i get this error:
    call_user_func_array() expects parameter 1 to be a valid callback, array must have exactly two members in ***/wp-includes/plugin.php on line 406
    Thank you very much.

  11. Hi, i was able to make that work, not sure where the mistake was, but i deleted and start over and now is working. 🙂
    One things is happening to me is that when i save the options i received two “Settings Saved” notifications on the top of the plugin.

    It is not affecting the plugin at all, but it is strange.

    Where could be the problem?

    Thanks again

  12. Another tip without modifying any of your classes.


    ...,
    array(
    'label' => 'Slider settings'
    ),
    ...

    When you add this you can nicely put header between fields on a single page. Now you have tabs and headers. Still a big fan of this plugin. I will hit the donate button. You’ve earned it.

  13. Hei,

    It actually gives me the error that “Options page not found”. What could I be doing wrong?

      1. My plugin is in a class, could that do something wrong (like the callback has to be called differently). Could you give a comment on how to implement it in a Class oriented plugin?

        It seems very promising class, would be great to get use of it…

        1. WHere it gives the error is on saving the options. Which basically means they were not regisered or something

          1. Thanks! That worked.

            Another question: is there a possibility to pass in a class or an ID (so I could do some customizations, like enabling native wordpress color picker).

            M. Rettfoss

          2. There is a ID for each input, automatically generated as the section_name[input_name] format. You can do what you wanted.

          3. True true,

            But just for future generations, you need to escape the square brackets in jQuery:

            $(‘#mnp_settings\\[button_color\\]’);

            I would still prefer not doing that:)

            M. Rettfoss

  14. Hi again,

    Would it be possible to call sanitize callback on register_settings. I see in your class that the argument is not there, so I was wondering if I could add it (maybe with some conditional checking if function exists), but then I don’t want to edit your class with new case specific callbacks. Is it possible to call external callback from within (your) class? My plugin is enclosed in class.

    Thanks! Otherwise your class is a piece of art! I really appreciate you wrote it. Do you accept donations or something?

    M.

    1. Yeah, I didn’t added any callback on register_settings, but you could easily add one. WordPress automatically gives you this filter if you didn’t pass any callback: add_filter( "sanitize_option_{$option_name}", $sanitize_callback );. If you think this should be in the class, please fork the class and give me a pull request. I’ll be happy to have this here.

      Otherwise your class is a piece of art! I really appreciate you wrote it

      Ahh, you are kind enough to say it! 🙂 If you want to donate, there is a donate button in the sidebar. You can use it.

  15. Is this currently working under WP 3.5.1?

    Also, just curious, is there documentation of which files to insert the code snippets in this post?

    I kinda need a starting point.. thanx 🙂

  16. Great framework, Tareq. I have been working on one very similar. I’ve forked you on GitHub and look forward to working with you in the future on some changes I have in mind.

    – Jesse

  17. Sorry Tareq…

    I’m trying to add a sanitize filter as you said, but I must be stupid or else because I can’t do it works.
    Can you help me?

    Thanks! And you know: sorry for my english…

  18. Hi Tareq,

    Is there option to add some custom content to tab.
    I need to include some custom things on options page.
    Would like to add custom action hook, so I can hook into second tab.

    Thanks in advanced

    BR
    Vlado

      1. Hi,

        I have modified it manually thanks.
        One more question please,

        When plugin is activated for the first time and if user has never saved options, options don’t exists in wp_options table.
        Can I somehow ‘save settings’ once plugin is activated?

        BR
        Vlado

  19. Hi,

    Nice work. But i have problem. how i can call the field value?

    When i tried

    function wp_settingapi_hook() {

    ?>

    body{background:}

    <?php

    }
    add_action('wp_head', 'wp_settingapi_hook');

    I want to change of my body background-color using a field. but i faild

  20. function admin_menu() {
    add_options_page( ‘Settings API’, ‘Settings API’, ‘delete_posts’, ‘settings_api_test’, array($this, ‘plugin_page’) );
    }
    ‘delete_posts’—>’manage_options’

  21. This is by far the best settings API class I have found. Simple native and easy to use. BRILLIANT! Thank you so much.

  22. Dear Tareq vaiya ami apnar setting api ti use koresi abong ati shothik vabe kaj korse kintoo ami value get korar shomoy condition use korte parsina like if(exist){something} please help me how can i do this

  23. Dear Tareq Hasan vai, It’s a awesome work. Very helpful and time saving. Can you Please add some more field like, wysiwyg, taxonomy or post-type select, group file upload etc.

    Thanks in advance.

  24. Via, many many thanks for your valuable contribution. It is really a very helpful to crate a nice option panel. But I have question! How can upload image using this? Please help me.

      1. Apnar settings panel er codegolo dekhe bujhlam konta ki kaj kore but eta bujhte parlam na j kivabe elementgolo dynamic kora jay. Apnra jemon theme option theke kono data ante gele ekta variable niye seta eivabe kore ani kintu plugin er belay kivabe korte hobe setaito janlamna. Kindly ekta chotto video tutorial korle amra sobai upokrito hotam.Eta amar kothana sobai same kothai bolche. ekta tutorial.

  25. Tareq vaia i found your article on google search. It’s a fantastic article, really easy to use. But as a beginner in WordPress plugin development i have something’s to know. For now if i want to add a repeater option then what should i do or where should i change or add.

    Thanks in advance.

  26. ভাইয়া ছোট্ট একটা সমস্য পেলাম। সেটা হলো যদি একই সাথে ২টা ছবি আপলোড ব্যবহার করা হয়, তাহলে সব সময় প্রথম আপলোডের টেক্সট ফিল্ডে ইমেজ এর লিংকটা যাচ্ছে।

    মানে, প্রথমে ব্রাউজে ক্লিক করে একটা ছবি আপলোড কর সেটা সিলেক্ট করলে টেক্সট ফিল্ডে সেটার লিংক চলে আসতেছে। এখন যদি পরের আপলোড ফিল্ডে ক্লিক করে আবার নতুন একটা ছবি আপলোড করে সিলেক্ট বাটনে ক্লিক করি তাহলে এটার লিংক ঐ প্রথম আপলোড ফিল্ডের টেক্সট বক্সে চলে যাচ্ছে। কিন্তু যাবার কথা ছিলো ২য় আপলোড ফিল্ডের টেক্সট বক্সে ।

  27. Would you be willing to expand on error messaging? Currently, when a field is sanitized, there’s no ability to display an error message. When a field is sanitized, and there’s an error in the field, such as an email address, it just removes the input. I hire through Upwork, and I’m willing to donate on Upwork or through your website for this feature. Thank you!

  28. Thank you so much for creating this plugin. would it be possible if you can extend your example so that the color picker can actually be applied to the h1 or h2 or h3 or h4 or h5 or h6 tag?

  29. Thanks for your very helpful settings api class and example plugin. I do have the following question: If I want to delete all options from the database upon deleting the plugin, how would the uninstall.php for the example plugin look like?

  30. Great job at making the settings class!
    We use it quite often at my agency.
    I have noticed that there quite a few of pending pull requests in the Github repo.
    If you need help dealing with them, we are happy to contribute to the project!
    Just let me know if you are interested.
    Cheers,
    Guido

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.