• Resolved herrstrietzel

    (@15broetchenmann)


    Dear CPT UI developers,
    I’ve exported a custom post type years ago to a function.php snippet.

    function cptui_register_my_cpts_team() {
    	$labels = array(
    		"name" => __( 'team', '' ),
    		"singular_name" => __( 'team', '' ),
    		);
    
    	$args = array(
    		"label" => __( 'team', '' ),
    		"labels" => $labels,
    		"description" => "",
    		"public" => true,
    		"publicly_queryable" => true,
    		"show_ui" => true,
    		"show_in_rest" => true,
    		"rest_base" => "",
    		"has_archive" => false,
    		"show_in_menu" => true,
    		"exclude_from_search" => false,
    		"capability_type" => "post",
    		"map_meta_cap" => true,
    		"hierarchical" => false,
    		"rewrite" => array( "slug" => "team", "with_front" => true ),
    		"query_var" => true,
    		"menu_icon" => "dashicons-admin-users",
    		"supports" => array( "title", "editor", "thumbnail", "custom-fields", "revisions" ),					
    		);
    	register_post_type( "team", $args );
    }
    

    This post type works flawlessly, but I’d like to “remap” this existing post type to the CPT UI.

    When I try to define a new post type with this name I get an error message (Post type already exists).
    So my question is there any way to remap/register this existing post type to be controlled via CPT UI?

    Best regards,
    herrstrietzel

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    This should end up being pretty quick, but it’ll have a moment of the post type not being registered. Comment out the add_action( 'init', 'cptui_register_my_cpts_team' ); line that you have around the code shown above, and then save the CPTUI’s UI version. That should prevent it from getting registered by the code long enough to avoid our existing post types clash prevention. Once done, you can delete the code version and confirm all is still registered.

    Thread Starter herrstrietzel

    (@15broetchenmann)

    @tw2113 Thanks for your response!

    Unfortunately, this didn’t work.

    I’ve also tried to

    • log out
    • clear caches / permalinks etc
    • switch to a standard theme like twentyXX

    CPTUI was still throwing an error message like (translated as I’m using a german WP version)

    Please choose a different content type name. “team” matches an existing page title form, which can cause problems.

    So apparently CPTUI is very strict/cautious when it comes to existing post types – which is actually great!

    What actually solved the problem, was temporarily out commenting some lines in the “post-types.php”

        if ( true === $slug_exists ) {
    
            add_filter( 'cptui_custom_error_message', 'cptui_slug_matches_post_type' );
    
            return 'error';
    
        }
    
        if ( 'new' === $data['cpt_type_status'] ) {
    
            $slug_as_page = cptui_check_page_slugs( $data['cpt_custom_post_type']['name'] );
    
            if ( true === $slug_as_page ) {
    
                add_filter( 'cptui_custom_error_message', 'cptui_slug_matches_page' );
    
                return 'error';
    
            }
    
        }

    Disclaimer for other users:
    don’t hack plugin files!!!
    Don’t try this unless you’re working on a local server copy
    Always backup your DB before applying changes like this.

    So I highly appreciate CPTUI’s safety measures and my use case is barely relevant for the majority of users.

    However, it might be a great option to actually allow adding already existing post-types via an additional checkbox/confirmation.

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    My bit about commenting out the add_action should have worked. However based on this message:

    Please choose a different content type name. “team” matches an existing page title form, which can cause problems.

    It sounds like you also have a PAGE named “team” as well as the post type. Does that sound accurate? If yes, then that’s where it would have still been failing from.

    Thread Starter herrstrietzel

    (@15broetchenmann)

    @Michael Beckwith: Thanks! You’re right! gosh … reading helps.

    In my case I had a slug conflict due to a custom post-type landing – which should be eliminated.

    I could also solve it by

    • temporarily changing the page slug of the page “team”
    • registering the post type “team” via cpt-UI
    • reverting the previously changed page slug
    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Pretty much. However with the last 3 parts, you’re potentially still going to run into issues in the case of the “team” post type needing archives, unless you set a different archive slug.

    A page named mysite.com/team/ and then a CPT archive url of mysite.com/team/ is going to cause potential issues. However if the archive is say /teammates/ then you should be fine.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Remap or register existing post type to CPT UI exported as function.php snippet’ is closed to new replies.