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 […]
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 […]