Custom Post Type Error

Hello, I’m new in Root Discourse, I have a problem, when I register a post type in extra.php, Wordpress get this warning: Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘technology_platform’ not found or invalid function name in /srv/www/wordpress-default/wp-includes/plugin.php on line 496.

This is my post-type code:
// Register Custom Post Type
function technology_platform() {

$labels = array(
	'name'                => 'Platforms',
	'singular_name'       => 'Platform',
	'menu_name'           => 'Technology',
	'name_admin_bar'      => 'Post Type',
	'parent_item_colon'   => 'Parent Item:',
	'all_items'           => 'All Items',
	'add_new_item'        => 'Add New Item',
	'add_new'             => 'Add New',
	'new_item'            => 'New Item',
	'edit_item'           => 'Edit Item',
	'update_item'         => 'Update Item',
	'view_item'           => 'View Item',
	'search_items'        => 'Search Item',
	'not_found'           => 'Not found',
	'not_found_in_trash'  => 'Not found in Trash',
);
$args = array(
	'label'               => 'post_type',
	'description'         => 'Technology Platform Section',
	'labels'              => $labels,
	'supports'            => array( 'title', 'editor', 'excerpt', 'thumbnail', 'custom-fields', ),
	'hierarchical'        => true,
	'public'              => true,
	'show_ui'             => true,
	'show_in_menu'        => true,
	'menu_position'       => 5,
	'menu_icon'           => 'dashicons-lightbulb',
	'show_in_admin_bar'   => true,
	'show_in_nav_menus'   => true,
	'can_export'          => true,
	'has_archive'         => true,
	'exclude_from_search' => false,
	'publicly_queryable'  => true,
	'capability_type'     => 'page',
);
register_post_type( 'post_type', $args );

}

// Hook into the ‘init’ action
add_action( ‘init’, ‘technology_platform’, 0 );

How I can solve this problem?
Thanks
Marco

you’re missing the namespace in the add_action part. it should be:
add_action( 'init', __NAMESPACE__ . '\\technology_platform', 0 );

check here:

and some more info about namespaces here: https://roots.io/upping-php-requirements-in-your-wordpress-themes-and-plugins/

4 Likes

Thanks so much! Now it works!

Don’t register custom post types in your theme. Those belong in a site-specific functionality plugin (or mu-plugin).

1 Like