Skip to content

Instantly share code, notes, and snippets.

@rutviksavsani
Created November 28, 2023 10:55
Show Gist options
  • Save rutviksavsani/4cd931d72a981b637a2b3d745ca5c359 to your computer and use it in GitHub Desktop.
Save rutviksavsani/4cd931d72a981b637a2b3d745ca5c359 to your computer and use it in GitHub Desktop.
Plugin My Artists: Registers a new Post type at a higher priority than 10.
<?php
/**
* Plugin Name: My Artists
* Author: Rutvik Savsani
* Version: 1.0.0
*/
/**
* Registers the `artists` post type.
*/
function artists_init() {
register_post_type(
'artists',
[
'labels' => [
'name' => __( 'Artists', 'my-test' ),
'singular_name' => __( 'Artists', 'my-test' ),
'all_items' => __( 'All Artists', 'my-test' ),
'archives' => __( 'Artists Archives', 'my-test' ),
'attributes' => __( 'Artists Attributes', 'my-test' ),
'insert_into_item' => __( 'Insert into Artists', 'my-test' ),
'uploaded_to_this_item' => __( 'Uploaded to this Artists', 'my-test' ),
'featured_image' => _x( 'Featured Image', 'artists', 'my-test' ),
'set_featured_image' => _x( 'Set featured image', 'artists', 'my-test' ),
'remove_featured_image' => _x( 'Remove featured image', 'artists', 'my-test' ),
'use_featured_image' => _x( 'Use as featured image', 'artists', 'my-test' ),
'filter_items_list' => __( 'Filter Artists list', 'my-test' ),
'items_list_navigation' => __( 'Artists list navigation', 'my-test' ),
'items_list' => __( 'Artists list', 'my-test' ),
'new_item' => __( 'New Artists', 'my-test' ),
'add_new' => __( 'Add New', 'my-test' ),
'add_new_item' => __( 'Add New Artists', 'my-test' ),
'edit_item' => __( 'Edit Artists', 'my-test' ),
'view_item' => __( 'View Artists', 'my-test' ),
'view_items' => __( 'View Artists', 'my-test' ),
'search_items' => __( 'Search Artists', 'my-test' ),
'not_found' => __( 'No Artists found', 'my-test' ),
'not_found_in_trash' => __( 'No Artists found in trash', 'my-test' ),
'parent_item_colon' => __( 'Parent Artists:', 'my-test' ),
'menu_name' => __( 'Artists', 'my-test' ),
],
'public' => true,
'hierarchical' => false,
'show_ui' => true,
'show_in_nav_menus' => true,
'supports' => [ 'title', 'editor' ],
'has_archive' => true,
'rewrite' => true,
'query_var' => true,
'menu_position' => null,
'menu_icon' => 'dashicons-admin-post',
'show_in_rest' => true,
'rest_base' => 'artists',
'rest_controller_class' => 'WP_REST_Posts_Controller',
]
);
}
add_action( 'init', 'artists_init', 11 );
/**
* Sets the post updated messages for the `artists` post type.
*
* @param array $messages Post updated messages.
* @return array Messages for the `artists` post type.
*/
function artists_updated_messages( $messages ) {
global $post;
$permalink = get_permalink( $post );
$messages['artists'] = [
0 => '', // Unused. Messages start at index 1.
/* translators: %s: post permalink */
1 => sprintf( __( 'Artists updated. <a target="_blank" href="%s">View Artists</a>', 'my-test' ), esc_url( $permalink ) ),
2 => __( 'Custom field updated.', 'my-test' ),
3 => __( 'Custom field deleted.', 'my-test' ),
4 => __( 'Artists updated.', 'my-test' ),
/* translators: %s: date and time of the revision */
5 => isset( $_GET['revision'] ) ? sprintf( __( 'Artists restored to revision from %s', 'my-test' ), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, // phpcs:ignore WordPress.Security.NonceVerification.Recommended
/* translators: %s: post permalink */
6 => sprintf( __( 'Artists published. <a href="%s">View Artists</a>', 'my-test' ), esc_url( $permalink ) ),
7 => __( 'Artists saved.', 'my-test' ),
/* translators: %s: post permalink */
8 => sprintf( __( 'Artists submitted. <a target="_blank" href="%s">Preview Artists</a>', 'my-test' ), esc_url( add_query_arg( 'preview', 'true', $permalink ) ) ),
/* translators: 1: Publish box date format, see https://secure.php.net/date 2: Post permalink */
9 => sprintf( __( 'Artists scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview Artists</a>', 'my-test' ), date_i18n( __( 'M j, Y @ G:i', 'my-test' ), strtotime( $post->post_date ) ), esc_url( $permalink ) ),
/* translators: %s: post permalink */
10 => sprintf( __( 'Artists draft updated. <a target="_blank" href="%s">Preview Artists</a>', 'my-test' ), esc_url( add_query_arg( 'preview', 'true', $permalink ) ) ),
];
return $messages;
}
add_filter( 'post_updated_messages', 'artists_updated_messages' );
/**
* Sets the bulk post updated messages for the `artists` post type.
*
* @param array $bulk_messages Arrays of messages, each keyed by the corresponding post type. Messages are
* keyed with 'updated', 'locked', 'deleted', 'trashed', and 'untrashed'.
* @param int[] $bulk_counts Array of item counts for each message, used to build internationalized strings.
* @return array Bulk messages for the `artists` post type.
*/
function artists_bulk_updated_messages( $bulk_messages, $bulk_counts ) {
global $post;
$bulk_messages['artists'] = [
/* translators: %s: Number of Artists. */
'updated' => _n( '%s Artists updated.', '%s Artists updated.', $bulk_counts['updated'], 'my-test' ),
'locked' => ( 1 === $bulk_counts['locked'] ) ? __( '1 Artists not updated, somebody is editing it.', 'my-test' ) :
/* translators: %s: Number of Artists. */
_n( '%s Artists not updated, somebody is editing it.', '%s Artists not updated, somebody is editing them.', $bulk_counts['locked'], 'my-test' ),
/* translators: %s: Number of Artists. */
'deleted' => _n( '%s Artists permanently deleted.', '%s Artists permanently deleted.', $bulk_counts['deleted'], 'my-test' ),
/* translators: %s: Number of Artists. */
'trashed' => _n( '%s Artists moved to the Trash.', '%s Artists moved to the Trash.', $bulk_counts['trashed'], 'my-test' ),
/* translators: %s: Number of Artists. */
'untrashed' => _n( '%s Artists restored from the Trash.', '%s Artists restored from the Trash.', $bulk_counts['untrashed'], 'my-test' ),
];
return $bulk_messages;
}
add_filter( 'bulk_post_updated_messages', 'artists_bulk_updated_messages', 10, 2 );
add_filter( 'jetpack_sitemap_news_sitemap_post_types', 'add_artists_to_news_sitemap', 10 );
function add_artists_to_news_sitemap( $post_types ) {
$post_types[] = 'artists';
return $post_types;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment