0

I am building a WP plugin and I want to replace the admin list page (edit.php?post_type=my_cpt) for my CPT. The CPT should not have any 'add new' buttons as new content will only be imported. The existing posts should still be editable and removable. I can hide the 'add new' buttons and links by defining the capabilities of my CPT as follows:

'show_in_menu'     => 'my_cpt',
'capability_type'  => 'post',
    'capabilities' => array(
        'create_posts' => 'do_not_allow',       
    ),  
    'map_meta_cap' => true

I also created a custom menu with the same slug name as the CPT. (links to admin.php?page=my_cpt_page) and the default 'all posts' link is removed from the menu.

//Main menu (this page will have a custom table for my CPT)
add_menu_page(
    __( 'My CPT', 'cpt' ),                  // Page Title
    'My Test CPT',                          // Menu Title
    'edit_posts',                           // capability
    'my_cpt',                               // menu slug
     array( $this->main_page, 'render' ),    // action
    'dashicons-email-alt',                  // icon
    6                                       // position
); 
//remove menu created by wordpress
remove_submenu_page( 'my_cpt', 'edit.php?post_type=my_cpt' );

Now, when I go to an edit page of a single item (eg: post.php?post=666&action=edit) and I press the 'Move to Trash' link, Wordpress will redirect me to the hidden page located at edit.php?post_type=my_cpt.

Is there any way to redirect edit.php?post_type=my_cpt to my own custom page (admin.php?page=my_cpt_page) without altering the .htaccess file? Or, even better, is there a way to replace/override the edit.php screen for my CPT?

Thanks in advance

Vincent.

  • Note that you would need to recreate what edit.php does on your new page, as well as filter all the edit links, this isn't a trivial task ( although it is easier if you're using the block editor/gutenberg ) – Tom J Nowell Jun 12 at 16:46
  • my custom page already has the necessary functionality. It has a custom WP_List_table and an import form above the table. My only goal is to add that import form above the table (without using Js/jQuery). I just found another way to manipulate the existing edit.php page by adding this filter: add_filter( 'views_edit-me_ctp', "my_ctp_custom_list_table"); But the table views ('all','published','Trash') are gone from the table when I added this filter. How can I get the views back? (i am not using my custom table in this test). – Vinzz Jun 12 at 17:42
  • Without seeing the way you used the filter it's not possible to answer that question. It also seems I misinterpreted your question, I thought you were talking about adding an edit page for your custom listing to link to that still conformed to your new URL – Tom J Nowell Jun 12 at 17:54
  • 1
    After playing around for more than a day I think I just found a solution by using the 'views_edit' filter which is using the built-in table. These two articles helped me: replace post type table and manipulate views. I'll post an example as an answer after I have fully tested and validated my solution – Vinzz Jun 12 at 18:21
0

I found a way to add html above the post type table which is generated by WP.

add_filter( 'views_edit-my_cpt', "custom_list_table"); 
function custom_list_table( $views ) {
    /* Print form */ ?>         
    <form method="post" action="//your.url/?action=action">
        <? wp_nonce_field( 'action-form', 'action_nonce' );  ?>     
        <label for="field1"><? _e('your label') ?></label><br />
        <input id="field1" type="text" name="field1"><input type="submit" name="submit" id="submitbtn" class="button button-primary" value="<? _e('Go')?> ">
    </form><?   
    return $views;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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