DevinVinson / WordPress-Plugin-Boilerplate

[WordPress] A foundation for WordPress Plugin Development that aims to provide a clear and consistent guide for building your plugins.
http://wppb.io
7.68k stars 2.26k forks source link

Uninstall.php to remove custom post type properly. #329

Open devcon7 opened 9 years ago

devcon7 commented 9 years ago

I am trying to refine the uninstall.php to remove all the metaboxes, custom post types, custom fields, and etc.

I was able to get the options, and metaboxes removed, but I think I have to use remove_action() to remove the custom post type.

However, the custom post type is loaded from the includes\class-plugin-name.php under "private function define_admin_hooks(){ ... $this->loader->add_action( 'init', $plugin_admin, 'plugin_name_register_custom_post_type' ); ... }

I can't seem to find a proper way of removing the custom post type registered via add_action.

Do I do just do "remove_action();" ? If so, can you post an example code?

Thank you!

mAAdhaTTah commented 9 years ago

The custom post type won't be registered if the function that registers it no longer runs, e.g. if your plugin is uninstalled. What you want to do (if necessary) in your uninstall.php file is clear up the database tables behind you. The removal of the plugin means your meta boxes, custom post types, etc. are no longer registered by your code.

rakibhoossain commented 6 years ago

Hey! Plugin could't be deleted by deactivated button. Here is my code:

// If uninstall not called from WordPress, then exit.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
    exit;
}

    /**
    * Delete all post and meta data.
    */
    function vcpm_delete_plugin(){

        $vcpm_posts_data = array(
            array(
                'post' => get_posts(
                    array(
                        'numberposts' => -1,
                        'post_type' => 'services',
                        'post_status' => 'any',
                    )
                )
            ),
            array(
                'post' => get_posts(
                    array(
                        'numberposts' => -1,
                        'post_type' => 'skill',
                        'post_status' => 'any',
                    )
                )
            ),
            array(
                'post' => get_posts(
                    array(
                        'numberposts' => -1,
                        'post_type' => 'portfolio',
                        'post_status' => 'any',
                    )
                )
            ),
            array(
                'post' => get_posts(
                    array(
                        'numberposts' => -1,
                        'post_type' => 'testimonial',
                        'post_status' => 'any',
                    )
                )
            ),
            array(
                'post' => get_posts(
                    array(
                        'numberposts' => -1,
                        'post_type' => 'team',
                        'post_status' => 'any',
                    )
                )
            ),
            array(
                'post' => get_posts(
                    array(
                        'numberposts' => -1,
                        'post_type' => 'partner',
                        'post_status' => 'any',
                    )
                )
            )

        );

        /**
         * Delete post.
         */
        foreach ( $vcpm_posts_data as $post_item ) {
            foreach ( $post_item['post'] as $post ) {
                wp_delete_post( $post->ID, true );
            }
        }

        /**
         * Delete Meta.
         */
        require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-vcpm-meta.php';
        $meta_data = Vcpm_Meta::vcpm_meta_box();

        foreach ($meta_data as $meta_item) {
            foreach ($meta_item['fields'] as $field) {
                 delete_post_meta_by_key( $field['id'] );
            }  
        }
    }

    //vcpm_delete_plugin();
jonathanbossenger commented 6 years ago

@serakib as @mAAdhaTTah pointed out, the removal of your plugin means the custom functionality of your plugin is lost, so for example any custom post types your plugin registers are not available.

You will be better off running custom sql queries on the database to clear out any data.