Search code examples
wordpresspluginscustom-post-typewp-admin

Change the default global edit link that lists my CustomPostType (edit.php?post_type=CPT), to use a plugin page link instead


I'm working on a plugin (with a https://wppb.me/ plugin boilerplate base, if relevant).

I defined my custom post type, with the editor options I want, that I can edit with the default /wp-admin/post.php?post={id}&action=edit

The listing of those CPT is done on my plugin pages only (no specific menu for the CPT) and I want to keep it that way.

Problem: I'd like to change the base edit link of this post type when no ID is specified ( eg : /wp-admin/edit.php?post_type=CPT ) to be the URL of my plugin ( eg : /wp-admin/admin.php?page=myplugin )

This, mostly because in Gutenberg Editor Fullscreen, the top left wordpress logo links to the /wp-admin/edit.php?post_type=CPT that I don't wont to use nor show. I'd like this link to be a page of my plugin (here, it would be the homepage of my plugin)

I tried with a filter on get_edit_post_link when there is no ID provided, but it doesn't seem to be the correct way to fix my problem.

Any tips or help to get me in the right direction in my research are welcome !


Solution

  • I found one of the possible solutions. After entering the site, you must redirect the user.

    function wp_custom_update_meta_cache_filter() {
        global $pagenow, $typenow;
        if ( $pagenow == 'edit.php' && $typenow == 'CPT' ) {
            wp_safe_redirect();
            die();
        }
    }
    add_action( 'admin_init', 'wp_custom_update_meta_cache_filter' );
    

    Based in (233 line): https://github.com/dipolukarov/wordpress/blob/master/wp-content/plugins/woocommerce/admin/woocommerce-admin-init.php