Add edit link to admin bar when viewing a post type archive

WordPress adds a neat 'Edit' link to the admin bar when you are logged in and viewing a post, page, or taxonomy. But by default there is no edit link when viewing a post type archive.

/**
 * Add edit link to admin bar when viewing a post type archive.
 *
 * @param WP_Admin_Bar $wp_admin_bar The admin bar object.
 */
function prefix_add_archive_edit_link_to_admin_bar( $wp_admin_bar ) {

	global $wp_query;

	if (
		// Not much use for the link in the admin.
		! is_admin()

		// Only on post archive pages.
		&& is_post_type_archive()

		// The post type should be showing its UI.
		&& $wp_query->queried_object->show_ui

		// And the current user should be able to edit posts.
		&& current_user_can( $wp_query->queried_object->cap->edit_posts ) ) {

		$title = sprintf(
			_x( 'Edit %s', 'admin bar edit archive link', 'textdomain' ),
			$wp_query->queried_object->label
		);

		$wp_admin_bar->add_menu(array(
			'id'    => 'edit',
			'title' => $label,
			'href'  => admin_url( 'edit.php?post_type=' .  $wp_query->queried_object->name ),
		));
	}
}

add_action( 'admin_bar_menu', 'prefix_add_archive_edit_link_to_admin_bar', 80 );

Responsive element width based on its height without JavaScript

Maintaining the correct height based on an element's width is a breeze using the trusty CSS padding-bottom trick.

This doesn't work when you need to maintain the aspect ratio of an element based on its height 😭.

But stackoverflow:

HTML

<div class="container">
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7">
<div class="content">My Cool Content!</div>
</div>

CSS

.container {
	position: relative;
	display: inline-block;
	// The height of the container.
	height: 200px;
}

.container > img {
	// How wide should the container be
	// as a percentage of its width?
	height: 100%;
}

.content {
	position: absolute;
	top: 0;
	bottom: 0;
	left: 0;
	right: 0;
}

💖 it + bonus points because it uses a spacer.gif.

Google Analytics without Cookies in WordPress

I somehow felt the urge to use Google Analytics to track page views on my portfolio site. But Google Analytics sets cookies and in Europe we're required to show a cookie consent warning because cookie monsters ugh...

In the spirit of keeping things simple I used an example by Foture of using fingerprint.js for that purpose. Which turned in to a WordPress plugin and yay no cookie and no notification required!

Clone on Github or download plugin as a Zip file.

Cookie Monster eating cookie
Via GIPHY

Setting your analytics tracking ID

The plugin has no settings page. Set your own tracking id by adding a filter:

add_filter(
	'analytics_without_cookies_tracking_id',
	function() { return 'UA-XXXXXXX-XX'; }
);

Visitor IP addresses are anonymized by default

Because we're required to do so by law in Germany. Turn it off if you are so inclined:

add_filter( 'analytics_without_cookies_anonymize_ip', '__return_true' );

Logged in users are not tracked by default

Turn tracking logged in users on:

add_filter( 'analytics_without_cookies_ignore_logged_in_users', '__return_false' );

Update a forked git clone with latest changes from its original source

Note to self:

...want to create a pull request on a repository I already cloned some time ago, but... how do I get it up to date again?

Probably hopefully only changed stuff in a branch, so let's checkout the master branch first:

git checkout master

Add upstream for the remote repository:

git remote add upstream git@github.com:bueltge/Remove-Comments-Absolutely.git

Fetch upstream repository:

git fetch upstream

Finally, merge into master branch:

git merge upstream/master

Via Stack Overflow, of course.

Fun with WordPress PHP Coding Standards in Sublime Text

Writing WordPress plugins and themes is my kind of fun, really. Adhering to the WordPress PHP Coding Standards while doing that is not only even more fun but also helps "...maintain a consistent style so the code can become clean and easy to read at a glance".

Here is how I managed to get automated tests against the WordPress coding standards up and running on a Mac using Homebrew and Git. Continue reading "Fun with WordPress PHP Coding Standards in Sublime Text"