generated from pascalmartineau/wp-skeleton
104 lines
5.1 KiB
PHP
104 lines
5.1 KiB
PHP
<?php
|
|
|
|
// Register 'project' post type
|
|
add_action( 'init', 'ccat_project_register_post_type' );
|
|
function ccat_project_register_post_type() {
|
|
register_post_type(
|
|
'project',
|
|
array(
|
|
'labels' => array(
|
|
'name' => __( "Projects", 'ccat' ),
|
|
'menu_name' => __( "Projects", 'ccat' ),
|
|
'singular_name' => __( "Project", 'ccat' ),
|
|
'add_new' => __( "Add New", 'ccat' ),
|
|
'add_new_item' => __( "Add New Project", 'ccat' ),
|
|
'new_item' => __( "New Project", 'ccat' ),
|
|
'edit_item' => __( "Edit Project", 'ccat' ),
|
|
'view_item' => __( "View Project", 'ccat' ),
|
|
'view_items' => __( "View Projects", 'ccat' ),
|
|
'search_items' => __( "Search Projects", 'ccat' ),
|
|
'not_found' => __( "No Projects found", 'ccat' ),
|
|
'not_found_in_trash' => __( "No Projects found in trash", 'ccat' ),
|
|
'parent_item_colon' => __( "Parent Project:", 'ccat' ),
|
|
'all_items' => __( "All Projects", 'ccat' ),
|
|
'archives' => __( "Project Archives", 'ccat' ),
|
|
'attributes' => __( "Project Attributes", 'ccat' ),
|
|
'insert_into_item' => __( "Insert into Project", 'ccat' ),
|
|
'uploaded_to_this_item' => __( "Uploaded to this Project", 'ccat' ),
|
|
'featured_image' => __( "Featured Image", 'ccat' ),
|
|
'set_featured_image' => __( "Set featured image", 'ccat' ),
|
|
'remove_featured_image' => __( "Remove featured image", 'ccat' ),
|
|
'use_featured_image' => __( "Use as featured image", 'ccat' ),
|
|
'filter_items_list' => __( "Filter Projects list", 'ccat' ),
|
|
'items_list_navigation' => __( "Projects list navigation", 'ccat' ),
|
|
'items_list' => __( "Projects list", 'ccat' ),
|
|
'item_published' => __( "Project published.", 'ccat' ),
|
|
'item_published_privately' => __( "Project published privately.", 'ccat' ),
|
|
'item_reverted_to_draft' => __( "Project reverted to draft.", 'ccat' ),
|
|
'item_scheduled' => __( "Project scheduled.", 'ccat' ),
|
|
'item_updated' => __( "Project updated.", 'ccat' ),
|
|
),
|
|
'public' => true,
|
|
'hierarchical' => false,
|
|
'show_ui' => true,
|
|
'show_in_nav_menus' => true,
|
|
'supports' => array( 'title', 'thumbnail', 'excerpt', 'revisions' ),
|
|
'has_archive' => false,
|
|
'rewrite' => array(
|
|
'slug' => 'projet/%project_id%',
|
|
'with_front' => false,
|
|
),
|
|
'query_var' => true,
|
|
'menu_icon' => 'dashicons-portfolio',
|
|
'show_in_rest' => true,
|
|
'rest_base' => 'project',
|
|
'rest_controller_class' => 'WP_REST_Posts_Controller',
|
|
'show_in_graphql' => true,
|
|
'graphql_single_name' => "Project",
|
|
'graphql_plural_name' => "Projects",
|
|
'graphql_interfaces' => array( 'Node' ),
|
|
)
|
|
);
|
|
}
|
|
|
|
// Custom 'project' post updated messages
|
|
add_filter( 'post_updated_messages', 'ccat_project_post_updated_messages' );
|
|
function ccat_project_post_updated_messages( $messages ) {
|
|
global $post;
|
|
$permalink = get_permalink( $post );
|
|
$preview_post_link_html = sprintf( ' <a target="_blank" href="%1$s">%2$s</a>', esc_url( get_preview_post_link( $post ) ), __( "Preview Project", 'ccat' ) );
|
|
$scheduled_post_link_html = sprintf( ' <a target="_blank" href="%1$s">%2$s</a>', esc_url( $permalink ), __( "Preview Project", 'ccat' ) );
|
|
$scheduled_date = date_i18n( __( "M j, Y @ H:i", 'ccat' ), strtotime( $post->post_date ) );
|
|
$view_post_link_html = sprintf( ' <a href="%1$s">%2$s</a>', esc_url( $permalink ), __( "View Project", 'ccat' ) );
|
|
$messages['project'] = array(
|
|
0 => '',
|
|
1 => __( "Project updated.", 'ccat' ) . $view_post_link_html,
|
|
2 => __( "Custom field updated.", 'ccat' ),
|
|
3 => __( "Custom field deleted.", 'ccat' ),
|
|
4 => __( "Project updated.", 'ccat' ),
|
|
5 => isset( $_GET['revision'] ) ? sprintf( __( "Project restored to revision from %s", 'ccat' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false,
|
|
6 => __( "Project published.", 'ccat' ) . $view_post_link_html,
|
|
7 => __( "Project saved.", 'ccat' ),
|
|
8 => __( "Project submitted.", 'ccat' ) . $preview_post_link_html,
|
|
9 => sprintf( __( 'Project scheduled for: %s.' ), '<strong>' . $scheduled_date . '</strong>' ) . $scheduled_post_link_html,
|
|
10 => __( 'Project draft updated.' ) . $preview_post_link_html,
|
|
);
|
|
|
|
return $messages;
|
|
}
|
|
|
|
// Add custom rewrite rules for project ID permalinks
|
|
add_action( 'init', 'ccat_project_add_rewrite_rules' );
|
|
function ccat_project_add_rewrite_rules() {
|
|
add_rewrite_rule( '^projet/([0-9]+)/?$', 'index.php?post_type=project&p=$matches[1]', 'top' );
|
|
}
|
|
|
|
// Filter project permalinks to use ID instead of slug
|
|
add_filter( 'post_type_link', 'ccat_project_custom_permalink', 10, 2 );
|
|
function ccat_project_custom_permalink( $permalink, $post ) {
|
|
if ( get_post_type( $post ) === 'project' ) {
|
|
$permalink = home_url( "/projet/$post->ID/" );
|
|
}
|
|
return $permalink;
|
|
}
|