我想写一个功能,以电子邮件给我的网址时,我的主题被激活。
当主题被激活时,钩子会启动什么?
我想写一个功能,以电子邮件给我的网址时,我的主题被激活。
当主题被激活时,钩子会启动什么?
我这里有代码,只需将文件命名为theme\\u activation\\u hook。php喜欢在网站上复制这个。
<?php
/**
* Provides activation/deactivation hook for wordpress theme.
*
* @author Krishna Kant Sharma (http://www.krishnakantsharma.com)
*
* Usage:
* ----------------------------------------------
* Include this file in your theme code.
* ----------------------------------------------
* function my_theme_activate() {
* // code to execute on theme activation
* }
* wp_register_theme_activation_hook(\'mytheme\', \'my_theme_activate\');
*
* function my_theme_deactivate() {
* // code to execute on theme deactivation
* }
* wp_register_theme_deactivation_hook(\'mytheme\', \'my_theme_deactivate\');
* ----------------------------------------------
*
*
*/
/**
*
* @desc registers a theme activation hook
* @param string $code : Code of the theme. This can be the base folder of your theme. Eg if your theme is in folder \'mytheme\' then code will be \'mytheme\'
* @param callback $function : Function to call when theme gets activated.
*/
function wp_register_theme_activation_hook($code, $function) {
$optionKey="theme_is_activated_" . $code;
if(!get_option($optionKey)) {
call_user_func($function);
update_option($optionKey , 1);
}
}
/**
* @desc registers deactivation hook
* @param string $code : Code of the theme. This must match the value you provided in wp_register_theme_activation_hook function as $code
* @param callback $function : Function to call when theme gets deactivated.
*/
function wp_register_theme_deactivation_hook($code, $function) {
// store function in code specific global
$GLOBALS["wp_register_theme_deactivation_hook_function" . $code]=$function;
// create a runtime function which will delete the option set while activation of this theme and will call deactivation function provided in $function
$fn=create_function(\'$theme\', \' call_user_func($GLOBALS["wp_register_theme_deactivation_hook_function\' . $code . \'"]); delete_option("theme_is_activated_\' . $code. \'");\');
// add above created function to switch_theme action hook. This hook gets called when admin changes the theme.
// Due to wordpress core implementation this hook can only be received by currently active theme (which is going to be deactivated as admin has chosen another one.
// Your theme can perceive this hook as a deactivation hook.
add_action("switch_theme", $fn);
}
没有专门的挂钩。我见过两种方法:
switch_theme
勾选主题-Execute “Setup” Code on Activation only?Set options on activation ThemesWordpress现在提供这个钩子作为after_switch_theme
. 您可以这样使用它:
add_action(\'after_switch_theme\', \'my_theme_activation\');
function my_theme_activation () {
// DO ALL THE THINGS
}
您可以使用switch_theme
钩子运行主题代码deactivation 也资料来源:http://codex.wordpress.org/Plugin_API/Action_Reference/after_switch_theme
将此代码放在functions.php
<?php if ( is_admin() && isset($_GET[\'activated\'] ) && $pagenow == "themes.php" ) {
// do your stuff
$url = get_site_url();
// The message
$message = "a new wordpress theme is activated on $url ";
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70, "\\r\\n");
// Send
wp_mail(\'mail@yourdomain.com\', \'theme geactiveerd\', $message);
}
?>
更换mail@yourdomain.com
使用您自己的电子邮件地址。希望有帮助。