Display Ingredients Count

Show how many ingredients a WP Recipe Maker recipe needs.

Lauren Gray Avatar

Display Ingredients Count adds a block that shows how many ingredients a WP Recipe Maker recipe needs, so readers can size up the effort at a glance. You can leave out optional ingredients or ones you pick.

Before you start

  • WordPress 5.8 or later
  • PHP 5.6 or later
  • WP Recipe Maker, installed and active. Without it, the block shows nothing.

Install

  1. In your WordPress dashboard, go to Plugins → Add New Plugin.
  2. Click Upload Plugin.
  3. Choose the plugin zip file.
  4. Click Install Now.
  5. Click Activate.

How to use

Add the count to a recipe post

  1. Open a post that has a WP Recipe Maker recipe in it.
  2. Click the block inserter (+) and search for Display Ingredients Count. It’s in the Text category.
  3. Type a label above the number if you want one, like “Ingredients”. The label field shows Write label… until you type. Leave it empty to show just the number.
  4. Use the options in the block sidebar to leave out ingredients (see Block options below).
  5. Save or update the post.

The editor shows a live count. If the post has no recipe, you’ll see No recipes found. in the editor, and the block won’t show on the front end.

Add the count to a template

You can also place the block in a template in the Site Editor or inside a Query Loop. It counts the ingredients of the recipe in whichever post is being shown. The Ignore ingredients search works here too, because it searches ingredients from every recipe on your site.

Block options

These are in the General panel of the block sidebar.

  • Ignore ingredients: start typing an ingredient name and pick it from the suggestions. Picked ingredients aren’t counted. The name has to match the ingredient name in the recipe exactly. Default: none.
  • Ignore By Optional: automatically leaves out any ingredient or ingredient group whose name contains the word “optional” (any capitalization). Default: off.

The block also has the usual style controls: colors, border, typography, spacing, and wide or full alignment.

Developer reference

For developers: hooks and functions you can use from your theme or a code snippet.

dic_get_ingredients_count( $recipe_id, $ignored = array(), $ignore_optional = false )

Function. Returns the number of ingredients in a WP Recipe Maker recipe, or false if WP Recipe Maker isn’t active or the recipe doesn’t exist.

  • $ignored: ingredient names to leave out of the count.
  • $ignore_optional: when true, skips ingredients and ingredient groups with “optional” in the name.

dic_get_recipe_id_from_post( $post_id )

Function. Returns the ID of the first WP Recipe Maker recipe in a post, or false.

$recipe_id = dic_get_recipe_id_from_post( get_the_ID() );

if ( $recipe_id ) {
	$count = dic_get_ingredients_count( $recipe_id, array( 'salt', 'water' ), true );
	echo esc_html( sprintf( '%d ingredients', $count ) );
}

FAQs

My post has two recipes. Which one gets counted?

Only the first recipe found in the post.

Are ingredient group headings counted?

No. Only the ingredients themselves are counted, not the group names.

Does “optional” in the notes count?

Ignore By Optional only looks at the ingredient name and the group name. If “optional” is only in the ingredient’s notes, the ingredient is still counted.