The functions below can be used to get the root domain for a WordPress site. If the site is a multisite network and you’re on http://www.site1.example.com/page-1, calling km_get_root_domain() would return example.com for example. It also works on single-site WP installations. View the code on Gist.
The code below can be used to get the URL of the main site in the WordPress Multisite Network: View the code on Gist.
CMB2 comes with default styles, but you may want to disable them if you’re outputting forms on the front end so they can be styled to match the rest of the site. Here’s how to disable CMB2 styles for all front end forms: View the code on Gist. Or if you’d rather prevent the CMB2 […]
If the BuddyPress activity stream component is enabled, the ‘Activity’ tab showing the activity stream is typically accessible at the main user domain URL. You can get that URL by calling bp_loggedin_user_domain(). And the profile tab URL is typically bp_loggedin_user_domain() . ‘profile/’.
The code below can be used to get the post types for a taxonomy in WordPress. This is useful if you know the taxonomy and want to get the post types associated with it. View the code on Gist. To do the opposite of this (get the taxonomies for a post type), see get_object_taxonomies().
In the WordPress editor, lines that have a line break at the end of them will be turned into an HTML line break tag <br> then they are output on the site. This is often desirable, but not always. The functions below show how to remove line breaks in WordPress. View the code on Gist. […]
WP-CLI‘s search-replace command is invaluable for changing all the URLs from a production or staging site to reflect your local development environment, among other things. Today I encountered something I hadn’t before; when running a search-replace on a database that I had already imported for local development, WP-CLI updated some of the database tables, but others […]
To get the URL (permalink) to the main blog posts page, you can use the function below. It will return the blog posts page URL whether the front page or another static page is set as the blog posts page. View the code on Gist.
By default, all Advanced Custom Fields metaboxes with a position/context of “side” are given a priority of “core” which means that they’ll be positioned below the WordPress Publish, Categories and Tags metaboxes, and possibly others. The function below can be used to bump up a priority of a metabox to “high” so that it appears […]
This class shows how to copy a file from one site to other sites within the same WordPress multisite network. This code uses wp_handle_sideload() to copy the file to all the sites on the network without attaching them to posts. If you DO want to attach the file to a post when it is sideloaded, you may want to […]
The function below can be used to get all of a user’s capabilities: View the code on Gist. You can pass a user’s ID or a user object to it, and it will return an array of capabilities like the one pictured below, or an empty array if the user has no capabilities or doesn’t […]
You can get the filename of the page template that the post in the WordPress admin is set to using: $page template = get_post_meta( $post->ID, ‘_wp_page_template’, true ); The function below gets the filename of the page template, then returns true if it matches the desired page template, or false if not.This is useful if you […]