挂钩进入WordPress主题定制器保存操作

gnr*_*cks 6 wordpress themes

我面临以下问题:

我过去常常将所有样式保存在主题选项页面中.当用户单击保存按钮时,我有一个后端脚本生成带有更改的css文件,这样它们就不会在每个页面中内联输出.这有很多好处,其中包括缓存.

我已经切换到主题定制器,一切都很好,除了我找不到挂钩到"保存"按钮的方法.我想触发一个函数,在后端单击该按钮时更新css文件的内容.

这甚至可能吗?

谢谢 !

Mik*_*ike 9

从WordPress 3.6.0开始,你现在可以打电话了customize_save_after.

<?php
function emailAdmin(){
    mail('your@email', 'Woza!', 'You won\'t believe this but someone has updated the theme customizations!!');
}
add_action( 'customize_save_after', 'emailAdmin' );
?>
Run Code Online (Sandbox Code Playgroud)

更多信息:http://developer.wordpress.org/reference/hooks/customize_save_after/


Dov*_*ovy 5

我面临同样的情况.customize_save在保存选项之前工作,所以这样就完成了.我已经通过电子邮件向Otto(ottodestruct.com)发送了相关信息.

我现在的解决方案如下:

add_action('customize_save', 'regenCSS', 100);
function regenCSS( $wp_customize ) {
    checkCSSRegen(); // Checks if I need to regen and does so
    set_theme_mod('regen-css', time()+3); // Waits 3 seconds until everything is saved
}
function checkCSSRegen() {
    if (get_theme_mod('regen-css') != "" && get_theme_mod('regen-css') < time()) {
        makecss();
        remove_theme_mod('regen-css');
    }
}
Run Code Online (Sandbox Code Playgroud)

我还添加了一个额外的checkCSSRegen(); 到我的customize_controls_init函数.

同样,这有点像黑客.不幸的是,这是我当时能找到的最好的.

另一种选择是使用ajax响应,只需ping一个php文件.这感觉更像是一个黑客.

另一个快速的黑客是做一个javascript动作,当点击保存按钮时,它设置一个计时器来延迟调用运行编译的PHP文件.这对我来说非常黑客.

上述唯一的后备,除非重新加载自定义程序或保存其他值,否则您可能无法获得所需的所有值.

其他人有更好的主意吗?

**更新**刚刚向Wordpress团队添加了以下请求.希望我们能把它挤进那里.

http://wordpress.org/ideas/topic/do-customize_save-action-hook-after-the-settings-are-saved?replies=3#post-24853

*更新2* 看起来它将在3.6版本中作为customize_save_after.猜猜一些推文和示例代码即使使用Wordpress团队也能让事情发生.;)


bra*_*ilo 0

customize_save未经测试,但中有动作钩子/wp-includes/class-wp-customize-manager.php

它在save()函数内部:

/**
 * Switch the theme and trigger the save action of each setting.
 *
 * @since 3.4.0
 */
Run Code Online (Sandbox Code Playgroud)

该文件中还有一些其他有趣的操作挂钩 ( do_action) 可能值得检查。