【问题标题】:Wordpress storing theme optionsWordpress 存储主题选项
【发布时间】:2011-07-09 03:07:03
【问题描述】:

我目前正在使用 update_option(name, value); 单独存储我的所有主题选项。函数,但我正在考虑将它们全部放入一个数组中,对其进行序列化,并将单个选项存储在数据库中。

这样更有效率吗?

【问题讨论】:

    标签: database wordpress serialization themes options


    【解决方案1】:

    是的,如果您在从 DB 读取配置后进行一些处理(读取:反序列化)!

    根据您将查询保存到数据库的选项数量。

    【讨论】:

      【解决方案2】:

      不需要序列化。有一些内置方法可以将主题选项整齐地存储在数组中。

      这是一个包含所有必要代码的完整示例:

      首先,在你的主题的functions.php文件中,你必须通过编写一个小函数并使用WordPress钩子来激活它来注册你将使用的设置:

      <?php
          function my_theme_admin_init() {
              register_setting('my_options', 'my_theme_options');
          }
      
          add_action('admin_init', 'my_theme_admin_init');
      ?>
      

      然后,在你要使用选项的地方,使用这点html和php。 (请注意,表单发布到 options.php。这样做会利用 WordPress 内置功能来处理为您保存选项):

      <form method="post" action="options.php">
          <?php 
          // Load the options from the WP db
          $options = get_option('my_theme_options');
          // WP built-in function to display the appropriate fields for saving options
          settings_fields("my_options"); ?>
          <table class="form-table">
              <tr>
                  <th scope="row">Facebook URL:</th>
                  <td>
                      <input type="text" name="my_theme_options[facebook]" size="40" value="<?php echo stripslashes($options["facebook"]); ?>" />
                  </td>
              </tr>
              <tr>
                  <th scope="row">Twitter URL:</th>
                  <td>
                      <input type="text" name="my_theme_options[twitter]" size="40" value="<?php echo stripslashes($options["twitter"]); ?>" />
                  </td>
              </tr>
              <tr>
                  <th scope="row">LinkedIn URL:</th>
                  <td>
                      <input type="text" name="my_theme_options[linkedin]" size="40" value="<?php echo stripslashes($options["linkedin"]); ?>" />
                  </td>
              </tr>
          </table>
          <p class="submit">
              <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
          </p>
      </form>
      

      【讨论】:

        猜你喜欢
        • 2012-08-13
        • 2014-08-02
        • 2012-11-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-29
        相关资源
        最近更新 更多