主题激活钩


15

我想编写一个函数,以在激活主题后通过电子邮件将网站的URL发送给我。

激活主题时启动的挂钩是什么?


5
为此目的使用主题激活挂钩绝对错误的:“自由运行程序意味着……出于任何目的……使用它…… 而无需与开发人员进行交流。或任何其他特定实体。在这种自由中,重要的是用户的目的,而不是开发人员的目的;您作为用户可以自由地出于您的目的运行程序,如果您将其分发给其他人,则...无权将您的目的强加于她。”
Chip Bennett

1
这是一个坏主意。作为早期的天真的插件开发人员,我实现了这样的事情,却没有考虑对我或我的用户造成的后果。1.这侵犯了用户的隐私。2.如果您的主题分布广泛,您将收到比您可能处理的更多的电子邮件。3.如果#2为true,则根据您的电子邮件托管位置,您的帐户可能被视为违反使用条款。因此,我的电子邮件帐户被关闭了一段时间。
Brian Fegter 2013年

@BrianFegter绝对有道理。当我尝试这个时,我才刚刚开始学习。感谢您分享关注点。关于StackOverflow和StackExchange的最大事实是,当您查看过去一年的问题时,就会意识到自己不时发展了多少:-)
Atif Mohammed Ameenuddin 2013年

Answers:


13

我在这里有该代码,只需在网站上将文件命名为theme_activation_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);
}

1
该代码的作者(Krishna Kant Sharma)也给出了答案,并提供了其源代码的链接。也许当Benny回答这个问题时,他还不够聪明,无法简单地编辑Krishna的答案并向其中添加代码,因此我很
不高兴




0

将此代码放在您的顶部 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用您自己的电子邮件地址替换。

希望能帮助到你。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.