Food2fork is one of a number of recipe websites that wallpapers the Internet, and it's the first of many recipe websites that'll be the target of our attention in coming months. The site's API has two main API functions: the first will search indexed recipes; the second will extract limited details of an actual recipe. In this article we'll use the second API function to build a basic recipe on your website.
Note: The F2F API is generally unreliable (possibly pending removal) and shouldn't be used. There are better alternatives.
Food2fork is used primarily to direct traffic to other recipe websites - so it's not overly functional. To that end, the recipe's directions (method) won't be returned by the API, and the method isn't visible on the F2F website. If you're after a full recipe solution we'll be providing far better options in the future.
See also: Example PHP Functions to Return Recipe Methods From Cooking Websites (link).
We're returning the limited F2F data in a very basic manner. If you plan on using this code it's expected that you'll style the returned HTML with appropriate markup .
The Result
To include the basic recipe for a Chocolate slice we'll use the shortcode of [food2fork r="http://food2fork.com/view/Caramel_Slice/cef370"]
. The result:
[food2fork r="http://food2fork.com/view/Caramel_Slice/cef370"]
We could have optionally used just the recipe ID ([food2fork r="cef370"]
).
WordPress Shortcode
Copy and paste the WordPress function into your theme's functions.php
file or, if you sensibly have one installed, your custom functions plugin.
If you require shortcode to work in a sidebar widget, you'll have to enable the functionality with a filter. If you're using our custom functions plugin, you'll have that feature enabled by default.
Shortcode Attributes
r
r
is the recipe ID. This is either the full URL to the recipe page or the last string of digits after the last backslash.apikey
apikey
is required to make queries and can be obtained from the website .attributionlink
attributionlink="'1"
in your shortcode. It is false by default.Considerations
The recipe isn't formatted so it's expected that you'll style it up yourself... such as rendering the ingredients in a list. It's also not returned in its entirely - likely to protect the origin website's copyright. Shortcodes we have planned for the future lets you return far more feature rich results.
The function will only query the API the first time it requests a particular recipe. Recipe text is then cached locally in your WordPress database and called upon only when required. Consider caching the image locally by either copying it to another directory or importing it into your WordPress library.
The food2fork API is free for personal use for up to 500 queries per day. There's a fee for commercial purposes but given the limited results it's not a particularly wise investment.