That shift from using a tool for a task to reshaping it for a future one is maybe the thing I love most about my efforts at coding (given my lesser skills with real hand tools).
Quite often I find myself making small tweaks to things I know are in my projects; but will anyone else ever know?
Fixed software bug (my own) likely no one will ever notice.
— Alan Levine (@cogdog) July 24, 2018
For a new WordPress project setting up some template sites for Portfolios, in the interest of speed, I’ve recommended using the JetPack feature that creates custom post types for portfolio content (and some of the themes built to use them).
A “custom post type” means that when people add content, it looks and acts like a normal blog post, but the content is separate, so the blog part can be used more for reflection, news, etc. The Portfolio content also gets its own category and tag taxonomies.
The itch under my skin is the terminology used in the custom post-type. Items added to a portfolio are called “Projects” which might make sense if you are building a portfolio of your web clients, crafts, etc, but seems a bit off to me for a learning portfolio. And for the school I am working with they use “Artifacts”.
So I dug into seeing how the Jetpack plugin works. And that is significant, as I can see all of their code. And having coded my own custom post types for projects like the DS106 Assignment Bank theme I have a fair understanding of the register_post_type functions.
The trickier part was finding a way to modify this in a child theme. With a bit of poking around I found a tip on a blog showing that there was a WordPress filter hook built to do this register_post_type_args, where I was able to change all the labels used for “Projects” in the dashboard to “Artifacts”
I just modified the labels
part of the array I saw in the Jetpack code used to create a custom post type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# ----------------------------------------------------------------- # Modify the Portfolio Custom Post type # Rename items as "artifacts" rather than "Projects" # h/t https://gregrickaby.com/2016/06/modify-wordpress-custom-post-type/ # ----------------------------------------------------------------- /** * Filter the Jetpack Portfolio CPT to register more options. * * @param $args array The original CPT args. * @param $post_type string The CPT slug. * * @return array */ function twu_portfolio_modify_cpt( $args, $post_type ) { // If not Jetpack Portfolio CPT or not supported, bail. if ( 'jetpack-portfolio' !== $post_type or !current_theme_supports( 'jetpack-portfolio' ) { ) { return $args; } $portfolio_args = array( 'labels' => array( 'name' => esc_html__( 'Artifacts', 'jetpack' ), 'singular_name' => esc_html__( 'Artifact', 'jetpack' ), 'menu_name' => esc_html__( 'Portfolio', 'jetpack' ), 'all_items' => esc_html__( 'All Artifacts', 'jetpack' ), 'add_new' => esc_html__( 'Add New', 'jetpack' ), 'add_new_item' => esc_html__( 'Add New Artifact', 'jetpack' ), 'edit_item' => esc_html__( 'Edit Artifact', 'jetpack' ), 'new_item' => esc_html__( 'New Artifact', 'jetpack' ), 'view_item' => esc_html__( 'View Artifact', 'jetpack' ), 'search_items' => esc_html__( 'Search Artifacts', 'jetpack' ), 'not_found' => esc_html__( 'No Artifacts found', 'jetpack' ), 'not_found_in_trash' => esc_html__( 'No Artifacts found in Trash', 'jetpack' ), 'filter_items_list' => esc_html__( 'Filter artifacts list', 'jetpack' ), 'items_list_navigation' => esc_html__( 'Artifact list navigation', 'jetpack' ), 'items_list' => esc_html__( 'Artifacts list', 'jetpack' ), ), ); // Merge args together. return array_merge( $args, $portfolio_args ); } add_filter( 'register_post_type_args', 'twu_portfolio_modify_cpt', 11, 2 ); |
But there is more as there are labels used for the two custom taxonomies created for the categories and tags. My hunch, which was wrong, was that there would be a wordpress hook for modifying a custom taxonomy. I did find what I was looking for in StackExchange (where I find maybe 80% of my code help)- use a wordpress to get all the info about a custom taxonomy, replace parts, and spit it back out, more or less child functioning it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# ----------------------------------------------------------------- # Modify the Portfolio Custom Taxonomies # Rename items as "artifacts" rather than "Projects" # h/t https://wordpress.stackexchange.com/a/161789/14945 # ----------------------------------------------------------------- function twu_portfolio_modify_taxonomy() { // get the arguments of the already-registered taxonomy for types $portfolio_type_args = get_taxonomy( 'jetpack-portfolio-type' ); // returns an object // bail if none found if (!$portfolio_type_args) return; // make changes to the args $portfolio_type_args->labels = array( 'name' => esc_html__( 'Artifact Types', 'jetpack' ), 'singular_name' => esc_html__( 'Artifact Type', 'jetpack' ), 'menu_name' => esc_html__( 'Artifact Types', 'jetpack' ), 'all_items' => esc_html__( 'All Artifact Types', 'jetpack' ), 'edit_item' => esc_html__( 'Edit Artifact Type', 'jetpack' ), 'view_item' => esc_html__( 'View Artifact Type', 'jetpack' ), 'update_item' => esc_html__( 'Update Artifact Type', 'jetpack' ), 'add_new_item' => esc_html__( 'Add New Artifact Type', 'jetpack' ), 'new_item_name' => esc_html__( 'New Artifact Type Name', 'jetpack' ), 'parent_item' => esc_html__( 'Parent Artifact Type', 'jetpack' ), 'parent_item_colon' => esc_html__( 'Parent Artifact Type:', 'jetpack' ), 'search_items' => esc_html__( 'Search Artifact Types', 'jetpack' ), 'items_list_navigation' => esc_html__( 'Artifact type list navigation', 'jetpack' ), 'items_list' => esc_html__( 'Artifact type list', 'jetpack' ), ); $portfolio_type_args->rewrite = array( 'slug' => 'artifact-type' ); // re-register the taxonomy register_taxonomy( 'jetpack-portfolio-type', 'jetpack-portfolio', (array) $portfolio_type_args ); // get the arguments of the already-registered taxonomy for tags $portfolio_tag_args = get_taxonomy( 'jetpack-portfolio-tag' ); // returns an object // make changes to the args $portfolio_tag_args->labels = array( 'name' => esc_html__( 'Artifact Tags', 'jetpack' ), 'singular_name' => esc_html__( 'Artifact Tag', 'jetpack' ), 'menu_name' => esc_html__( 'Artifact Tags', 'jetpack' ), 'all_items' => esc_html__( 'All Artifact Tags', 'jetpack' ), 'edit_item' => esc_html__( 'Edit Artifact Tag', 'jetpack' ), 'view_item' => esc_html__( 'View Artifact Tag', 'jetpack' ), 'update_item' => esc_html__( 'Update Artifact Tag', 'jetpack' ), 'add_new_item' => esc_html__( 'Add New Artifact Tag', 'jetpack' ), 'new_item_name' => esc_html__( 'New Artifact Tag Name', 'jetpack' ), 'search_items' => esc_html__( 'Search Artifact Tags', 'jetpack' ), 'popular_items' => esc_html__( 'Popular Artifact Tags', 'jetpack' ), 'separate_items_with_commas' => esc_html__( 'Separate tags with commas', 'jetpack' ), 'add_or_remove_items' => esc_html__( 'Add or remove tags', 'jetpack' ), 'choose_from_most_used' => esc_html__( 'Choose from the most used tags', 'jetpack' ), 'not_found' => esc_html__( 'No tags found.', 'jetpack' ), 'items_list_navigation' => esc_html__( 'Artifact tag list navigation', 'jetpack' ), 'items_list' => esc_html__( 'Artifact tag list', 'jetpack' ), ); $portfolio_tag_args->rewrite = array( 'slug' => 'artifact-tag' ); // re-register the taxonomy register_taxonomy( 'jetpack-portfolio-tag', 'jetpack-portfolio', (array) $portfolio_tag_args ); } // hook it up to 11 so that it overrides the original register_taxonomy function add_action( 'init', 'twu_portfolio_modify_taxonomy', 11 ); |
And thus I have minor success, I’ve changed all references (I hope) in the themes I am making so that the things inside a portfolio are called “Artifacts” rather than “Projects”.
Today's WordPress Sledgehammer Hacking is modding the Jetpack Custom Post type for Portfolios call items "Artifacts" rather than "Projects" #moreCodeTwiddlingThanYoudKnow pic.twitter.com/Seo9giKMr7
— Alan Levine (@cogdog) July 27, 2018
But while I was in the Jetpack code, I noticed use of a function to change the messages that appear when a custom post type is modified (or even in some of my SPLOTs like TRU Collector, where I renamed “Posts” to be “Collectibles”). WordPress always responds with “post updated” or “post published” when I no longer or never call them posts.
I found the WordPress hook post_updated_messages that is used but no real explanation I could decipher. So I figured out from the Jetpack code, and a post from What About WordPress that you simply return an array with reworded versions of the 10 messages the WordPress admin uses.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
function twu_portfolio_updated_messages( $messages ) { global $post; $messages['jetpack-portfolio'] = array( 0 => '', // Unused. Messages start at index 1. 1 => sprintf( __( 'Artifact updated. <a href="%s">View item</a>', 'jetpack'), esc_url( get_permalink( $post->ID ) ) ), 2 => esc_html__( 'Custom field updated.', 'jetpack' ), 3 => esc_html__( 'Custom field deleted.', 'jetpack' ), 4 => esc_html__( 'Artifact updated.', 'jetpack' ), /* translators: %s: date and time of the revision */ 5 => isset( $_GET['revision'] ) ? sprintf( esc_html__( 'Artifact restored to revision from %s', 'jetpack'), wp_post_revision_title( (int) $_GET['revision'], false ) ) : false, 6 => sprintf( __( 'Artifact published. <a href="%s">View Artifact</a>', 'jetpack' ), esc_url( get_permalink( $post->ID ) ) ), 7 => esc_html__( 'Artifact saved.', 'jetpack' ), 8 => sprintf( __( 'Artifact submitted. <a target="_blank" href="%s" rel="noopener noreferrer">Preview artifact</a>', 'jetpack'), esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) ), 9 => sprintf( __( 'Artifact scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s" rel="noopener noreferrer">Preview artifact</a>', 'jetpack' ), // translators: Publish box date format, see http://php.net/date date_i18n( __( 'M j, Y @ G:i', 'jetpack' ), strtotime( $post->post_date ) ), esc_url( get_permalink( $post->ID ) ) ), 10 => sprintf( __( 'Artifact item draft updated. <a target="_blank" href="%s" rel="noopener noreferrer">Preview artifact</a>', 'jetpack' ), esc_url( add_query_arg( 'preview', 'true', get_permalink( $post->ID ) ) ) ), ); return $messages; } add_filter( 'post_updated_messages', 'twu_portfolio_updated_messages', 11); |
I’m folding these into the SPLOT family themes soon.
Tiny stuff.
The last little bit was following the Jetpack plugin code where it changed the heading for Column listing of content in the dashboard from “Title” to “Project”, so I follow suit and change it once more to “Artifact”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
add_filter( 'post_updated_messages', 'twu_portfolio_updated_messages', 11); function twu_portfolio_edit_admin_columns( $columns ) { // change 'Project' to 'Artifact' $columns['title'] = __( 'Artifact', 'jetpack' ); if ( current_theme_supports( 'post-thumbnails' ) ) { // add featured image before 'Project' $columns = array_slice( $columns, 0, 1, true ) + array( 'thumbnail' => '' ) + array_slice( $columns, 1, NULL, true ); } return $columns; } add_filter( 'manage_jetpack-portfolio_posts_columns', 'twu_portfolio_edit_admin_columns' , 11); |
Because I can.
All of the add_filter()
bits I wrote have a priority value of 11 so it happens after Jetpack does its stuff (using the default 10 value).
If everything works well, you likely never notice it.
But I know it’s there, and in the process, I figured out tings I can use in future projects. Or artifacts. Wait, don’t substitute that!)
Featured Image:
Serious question – what’s the merits of baking these changes into a theme versus doing a custom language pack? I guess if it’s baked into a theme it will survive Jetpack upgrades, which a local translation might not? The flipside is a language translation would let this change be used with any theme, which if it’s institutional specific terminology, might be attractive as well?
Serious answer, I’m not quite sure. I’ve not done much to anything with language packs, and what impact does this have on changing the language as needed in non English-landia. A few changes (url slugs) are outside of that scope. But something to think about and look into
I had a quick poke about and since the Jetpack plugin is top quality it comes with language files for most languages. They’re part of the plugin structure though, so still a question about whether they would survive an upgrade of Jetpack (especially if you were just changing the file for your chosen language). I don’t know if it’s possible to put translation files for plugins where ever the core WP translation files are (or bake them in instead). It’s got me thinking though that it could be quite useful to have a default blog site language which was your language plus institution e.g. English(UK) – Edinburgh. It could be baked into the default template site on a WP multi-site install then I’ve got all my institution specific language right there from the start. Hmm….I started pondering this because we want to change some default labels to reference our local authentication system. One to keep pondering.
Yes, this offers some potential, it looks like via https://wordpress.stackexchange.com/a/225220/14945 and confirmed here https://developer.wordpress.org/themes/functionality/localization/#using-localizations you can create a server side directory at /wp-content/languages for .pot files that do not have to come from plugins. Then you define that as the default language in the config.php for the site. It might also be available from the language choices at a user level. Just guessing.