Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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?

share|improve this answer
    
For When it comes to deactivating ... I know I can use remove_role('register'); for the custom user-role; but how should I go about this for the page-templates? –  Designer 17 Feb 13 at 0:58
    
@Designer17 Don’t delete the pages, your user might have used them for other purposes already. You can remove the template entries with delete_post_meta(). –  toscho Feb 13 at 1:00
    
Okay, thanks that makes sense. I'll give it a whirl! –  Designer 17 Feb 13 at 1:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.