{"id":1372,"date":"2009-12-28T15:46:19","date_gmt":"2009-12-28T13:46:19","guid":{"rendered":"http:\/\/planetozh.com\/blog\/?p=1372"},"modified":"2009-12-28T15:49:12","modified_gmt":"2009-12-28T13:49:12","slug":"how-to-per-user-options-in-wordpress","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2009\/12\/how-to-per-user-options-in-wordpress\/","title":{"rendered":"How To: Per User Options in WordPress"},"content":{"rendered":"<p>The essence of plugins is giving to WordPress users more control over their blog, that is, most of the time, more <strong>options<\/strong>. Typically, a plugin adds a page under the Settings menu, where you can modify the options (see this <a href=\"http:\/\/planetozh.com\/blog\/2009\/05\/handling-plugins-options-in-wordpress-28-with-register_setting\/\">article on register_setting()<\/a> for best practices).<\/p>\n<p>But if you think about multi user blogs, it can make sense to implement <strong>per user options<\/strong> instead of &quot;global&quot; settings. And guess what? It&#39;s dead easy.<br \/>\n<!--more--><\/p>\n<p>First, a few theory, then a complete example. Scroll down if you&#39;re the kind of guy who likes code, not explanations :)<\/p>\n<h2>Modify the &quot;Profile&quot; page<\/h2>\n<p>What we are going to do is play with the action hooks provided by file <tt>wp-admin\/user-edit.php<\/tt> to add some fields to the profile page, then save them.<\/p>\n<p>To add content (ie fields, checkboxes, texareas&#8230;) to the profile page, you can use any of the 3 actions:<\/p>\n<ul>\n<li><tt>personal_options<\/tt>: append content to the &quot;Personal Options&quot; block<\/li>\n<li><tt>profile_personal_options<\/tt>: add content after the &quot;Personal Options&quot; block (like add a whole new custom block)<\/li>\n<li><tt>show_user_profile<\/tt>: add content right before the &quot;Update Profile&quot; button<\/li>\n<\/ul>\n<p>For instance, to add content within the &quot;Personal Options&quot; block, you simply need to do something like:<\/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\">'personal_options'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'ozh_personal_options'<\/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_personal_options<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: #b1b100\">echo<\/span> <span style=\"color: #0000ff\">&quot;&lt;p&gt;Content added by action 'personal_options'&lt;\/p&gt;&quot;<\/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>These action hooks will pass one parameter to your function (that is <tt>ozh_personal_options()<\/tt> in the above example) containing a <tt>WP_user<\/tt> object.<\/p>\n<h2>Save custom per user data<\/h2>\n<p>Simply adding fields and pressing &quot;Update Profile&quot; won&#39;t do, you need to tell WordPress what you want to save. Hook into action <tt>personal_options_update<\/tt> and sanitize, I mean, <strong>SANITIZE<\/strong>, the POSTed data. This action will pass the user ID as a parameter to your custom function. Example:<\/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\">add_action<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'personal_options_update'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'ozh_save_user_setting'<\/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_save_user_setting<span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$user_id<\/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: #000088\">$option<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$_post<\/span><span style=\"color: #009900\">&#091;<\/span><span style=\"color: #0000ff\">'somecheckbox'<\/span><span style=\"color: #009900\">&#093;<\/span> <span style=\"color: #339933\">==<\/span> <span style=\"color: #0000ff\">'yes'<\/span> ? <span style=\"color: #0000ff\">'yes'<\/span> <span style=\"color: #339933\">:<\/span> <span style=\"color: #0000ff\">'no'<\/span> <span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span> <span style=\"color: #666666;font-style: italic\">\/\/ sanitize: accept only 'yes' or 'no'<\/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; update_usermeta<span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$user_id<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'some_user_options'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #000088\">$option<\/span> &nbsp;<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<h2>Complete example of a plugin with per user setting<\/h2>\n<p>Let&#39;s make a dummy plugin that will allow registered users of a blog to be nagged by a silly javascript alert box on every admin page they&#39;ll visit.<\/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\"><span style=\"color: #000000;font-weight: bold\">&lt;?php<\/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: #666666;font-style: italic\">\/*<\/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: #666666;font-style: italic\">Plugin Name: Example plugin - Per User Settings<\/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: #666666;font-style: italic\">Plugin URI: http:\/\/planetozh.com\/blog\/<\/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: #666666;font-style: italic\">Description: Add a silly per user setting to your &lt;a href=&quot;profile.php&quot;&gt;Profile&lt;\/a&gt; page<\/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: #666666;font-style: italic\">Author: Ozh<\/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: #666666;font-style: italic\">Author URI: http:\/\/planetozh.com\/<\/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: #666666;font-style: italic\">*\/<\/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\">\/\/ Add a custom field to the form in &quot;Profile&quot;<\/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_user_setting_alert_box<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$user<\/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\"><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>tr<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>th scope<span style=\"color: #339933\">=<\/span><span style=\"color: #0000ff\">&quot;row&quot;<\/span><span style=\"color: #339933\">&gt;<\/span>Add Alert Box<span style=\"color: #339933\">&lt;\/<\/span>th<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>td<span style=\"color: #339933\">&gt;&lt;<\/span>label <span style=\"color: #b1b100\">for<\/span><span style=\"color: #339933\">=<\/span><span style=\"color: #0000ff\">&quot;ozh_add_nag&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>input name<span style=\"color: #339933\">=<\/span><span style=\"color: #0000ff\">&quot;ozh_add_nag&quot;<\/span> type<span style=\"color: #339933\">=<\/span><span style=\"color: #0000ff\">&quot;checkbox&quot;<\/span> id<span style=\"color: #339933\">=<\/span><span style=\"color: #0000ff\">&quot;ozh_add_nag&quot;<\/span> value<span style=\"color: #339933\">=<\/span><span style=\"color: #0000ff\">&quot;1&quot;<\/span> <span style=\"color: #000000;font-weight: bold\">&lt;?php<\/span> checked<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'1'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #000088\">$user<\/span><span style=\"color: #339933\">-&gt;<\/span><span style=\"color: #004000\">ozh_add_nag<\/span><span style=\"color: #009900\">&#041;<\/span><span style=\"color: #339933\">;<\/span> <span style=\"color: #000000;font-weight: bold\">?&gt;<\/span> <span style=\"color: #339933\">\/&gt;<\/span> Add a silly and annoying alert box on <span style=\"color: #990000\">each<\/span> admin page<\/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>label<span style=\"color: #339933\">&gt;&lt;\/<\/span>td<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; <span style=\"color: #339933\">&lt;\/<\/span>tr<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\"><span style=\"color: #000000;font-weight: bold\">&lt;?php<\/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\">\/\/ Handle data that's posted and sanitize before saving it<\/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_save_user_setting_alert_box<span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$user_id<\/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: #000088\">$ozh_add_nag<\/span> <span style=\"color: #339933\">=<\/span> <span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #339933\">!<\/span><span style=\"color: #990000\">empty<\/span><span style=\"color: #009900\">&#040;<\/span><span style=\"color: #000088\">$_POST<\/span><span style=\"color: #339933\">&amp;<\/span><span style=\"color: #666666;font-style: italic\">#91;'ozh_add_nag'&amp;#93;) ? 1 : 0 );<\/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; update_usermeta<span style=\"color: #009900\">&#040;<\/span> <span style=\"color: #000088\">$user_id<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'ozh_add_nag'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #000088\">$ozh_add_nag<\/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\">\/\/ Now, the per user behavior itself: add the silly alert box if applicable<\/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_get_user_setting_alert_box<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: #b1b100\">if<\/span><span style=\"color: #009900\">&#040;<\/span> get_user_option<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'ozh_add_nag'<\/span><span style=\"color: #009900\">&#041;<\/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>script type<span style=\"color: #339933\">=<\/span><span style=\"color: #0000ff\">&quot;text\/javascript&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; alert<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">&quot;Annoying, isn't it?&quot;<\/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; <span style=\"color: #000000;font-weight: bold\">&lt;\/script&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: #000000;font-weight: bold\">&lt;?php<\/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: #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\"><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\">\/\/ Hook everything<\/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\">add_action<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'personal_options'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'ozh_user_setting_alert_box'<\/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\">add_action<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'personal_options_update'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'ozh_save_user_setting_alert_box'<\/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\">add_action<span style=\"color: #009900\">&#040;<\/span><span style=\"color: #0000ff\">'admin_head'<\/span><span style=\"color: #339933\">,<\/span> <span style=\"color: #0000ff\">'ozh_get_user_setting_alert_box'<\/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;<\/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\">?&gt;<\/span><\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>For your convenience, the above plugin example and a few more inline comments is <a href=\"http:\/\/planetozh.com\/blog\/wp-content\/uploads\/2009\/12\/plugin-example-per-user-setting.txt\">available here<\/a> (rename as .php)<\/p>\n<h2>Best practices<\/h2>\n<p>A few things you&#39;ll want to do:<\/p>\n<ul>\n<li><strong>Visual integration<\/strong>: use the same markup and CSS classes than the latest WordPress as of date to add rows and fields. Your plugin must add content to the profile page as if it was a built-in option. I hate it when plugin options look like weird plugin option. It must look like WordPress.<\/li>\n<li><strong>Data sanitization<\/strong>: don&#39;t save blindly and assume everything posted will be stupid or malicious. If you&#39;re expecting an integer, use <tt>intval()<\/tt>. If you&#39;re expecting one value from a set, explicitely test for each value. If you&#39;re expecting free text including HTML, use <tt>esc_html()<\/tt>. And so on. Sanitize everything.<\/li>\n<li><strong>Data concatenation<\/strong>: if you&#39;re going to add several options, please, <a href=\"http:\/\/striderweb.com\/nerdaphernalia\/2008\/07\/consolidate-options-with-arrays\/\">put them in one array<\/a> and save only this one entry.\n<li><strong>Wise implementation<\/strong>: sometimes it just makes sense to have a standalone plugin option page with global settings instead of per user settings. The typical WordPress blog is a single user blog.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>The essence of plugins is giving to WordPress users more control over their blog, that is, most of the time, more options. Typically, a plugin adds a page under the Settings menu, where you can modify the options (see this article on register_setting() for best practices). But if you think about multi user blogs, it [&hellip;]<\/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":[367,366,245],"class_list":["post-1372","post","type-post","status-publish","format-standard","hentry","category-published","tag-how-to","tag-options","tag-wordpress"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/1372","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=1372"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/1372\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=1372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=1372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=1372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}