If you want to get the file mime type for a remote file and you have its URL, the function below can be used. Example: If the remote file is a png image with a url of http://example.com/directory/image.png?ver=123, passing in that URL to this function would return image/png. View the code on Gist.
The get_the_excerpt() entry in the WordPress Codex says that it returns “…either a user-supplied excerpt, that is returned unchanged, or an automatically generated word-counted trimmed-down version of the full post content” (although the more up-to-date WordPress Developer Resources entry has no such language). I find that calling get_the_excerpt() doesn’t work when a post has post content but not a post excerpt. […]
The function below can be used to get a user’s first and last name with a space between them, like “Kellen Mace”. If they only have a first name saved, it will return that, and it will return their WP login as a last resort. This is useful if you want to display the users’ […]
WordPress has a built in username_exists() function to check if a user exists by their username, but there isn’t a similar function for checking if a user exists by their user ID. I’ve seen posts online where folks suggest a way of doing that with a direct database query using $wpdb. While that works, the function […]
The functions below can be used to get the root domain of 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 instance. 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 […]