【问题标题】:Fire an action right after Appearance > Theme Options has been saved保存外观 > 主题选项后立即触发操作
【发布时间】:2013-09-28 09:35:55
【问题描述】:


我刚开始使用 Wordpress (v. 3.6.1)。
我安装了 OptionTree,它似乎处理了主题选项页面。我想在用户保存此页面的更改后立即运行我的函数(在插件中或其他任何地方)。

到目前为止,我发现 option-tree/includes/ot-settings-api.php 生成表单并将表单操作设置为 options.php (这是wordpress 核心文件)。我正在考虑将操作更改为我的自定义 php 文件并处理保存过程,最后运行我自己的函数。但是这个解决方案看起来很丑。
我想知道是否还有其他方法可以完成这项工作。
谢谢。

【问题讨论】:

标签: wordpress wordpress-theming


【解决方案1】:

感谢@Sheikh Heera 链接 (tutsplus) 我可以找到解决方案。
我认为这是某种黑客行为,我仍然不知道这是否是最好的方法。无论如何,我这样做了:

  1. 在您的主题 lib 文件夹中创建一个文件 your-theme-settings.php
  2. 在您的主题中添加此代码,让 Wordpress 了解您的文件functions.php

    include_once('lib/your-theme-settings.php');
    
  3. 将此代码添加到your-theme-settings.php

    function your_theme_register_settings() {
        register_setting('option_tree', 'option_tree', 'your_theme_validate_options');
    }
    
    function your_theme_validate_options($input) {
        // do whatever you have to do with $input.
    }
    
    add_action('admin_init', 'your_theme_register_settings');
    

在第 3 步中,我将 'option_tree' 作为 register_settings 函数的第一个和第二个参数,因为我注意到 OptionTree 插件的 Option GroupOption Nameoption_tree

我不确定这是否是最好的解决方案,所以如果你能分享你的想法,我会很高兴。

【讨论】:

  • 旁注:admin_init 仅在管理员端触发,因此无需检查is_admin
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-13
  • 2021-01-18
  • 2014-08-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多