Activate / Deactivate plugin

时间:2014-02-13 作者:Designer 17

我正在开发我的第一个插件,并尝试使用register_activation_hook()register_deactivation_hook() ... 我知道我做得不对:

/*====================================================*/
//  1.0 Actions to perform upon plugin activation
/*====================================================*/
register_activation_hook( __FILE__, \'construct_uploader_page\' );
register_activation_hook( __FILE__, \'construct_terms_page\' );
register_activation_hook( __FILE__, \'create_custom_user_role\' );

// Construct Event Photo Uploader template page
function construct_uploader_page() {

    $post_id = -1;

    // Setup custom vars
    $author_id = 1;
    $slug = \'event-photo-uploader\';
    $title = \'Event Photo Uploader\';

    // Check if page exists... if not, create it    
    if ( null == get_page_by_title( $title )) {

        $uploader_page = array(
            \'comment_status\'    => \'closed\',
            \'ping_status\'       => \'closed\',
            \'post_author\'       => $author_id,
            \'post_name\'         => $slug,
            \'post_title\'        => $title,
            \'post_status\'       => \'publish\',
            \'post_type\'         => \'page\'
        );

        $post_id = wp_insert_post( $uploader_page );

        if ( !$post_id ) {
            wp_die( \'Error creating template page\' );

        } else {
            update_post_meta( $post_id, \'_wp_page_template\', \'custom-uploadr.php\' );
        }
    }
} // end construct uploader page

// Add Event Photo Uploader page
add_action( \'template_include\', \'uploadr_redirect\' );

// Callback to add menu items
function uploadr_redirect( $template ) {
    $plugindir = dirname( __FILE__ );

    // A Specific Custom Post Type
    if ( is_page_template( \'custom-uploadr.php\' )) {
        $template = $plugindir . \'/templates/custom-uploadr.php\';
    }

    return $template;   
}

// Construct Terms and Usage template page
function construct_terms_page() {

    $post_id = -1;

    // Setup variables
    $author_id = 1;
    $slug = \'terms-and-usage\';
    $title = \'Terms and Usage\';

    // Check if page exists... if not, create it
    if ( null == get_page_by_title( $title )) {
        $uploader_page = array(
            \'comment_status\'    => \'closed\',
            \'ping_status\'       => \'closed\',
            \'post_author\'       => $author_id,
            \'post_name\'         => $slug,
            \'post_title\'        => $title,
            \'post_status\'       => \'publish\',
            \'post_type\'         => \'page\'
        );

        $post_id = wp_insert_post( $uploader_page );

        if ( !$post_id ) {
            wp_die( \'Error creating template page\' );
        } else {
            update_post_meta( $post_id, \'_wp_page_template\', \'terms-and-use.php\' );
        }
    }
} // end construct terms page

// Add Terms and Usage page
add_action( \'template_include\', \'terms_redirect\' );

// Callback to add menu items
function terms_redirect( $template ) {
    $plugindir = dirname( __FILE__ );

    // A Specific Custom Post Type
    if ( is_page( \'Terms and Usage\' )) {

        $template = $plugindir . \'/templates/terms-and-use.php\';
    }

    return $template;
}

// Create custom user-role for registration
function create_custom_user_role() {

    $custom_role = get_role(\'subscriber\');
    $standard_user = add_role( \'register\', \'Register User\', $custom_role->capabilities  );
}
// End Activation function
我需要在插件激活时添加这三件事(两个页面和一个自定义角色),然后在插件停用时停用它们。。。我需要帮助;从我所看到的一切来看,应该只有一个activation_hook() 还有一个deactivation_hook(), 但我需要激活/停用多个功能。

谢谢你的帮助!

1 个回复
最合适的回答,由SO网友:fuxia 整理而成

您可以注册多个激活挂钩,这不是问题。或者可以使用调用其他函数的包装函数。

问题是:

add_action( \'template_include\', \'terms_redirect\' );
template_include 是一个筛选器,而不是一个操作,因此您应该使用add_filter().

$author_id = 1; 很危险,可能没有具有该id的作者。那么谁拥有该页面?使用get_current_user_id() 相反

get_page_by_title( $title ) 不足以检查。如果slug 是否已在使用?

结束

相关推荐

调试寄存器_ACTIVATION_HOOK()

我很好奇WordPress或PHP是否存在已知的问题,环境可能会退化到WordPress忽略register\\u activation\\u hook()调用的程度。我在运行MAMP的本地环境中紧张地工作了一个小时,这时我让一位同事在她的机器上测试代码;完全相同的代码在一台机器上工作,但在另一台机器上不工作。在本例中,我只是想将消息附加到日志文件中,我正在实时跟踪该日志文件。error_log(\"Plugin code is being processed\"); register_