Performance: Registering custom post types
This affects page load speed and server response time by adding new content types that WordPress must recognize and handle.
<?php
function register_my_cpt() {
register_post_type('book', array(
'labels' => array('name' => 'Books'),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'books'),
'supports' => array('title', 'editor'),
));
}
add_action('init', 'register_my_cpt');
<?php
function register_my_cpt() {
register_post_type('book', array(
'labels' => array('name' => 'Books'),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'books'),
'supports' => array('title', 'editor', 'thumbnail', 'custom-fields', 'comments', 'revisions', 'author'),
));
}
add_action('init', 'register_my_cpt');
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Register CPT with many supports | More meta boxes added to DOM | Multiple reflows due to complex admin UI | Higher paint cost from extra UI elements | [X] Bad |
| Register CPT with minimal supports | Fewer DOM nodes in admin UI | Single reflow with simpler UI | Lower paint cost | [OK] Good |
register_post_type function in WordPress?init hook is the standard place to register custom post types in WordPress.add_action('init', 'register_post_type') correctly hooks the function to run at initialization.add_action('init', function() {
register_post_type('book', [
'label' => 'Books',
'public' => true,
'supports' => ['title', 'editor']
]);
});function create_movie_post_type() {
register_post_type('movie', [
'label' => 'Movies',
'public' => true,
'supports' => 'title,editor'
]);
}
add_action('init', 'create_movie_post_type');Pass the test and this pattern moves unknown