Sunday, November 6, 2022
HomeWordPress Developmentphp - dynamically register customized put up kind

php – dynamically register customized put up kind


I’ve this code in a customized plugin.

    public operate create_new_cpt( WP_REST_Request $request )
    {
        $args = $request->get_param('newCptDetails');

        $cpt = register_post_type( 
            $args['name'], 
            [
                'label' => $args['label'],
                'description' => $args['desc'],
                'show_ui' => true,
                'show_in_menu' => true,
                'show_in_rest' => true
            ]
        );

        if( $cpt ){
            add_option( 'cpt_name', $args['name'] );
            add_option( 'cpt_label', $args['label'] );
            add_option( 'description', $args['desc'] );
            return $cpt;
        } else {
            return 'not registered';
        }
    }

The code is a callback operate for a customized REST route that’s presupposed to get all of the wanted params to create a brand new CPT. I’ve executed a check however I’ve seen that the CPT just isn’t registered and I believe it is as a result of I am not calling the init hook. In my plugin code I am creating 4 routes, one is to create new cpt, one other to listing all cpt and the final two shall be used to allow or disable a CPT.

    public operate setup_admin_page_routes()
    {

        register_rest_route( 
            $this->namespace, 
            '/list-cpt', 
            [
                'methods' => 'GET',
                'callback' => [$this, 'list_cpt'],
                'permissions_callback' => [$this, 'permissions_callback']
            ], 
        );

        register_rest_route( 
            $this->namespace, 
            '/create-cpt', 
            [
                'methods' => 'POST',
                'callback' => [$this, 'create_new_cpt'],
                'permissions_callback' => [$this, 'permissions_callback']
            ], 
        );

        register_rest_route( 
            $this->namespace, 
            '/disable-cpt', 
            [
                'methods' => 'POST',
                'callback' => [$this, 'deactivate_cpt'],
                'permissions_callback' => [$this, 'permissions_callback']
            ], 
        );

    }

How I can name the init hook insite my operate and the way I’ll allow or disable or delete CPT when from the admin web page I am creating the person will give the associated command?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments