Activate / Deactivate plugin

I'm working on my first plugin and am trying to use register_activation_hook() and register_deactivation_hook() ... I know I'm not doing this right:

/*====================================================*/
//  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

I need to add these three things when the plugin is activated ( two pages and a custom role ) and then deactivate them when the plugin is, well, deactivated ... I need help; from everything I've seen there is only supposed to be one activation_hook() and one deactivation_hook(), but I have more than one function I need activated/deactivated.

Thanks for any help!

Topic deactivated-plugin activation plugin-development Wordpress

Category Web


You can register multiple activation hooks, that’s not a problem. Or you can use a wrapper function that calls the other functions.

What is wrong, is this:

add_action( 'template_include', 'terms_redirect' );

template_include is a filter, not an action, so you should use add_filter().

$author_id = 1; is dangerous, there might be no author with that id. Who owns that page then? Use get_current_user_id() instead.

get_page_by_title( $title ) is not good enough for a check. What happens if the slug is already in use?

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.