{"id":1241,"date":"2009-05-05T12:50:54","date_gmt":"2009-05-05T10:50:54","guid":{"rendered":"http:\/\/planetozh.com\/blog\/?p=1241"},"modified":"2009-05-05T12:55:31","modified_gmt":"2009-05-05T10:55:31","slug":"handling-plugins-options-in-wordpress-28-with-register_setting","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2009\/05\/handling-plugins-options-in-wordpress-28-with-register_setting\/","title":{"rendered":"Handling Plugins Options in WordPress 2.8 with register_setting()"},"content":{"rendered":"<p>In the upcoming WordPress 2.8 there&#39;s an interesting function set meant to help authors manage their plugin custom options. In a nutshell: whitelist your options, define how you want them to be validated, and just lean back and rely on the API to handle everything for you.<\/p>\n<p>Your new best friend is function <tt>register_setting()<\/tt>, and I&#39;m going to recapitulate how it&#39;s supposed to be used.<br \/>\n<!--more--><\/p>\n<h2>The (very) old (deprecated) way (that sucks)<\/h2>\n<p>If you began coding plugin a long time ago, you may have sticked to the habit of doing everything yourself, re-inventing and re-coding the wheel everytime.<\/p>\n<p>Does something like checking for $_POST on your plugin admin page ring a bell? Time to evolve to a hassle free way of coding stuff.<\/p>\n<h2>The (new) register_setting() way (that pwnz)<\/h2>\n<p>The concept is the following: do the little tasks, let WordPress manage the dirty job.<\/p>\n<p>&#8211; on init, tell WordPress that you&#39;re going to use some new options<br \/>\n&#8211; optional but cool, tell WordPress what function you&#39;d like to be used for data validation (the user is supposed to enter text on your plugin admin page? Make sure it&#39;s only text, then)<br \/>\n&#8211; optional but cool, store all your options in one DB entry (yeah, those who&#39;ve been reading this blog for a while know that it&#39;s my favorite pet peeve)<br \/>\n&#8211; don&#39;t handle POST data, let WordPress do it<br \/>\n&#8211; don&#39;t update\/create the DB entry in the options, let WordPress do it<br \/>\n&#8211; don&#39;t bother with all the security, nonces, anti XSS stuff you have to throw in, let WordPress do it<\/p>\n<h2>Code, dissected<\/h2>\n<p>First, tell WordPress that you&#39;re going to use new options<\/p>\n<div id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">add_action<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'admin_init'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'ozh_sampleoptions_init'<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">function<\/span> ozh_sampleoptions_init<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; register_setting<span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">'ozh_sampleoptions_options'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'ozh_sample'<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>This function says: I&#39;m going to need a new set of options, named &#39;ozh_sampleoptions_options&#39;, it will be stored in the DB entry &#39;ozh_sample&#39;. There can a third parameter, a callback function to sanitize data: we&#39;ll see this later.<\/p>\n<p>Then, we need to draw the plugin option page itself. Nothing really new here, except just one new thing. The function that echoes the admin form would be something like:<\/p>\n<div id=\"ig-sh-2\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ Draw the menu page itself<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">function<\/span> ozh_sampleoptions_do_page<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000000;font-weight: bold\">?&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #339933\">&lt;<\/span>div <span style=\"color: #000000;font-weight: bold\">class<\/span><span style=\"color: #339933\">=<\/span><span style=\"color: #0000ff\">&quot;wrap&quot;<\/span><span style=\"color: #339933\">&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; &nbsp; &nbsp; <span style=\"color: #339933\">&lt;<\/span>h2<span style=\"color: #339933\">&gt;<\/span>Ozh<span style=\"color: #0000ff\">'s Sample Options&lt;\/h2&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">&nbsp; &nbsp; &nbsp; &nbsp; &lt;form method=&quot;post&quot; action=&quot;options.php&quot;&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;?php settings_fields('<\/span>ozh_sampleoptions_options<span style=\"color: #0000ff\">'); ?&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;?php $options = get_option('<\/span>ozh_sample<span style=\"color: #0000ff\">'); ?&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;table class=&quot;form-table&quot;&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tr valign=&quot;top&quot;&gt;&lt;th scope=&quot;row&quot;&gt;A Checkbox&lt;\/th&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;input name=&quot;ozh_sample&amp;#91;option1&amp;#93;&quot; type=&quot;checkbox&quot; value=&quot;1&quot; &lt;?php checked('<\/span><span style=\"color: #cc66cc\">1<\/span><span style=\"color: #0000ff\">', $options&amp;#91;'<\/span>option1<span style=\"color: #0000ff\">'&amp;#93;); ?&gt; \/&gt;&lt;\/td&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;\/tr&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;tr valign=&quot;top&quot;&gt;&lt;th scope=&quot;row&quot;&gt;Some text&lt;\/th&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;td&gt;&lt;input type=&quot;text&quot; name=&quot;ozh_sample&amp;#91;sometext&amp;#93;&quot; value=&quot;&lt;?php echo $options&amp;#91;'<\/span>sometext<span style=\"color: #0000ff\">'&amp;#93;; ?&gt;&quot; \/&gt;&lt;\/td&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;\/tr&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;\/table&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;p class=&quot;submit&quot;&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;input type=&quot;submit&quot; class=&quot;button-primary&quot; value=&quot;&lt;?php _e('<\/span>Save Changes<span style=\"color: #0000ff\">') ?&gt;&quot; \/&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;\/p&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">&nbsp; &nbsp; &nbsp; &nbsp; &lt;\/form&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">&nbsp; &nbsp; &lt;\/div&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">&nbsp; &nbsp; &lt;?php &nbsp; <\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">}<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">&amp;#91;\/php&amp;#93;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #0000ff\">Notice the &lt;tt&gt;settings_fields()&lt;\/tt&gt; call. This tells WordPress to add all the hidden fields our form needs (nonce, referrer check...), and that we'<\/span>re going to <span style=\"color: #000000;font-weight: bold\">use<\/span> our option set previously <span style=\"color: #990000\">defined<\/span> <span style=\"color: #b1b100\">as<\/span> <span style=\"color: #0000ff\">'ozh_sampleoptions_options'<\/span><span style=\"color: #339933\">.<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">And that<span style=\"color: #0000ff\">'s all, basically. You don'<\/span>t need to check <span style=\"color: #b1b100\">if<\/span> user submits the form<span style=\"color: #339933\">,<\/span> to check whether the options need to be updated or not<span style=\"color: #339933\">,<\/span> to handle things passed to <span style=\"color: #000088\">$_POST<\/span><span style=\"color: #339933\">.<\/span> WordPress takes care of it<span style=\"color: #339933\">.<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #339933\">&lt;<\/span>h2<span style=\"color: #339933\">&gt;<\/span>Data sanitization<span style=\"color: #339933\">&lt;\/<\/span>h2<span style=\"color: #339933\">&gt;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">Function<\/span> <span style=\"color: #339933\">&lt;<\/span>tt<span style=\"color: #339933\">&gt;<\/span>register_setting<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">&lt;\/<\/span>tt<span style=\"color: #339933\">&gt;<\/span> accepts <span style=\"color: #cc66cc\">3<\/span> parameters<span style=\"color: #339933\">:<\/span> the option group name<span style=\"color: #339933\">,<\/span> the option name to save and sanitize<span style=\"color: #339933\">,<\/span> and a callback <span style=\"color: #000000;font-weight: bold\">function<\/span> that does the sanitization<span style=\"color: #339933\">.<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #b1b100\">If<\/span> you want the user to submit integers<span style=\"color: #339933\">,<\/span> and only integers <span style=\"color: #009900\">&#040;<\/span>say<span style=\"color: #339933\">,<\/span> they age<span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">,<\/span> you would <span style=\"color: #b1b100\">for<\/span> instance <span style=\"color: #000000;font-weight: bold\">use<\/span><span style=\"color: #339933\">:<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#091;<\/span>php<span style=\"color: #009900\">&#093;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">register_setting<span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">'my_options'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'your_age'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'intval'<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>If you&#39;re allowing safe text with a limited set of HTML tags:<\/p>\n<div id=\"ig-sh-3\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">register_setting<span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">'my_options'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'your_bio'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'wp_filter_nohtml_kses'<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>Now, in the previous example, since we want to store everything in a single DB entry, we&#39;ll define our own validation function:<\/p>\n<div id=\"ig-sh-4\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">php<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"php\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">function<\/span> ozh_sampleoptions_init<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; register_setting<span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #0000ff\">'ozh_sampleoptions_options'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'ozh_sample'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'ozh_sampleoptions_validate'<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp;<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #666666;font-style: italic\">\/\/ Sanitize and validate input. Accepts an array, return a sanitized array.<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #000000;font-weight: bold\">function<\/span> ozh_sampleoptions_validate<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$input<\/span><span style=\"color: #009900\">&#041;<\/span> <span style=\"color: #009900\">&#123;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/\/ Our first value is either 0 or 1<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$input<\/span><span style=\"color: #009900\">&#091;<\/span><span style=\"color: #0000ff\">'option1'<\/span><span style=\"color: #009900\">&#093;<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$input<\/span><span style=\"color: #009900\">&#091;<\/span><span style=\"color: #0000ff\">'option1'<\/span><span style=\"color: #009900\">&#093;<\/span> <span style=\"color: #339933\">==<\/span> <span style=\"color: #cc66cc\">1<\/span> ? <span style=\"color: #cc66cc\">1<\/span> <span style=\"color: #339933\">:<\/span> <span style=\"color: #cc66cc\">0<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #666666;font-style: italic\">\/\/ Say our second option must be safe text with no HTML tags<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #000088\">$input<\/span><span style=\"color: #009900\">&#091;<\/span><span style=\"color: #0000ff\">'sometext'<\/span><span style=\"color: #009900\">&#093;<\/span> <span style=\"color: #339933\">=<\/span> &nbsp;wp_filter_nohtml_kses<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$input<\/span><span style=\"color: #009900\">&#091;<\/span><span style=\"color: #0000ff\">'sometext'<\/span><span style=\"color: #009900\">&#093;<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp; <span style=\"color: #b1b100\">return<\/span> <span style=\"color: #000088\">$input<\/span><span style=\"color: #339933\">;<\/span><\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\"><span style=\"color: #009900\">&#125;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<h2>All wrapped up<\/h2>\n<p>For your convenience, you&#39;ll find all the code of this article in the following plugin: <a href=\"https:\/\/planetozh.com\/blog\/wp-content\/uploads\/2009\/05\/ozh-sampleoptions-pluginphp.txt\">ozh-sampleoptions-plugin.php<\/a> (rename as .php)<\/p>\n<p>Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the upcoming WordPress 2.8 there&#39;s an interesting function set meant to help authors manage their plugin custom options. In a nutshell: whitelist your options, define how you want them to be validated, and just lean back and rely on the API to handle everything for you. Your new best friend is function register_setting(), and I&#39;m going to recapitulate how\u2026<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[2,85,353,245],"class_list":["post-1241","post","type-post","status-publish","format-standard","hentry","category-published","tag-code","tag-plugins","tag-register_setting","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/1241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=1241"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/1241\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=1241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=1241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=1241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}