Ask your WordPress questions! Pay money and get answers fast! (more info)

Need plugin to unregister CPT when deactivated. WordPress

  • SOLVED

I have a custom post type I typically register via functions.php. When I remove the code from functions.php the custom post type disappears from the WP-admin menu.

Perfect.

This morning I threw the code into a plugin, but now if I deactivate the plugin, it doesn't remove the CPT.

1) Can anyone show me what to add to the plugin file to make that work?

2) Also, I have some images the plugin uses. If I put the images in a folder in the plugin file, how can I reference those from a page template file?

Here's the plugin (and again, it's working fine. just need it to deregister the CPT when deactivated):




// team member CPT
function wpq_after_switch_theme() {
register_cpt_team_member();
wpq_activate_roles();
flush_rewrite_rules();
}

add_action( 'after_switch_theme', 'wpq_after_switch_theme' );
add_action( 'init', 'register_cpt_team_member' );

function register_cpt_team_member() {

$labels = array(
'name' => _x( 'Team Members', 'team_member' ),
'singular_name' => _x( 'Team Member', 'team_member' ),
'add_new' => _x( 'Add New Team Member', 'team_member' ),
'add_new_item' => _x( 'Add New Team Member', 'team_member' ),
'edit_item' => _x( 'Edit Team Member', 'team_member' ),
'new_item' => _x( 'New Team Member', 'team_member' ),
'view_item' => _x( 'View Team Member', 'team_member' ),
'search_items' => _x( 'Search Team Members', 'team_member' ),
'not_found' => _x( 'No team members found', 'team_member' ),
'not_found_in_trash' => _x( 'No team members found in Trash', 'team_member' ),
'parent_item_colon' => _x( 'Parent Team Member:', 'team_member' ),
'menu_name' => _x( 'Team Members', 'team_member' ),
);

$args = array(
'labels' => $labels,
'hierarchical' => false,
'description' => 'Team members',
'supports' => array( 'title', 'author' ),
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'menu_position' => 5,
'show_in_nav_menus' => true,
'publicly_queryable' => true,
'exclude_from_search' => false,
'has_archive' => true,
'query_var' => true,
'can_export' => true,
'rewrite' => array('slug' => 'team'),

//custom cpt capabilites
'capability_type' => 'team_member',
'map_meta_cap' => true,
);

register_post_type( 'team-member', $args );
}

function wpq_activate_roles() {

if(class_exists('WP_Roles')){

if(!isset($wp_roles)) $wp_roles = new WP_Roles();

if(is_object($wp_roles)) {

//team_member

$team_caps = $GLOBALS['wp_post_types']['team-member']->cap ;

foreach($team_caps as $key=>$value){

$wp_roles->add_cap('administrator', $value);

//$wp_roles->add_cap('editor', $value);

}

$wp_roles->add_role('team_member', 'Team Member', array('read' => true));

//$wp_roles->add_cap('team_member', 'upload_files');
$wp_roles->add_cap('team_member', 'read_team_member');
$wp_roles->add_cap('team_member', 'edit_team_member');
$wp_roles->add_cap('team_member', 'edit_team_members');
$wp_roles->add_cap('team_member', 'publish_team_members');
$wp_roles->add_cap('team_member', 'edit_published_team_members');
}

}

}

// team tools widget


function arphabet_widgets_init() {

register_sidebar( array(
'name' => 'Team Tools Sidebar',
'id' => 'team_tools',
'before_widget' => '<div class="biz-widget">',
'after_widget' => '</div>',
'before_title' => '<h2>',
'after_title' => '</h2>',
) );

}
add_action( 'widgets_init', 'arphabet_widgets_init' );

Answers (1)

2015-07-29

timDesain Nanang answers:

<blockquote>1) Can anyone show me what to add to the plugin file to make that work?</blockquote>

try change this part:

function wpq_after_switch_theme() {

register_cpt_team_member();

wpq_activate_roles();

flush_rewrite_rules();

}

add_action( 'after_switch_theme', 'wpq_after_switch_theme' );

to:
register_activation_hook(__FILE__, 'wpq_plugin_activate');
function wpq_plugin_activate() {
global $wpdb;

register_cpt_team_member();

wpq_activate_roles();

//rewrite
flush_rewrite_rules();
}

register_deactivation_hook(__FILE__, 'wpq_plugin_deactivate');
function wpq_plugin_deactivate() {
//rewrite
flush_rewrite_rules();
}


<blockquote>2) Also, I have some images the plugin uses. If I put the images in a folder in the plugin file, how can I reference those from a page template file?</blockquote>

if the directory structure: /plugin-dir-name/assets/img/
try the following code:
<img src="<?php echo plugins_url('',__FILE__));?>/assets/img/your-image.jpg" alt="alt-name" />


Kyler Boudreau comments:

Awesome. First part worked beautifully.

Question on the the URL - don't I need to somehow specify the plugin folder? For example, if my plugin folder is sample-plugin making the full image path sample-plugin/assets/images, what would I use in a template file?

Thanks man!


timDesain Nanang comments:

if used in a theme file:
<img src="<?php echo plugins_url();?>/sample-plugin/assets/images/image-file.jpg" alt="alt-name" />


Kyler Boudreau comments:

Brilliant. THANK YOU!