插件开发全攻略(08)—构建一个WordPress插件用户面板

插件开发全攻略(08)—构建一个WordPress插件用户面板

将会有这么一种情况,你将有一个主要的管理面板,但是希望能够让独立的用户设定他们自己的偏好。在Devlounge Plugin Series这个例子中,我们添加了一个是否把文字添加到文章末尾的选项。然而,假如一个登录用户不希望看到这段文字呢?为什么不给他们一个选择,而且不影响到所有其他的用户呢?

 

这篇文章将会涉及到这个问题,让你可以添加你自己的用户面板。

命名你的选项

  1.   class DevloungePluginSeries {
  2.      var $adminOptionsName = DevloungePluginSeriesAdminOptions;
  3.      var $adminUsersName = DevloungePluginSeriesAdminUsersOptions;
  4.  ?>

第3行显示,我添加了一个成员函数,叫做adminUsersName。我给这个变量取了一个长而且独特的名字DevloungePluginSeriesAdminUsersOptions

设定你的缺省用户选项

你将需要一个地方来初始化你的选项,尤其是当一个用户第一次激活了你的插件的时候。然而,这些选项在管理面板之外也应该起作用,不论用户是登录了还是没有登录。

这里是一个我引入到DevloungePluginSeries类中的函数:

  1.   //Returns an array of user options
  2.          function getUserOptions() {
  3.              global $user_email;
  4.              if (empty($user_email)) {
  5.                  get_currentuserinfo();
  6.              }
  7.              if (empty($user_email)) { return ; }
  8.              $devOptions = get_option($this->adminUsersName);
  9.              if (!isset($devOptions)) {
  10.                  $devOptions = array();
  11.              }
  12.              if (empty($devOptions[$user_email])) {
  13.                  $devOptions[$user_email] = true,true;
  14.                  update_option($this->adminUsersName, $devOptions);
  15.              }
  16.              return $devOptions;
  17.          }
  18.  ?>

这个函数完成以下任务:

  • 检查用户是否已经登陆(3-7行)。这个检查仅需要查看变量user_email是否设定即可。
  • 尝试找到以前存储在数据库中的选项(第8行)。
  • 如果没有找到option,设定默认值(第9-15行)。
  • 返回option供你使用

初始化管理用户option

可以在任何时候调用getUserOptions来取得用户管理options。然而,那么插件第一次安装的时候呢?应该有某种函数被调用,以取得用户选项。我添加了下面这个函数到init函数:

  1.   function init() {
  2.      $this->getAdminOptions();
  3.      $this->getUserOptions();
  4.  }
  5.  ?>

第3行调用了新函数getUserOptions。由于已经有了一个action调用了init函数,不需要额外的步骤了。

怎样使管理面板和用户面板一起工作

你会回想起上一篇关于设定一个管理面板的文章中,WordPress管理员可以设定文章末尾的内容,代码是否是否在头部显示,评论中的作者名字是否大写。而用户面板允许那些不是管理员的用户有能力设定他们是否希望这些options。

我们将允许用户决定,如果他们:

  • 想要内容在文章的末尾显示(仅当管理员启用了这个option)
  • 想要评论作者的名字大写(仅当管理员启用了这个option)

设定用户面板函数

我们要做的第一件事情就是编写一个函数来显示用户面板。函数名字为printAdminUsersPage。下一段代码将会读入我们早先设定的options并且检查一下是否有任何文章选项已经被提交。这一节中所有的代码都默认包含于printAdminUsersPage函数。

  1.  //Prints out the admin page
  2.  function printAdminUsersPage() {
  3.      global $user_email;
  4.      if (empty($user_email)) {
  5.          get_currentuserinfo();
  6.      }
  7.      $devOptions = $this->getUserOptions();
  8.      //Save the updated options to the database
  9.      if (isset($_POST['update_devloungePluginSeriesSettings']) && isset($_POST['devloungeAddContent']) && isset($_POST['devloungeAuthor'])) {
  10.          if (isset($user_email)) {
  11.              $devOptions[$user_email] = $_POST['devloungeAddContent'] . “,” . $_POST['devloungeAuthor'];
  12.              ?>
  13.              <div><p><strong>Settings successfully updated.</strong></p></div>
  14.              <?php
  15.              update_option($this->adminUsersName, $devOptions);
  16.          }
  17.      }
  18.      //Get the author options
  19.      $devOptions = $devOptions[$user_email];
  20.      $devOptions = explode(,, $devOptions);
  21.      if (sizeof($devOptions)>= 2) {
  22.          $content = $devOptions[0];
  23.          $author = $devOptions[1];
  24.      }
  25.              ?>

上述代码:

  • 取回用户options(第7行)
  • 存储发送的数据到数据库(第9-18行)
  • 为用户读入用逗号分隔的变量

下一段代码将会显示用户面板必须的HTML表单。所有的代码做的事情就是显示表单元素,并且读入已经取得的选项。

  1.  <div>
  2.  <form method=”post” action=”<?php echo $_SERVER["REQUEST_URI"]; ?>“>
  3.  <h2>Devlounge Plugin Series User Options</h2>
  4.  <h3>Allow Content Added to the End of a Post?</h3>
  5.  <p>Selecting “No” will disable the content from being added into the end of a post.</p>
  6.  <p><label for=”devloungeAddContent_yes”><input type=”radio” id=”devloungeAddContent_yes” name=”devloungeAddContent” value=”true” <?php if ($content == true) { _e(checked=”checked”, DevloungePluginSeries); }?> /> Yes</label>    <label for=”devloungeAddContent_no”><input type=”radio” id=”devloungeAddContent_no” name=”devloungeAddContent” value=”false” <?php if ($content == false) { _e(checked=”checked”, DevloungePluginSeries); }?>/> No</label></p>
  7.  <h3>Allow Comment Authors to be Uppercase?</h3>
  8.  <p>Selecting “No” will leave the comment authors alone.</p>
  9.  <p><label for=”devloungeAuthor_yes”><input type=”radio” id=”devloungeAuthor_yes” name=”devloungeAuthor” value=”true” <?php if ($author == true) { _e(checked=”checked”, DevloungePluginSeries); }?> /> Yes</label>    <label for=”devloungeAuthor_no”><input type=”radio” id=”devloungeAuthor_no” name=”devloungeAuthor” value=”false” <?php if ($author == false) { _e(checked=”checked”, DevloungePluginSeries); }?>/> No</label></p>
  10.  <div>
  11.  <input type=”submit” name=”update_devloungePluginSeriesSettings” value=”<?php _e(Update Settings, DevloungePluginSeries) ?>” /></div>
  12.  </form>
  13.   </div>
  14.                      <?php
  15.                  }//End function printAdminUsersPage()

设定用户面板Action

设定管理面板的时候,我们定义了一个函数叫做DevloungePluginSeries_ap,以帮助初始化管理面板。我们现在将要再次求助于此函数来添加我们的用户面板。

  1.   //Initialize the admin and users panel
  2.  if (!function_exists(DevloungePluginSeries_ap)) {
  3.      function DevloungePluginSeries_ap() {
  4.          global $dl_pluginSeries;
  5.          if (!isset($dl_pluginSeries)) {
  6.              return;
  7.          }
  8.          if (function_exists(add_options_page)) {
  9.      add_options_page(Devlounge Plugin Series, Devlounge Plugin Series, 9, basename(__FILE__), array(&$dl_pluginSeries, printAdminPage));
  10.          }
  11.          if (function_exists(add_submenu_page)) {
  12.              add_submenu_page(profile.php, Devlounge Plugin Series User Options,Devlounge Plugin Series User Options, 0, basename(__FILE__), array(&$dl_pluginSeries, printAdminUsersPage));
  13.          }
  14.      }
  15.  }
  16.  ?>

在第12行,你能看到一行代码:

  • 往profile.php页面中添加了一个子菜单
  • 让一个用户级别高于或者等于0的用户访问用户面板
  • 调用我们的printAdminUsersPage函数

关于访问级别的(此例中为0)更详细的描述,可以参考用户级别

作者:Charles

原文链接:http://sexywp.com/how-to-write-a-wp-plugin-08.htm

此条目发表在 wordpress 分类目录。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。 必填项已被标记为 *

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>