Responsive element width based on its height without JavaScript

Maintaining an element’s aspect ratio by changing its height based on its width is a breeze using the trusty old CSS padding-bottom trick.

This doesn’t work when you need to maintain the aspect ratio of an element based on its height 😭.
Continue reading “Responsive element width based on its height without JavaScript”

Remove all terms from a post

If you want to remove all taxonomy terms related to a post, wp_remove_object_terms() seems like the way to go. That function however expects the IDs of the terms to remove, there is no way to tell it to remove all terms at once without knowing the IDs beforehand.

Passing an empty $terms array or false or null to wp_set_object_terms() results in removing all terms associated to the post.

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 in Sublime Text on a Mac using Homebrew and Git. Continue reading “Fun with WordPress PHP Coding Standards in Sublime Text”

How to cancel Adobe Creative Cloud outside of the US

  1. Log in to your Adobe account.
  2. Click “Cancel Account”.
  3. Ah outside of the US you need to contact support to cancel your account WTF?
  4. Click through several steps to be able to view your contact options.
  5. Log in again.
  6. Chat currently closed, although it is within office hours.
  7. Let’s call support…
  8. Line is dead, no answer.
  9. One last support method is mentioned: “Ask our community”. Yeah right.
  10. Try again tomorrow.

More about: dark patterns.

And then:

Couldn't uninstall Creative Cloud for Desktop

…le sigh…

Save a Custom Meta Boxes plugin Taxonomy Select Field as a term for a post

The helpful Custom Meta Boxes plugin by Human Made provides a Taxonomy Select Field. Ever so sneakily, instead of adding the post to the selected term as one might expect, the plugin saves the taxonomy term id in a custom field. Let’s fix that. Continue reading “Save a Custom Meta Boxes plugin Taxonomy Select Field as a term for a post”

Add “virtual” page templates in WordPress

A super nifty feature was added to WordPress 4.4: the possibility to dynamically add page templates (#34995).

Previously a theme author would need to add actual custom page template files to their theme. The template would then show up in the Template dropdown of the Page Attributes meta box.

Theme and plugin authors can now add page templates to the Template dropdown by adding a 'name' => 'Title' entry to the list of templates via the theme_page_templates filter.

Add an entry to the page template dropdown

/**
 * Add an entry to the page templates dropdown.
 *
 * This function hooks into the 'theme_page_templates' filter which is
 * documented in wp-includes/class-wp-theme.php.
 *
 * @param array $page_templates  Array of page templates. Keys are filenames,
 *                               values are translated names.
 * @return array Array of page templates including our custom ones.
 */
function prefix_filter_page_templates( $page_templates ) {

	/*
	 * Add an entry to the $page_templates array.
	 * Prefixing the key might be a good idea as
	 * an entry with that key might already exist.
	 */
	$page_templates['prefix_example_template'] = __( 'Example Template', 'textdomain' );

	return $page_templates;
}
add_filter( 'theme_page_templates', 'prefix_filter_page_templates' );

You can find out which template is assigned to a page with the get_page_template_slug( $post ); function.

So what?

A plugin might want to add a page template which adds something to the post content. Perhaps a calendar page template, or a price list page template.

/**
 * Add something to the post content.
 *
 * If the 'prefix_example_template' is selected,
 * add stuff below the post content.
 *
 * @since 1.0.0
 * @param string $content Post content.
 * @return string
 */
function prefix_add_stuff_to_content( $content ) {

	if ( ! is_admin() && is_page() ) {

		$template_slug = get_page_template_slug( $post->ID );

		if ( 'prefix_example_template' === $template_slug ) {
			$content = 'Stuff I want to add goes here' . $content;
		}
	}
	
	return $content;
}
add_filter( 'the_content', 'prefix_add_stuff_to_content' );

Maybe a page with our “Example Template” will not need the text editor in the admin.

/**
 * Remove TinyMCE editor if the 'Example Template' is selected/
 */
function prefix_maybe_remove_editor_support() {

	if ( empty( $_GET['post'] ) ) {
		return;
	}

	$post = get_post( absint( $_GET['post'] ) );

	if ( ! $post ) {
		return;
	}

	$post_type = get_post_type( $_post );

	if ( 'page' !== $post_type ) {
		return;
	}

	if ( 'prefix_example' === get_page_template_slug( $post ) ) {
		remove_post_type_support( 'page', 'editor' );
	}
}
add_action( 'load-post.php', 'prefix_maybe_remove_editor_support' );

Or we might want to add a meta box to the page edit screen if our page template is selected.

/**
 * Add a meta box to the page edit screen if the `prefix_example_template`
 * page template is selected for the page in question.
 */
function prefix_maybe_add_my_cool_meta_box( $post_type, $post ) {
	
	if ( 'page' !== $post_type ) {
		return;
	}

	if ( 'prefix_example_template' !== get_page_template_slug( $post->ID ) ) {
		return;	
	}

	add_meta_box(
		'example-meta-box',
		esc_html__( 'My Cool Meta Box', 'textdomain' ),
		'callback',
		'page',
		'normal',
		'high'
	);
}
add_action( 'add_meta_boxes', 'prefix_maybe_add_my_cool_meta_box', 10, 2 );

Okay, the last two examples are not specifically related to the new “virtual” page templates. I hoped to illustrate some stuff you can do with them.

I’ve been happily using this in my projects (even if frowned upon) to dynamically add page templates for use as archive pages for custom post types. Neat for vanity URLs!

Plugin: Prevent automatically attaching unattached attachments when inserting into post

In an effort to keep things simple for the user (and to separately store the post content and its images), we decided to automatically create an image gallery from all images attached to a post. To add an image to a post’s image gallery, the user just needed to upload an image to that the post, without inserting it into the post content. Voilá, great for product pages and such.

As things go it turns out that this makes it impossible super inconvenient to also add images, say a sponsor logo, to the post content if you did not intend for that image to end up in the post’s gallery as well.

Curiously, when you insert an item into the post content via the media modal’s ‘Media Library’ tab: even when you did not upload that image via the current post edit screen, WordPress will mark that attachment as attached to that specific post if it was not already attached to another post. Now what?

Filters to the rescue

add_filter( 'wp_insert_post_parent', 'prefix_filter_insert_post_parent', 10, 2 );

function prefix_filter_insert_post_parent( $post_parent, $post_ID ) {

	// The 'send-attachment-to-editor' action is set when inserting attachments into the editor.
	if ( ! isset( $_REQUEST['action'] ) || 'send-attachment-to-editor' !== $_REQUEST['action'] ) {
		return $post_parent;
	}

	$post_before = get_post( $post_ID );

	// If the attachment already has a parent, return its ID.
	if ( 0 !== $post_before->post_parent ) {
		return $post_parent;
	}

	// Else, sadly, but convenient for us, the attachment needs to stay an orphan...
	return 0;
}

In the unlikely case you run into the same problem: the XQR-Z100A WordPress plugin takes care of that now.

Thanks for your help J.D.!