RBA Cash Rate: 4.35% · 1AUD = 0.67 USD · Inflation: 4.1%  
Leading Digital Marketing Experts | 1300 235 433 | Aggregation Enquires Welcome | Book Appointment
Example Interest Rates: Home Loan Variable: 5.38% (6.14%*) • Home Loan Fixed: 5.44% (6.26%*) • Fixed: 5.44% (6.26%*) • Variable: 5.38% (6.14%*) • Investment IO: 5.69% (6.52%*) • Investment PI: 5.49% (5.98%*)

Add Food2fork Recipes to WordPress with Shortcode

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.

1
<?php 
2
/*
3
 Add Food2fork Recipes to WordPress with Shortcode
4
 http://www.beliefmedia.com/code/wp-snippets/food2fork-wordpress-shortcode
5
 Register for API at http://food2fork.com/about/api
6
*/
7
 
8
function beliefmedia_foodfork_recipe($atts) {
9
 
10
  extract(shortcode_atts(array(
11
    'r' => 'e6389e', /* Recipe URL or ID */
12
    'apikey' => 'xxxxxxxxxxxxxxxxxxxxx',
13
    'w' => '',
14
    'attributionlink' => 0
15
  ), $atts));
16
 
17
 /* Post ID to save recipe */
18
 global $post;
19
 $id = $post->ID;
20
 
21
  /* If a URL get recipe ID */
22
  if (strripos($r,'http') !== false) {
23
   $chunks = explode("/", $r);
24
   $r = end($chunks);
25
  }
26
 
27
  /* Recipe Key */
28
  $f2fhash = 'rf2fg_' . md5($r);
29
 
30
  if ( (get_post_meta($id, $f2fhash, true )) != '') {
31
   $temp = get_post_meta($id, $f2fhash, true);
32
 
33
   } else {
34
 
35
    /* Retrieve only necessary JSON data */
36
    $feed = file_get_contents('http://food2fork.com/api/get?key=' . $apikey . '&rId=' . $r);
37
    $json_data = json_decode($feed, true);
38
 
39
    $temp['publisher'] = $json_data['recipe']['publisher'];
40
    $temp['url'] = $json_data['recipe']['f2f_url'];
41
    $ingredients = $json_data['recipe']['ingredients'];
42
 
43
     foreach ($ingredients as $ingredient) {
44
      $ingrd .= $ingredient . '';
45
     }
46
     $ing = rtrim($ingrd, '');
47
 
48
     $temp['ingredients'] = $ing;
49
 
50
     $ingints = rtrim($ingrd, '');
51
     $temp['ingredients'] = $ingints;
52
 
53
    $temp['source'] = $json_data['recipe']['source_url'];
54
    $temp['rid'] = $json_data['recipe']['recipe_id'];
55
    $temp['imageurl'] = $json_data['recipe']['image_url'];
56
    $temp['socialrank'] = $json_data['recipe']['social_rank'];
57
    $temp['publisherurl'] = $json_data['recipe']['publisher_url'];
58
    $temp['title'] = $json_data['recipe']['title'];
59
 
60
    /* WP will save serialised array to DB */
61
    if (!empty($temp['rid'])) add_post_meta($id, $f2fhash, $temp);
62
   }
63
 
64
   /* Return result */
65
   $return .= '<strong>' . $temp['title'] . '</strong>';
66
   $return .= '
67
<p align="center"><img src="' . $temp['imageurl'] . '">';
68
   $return .= '<strong>Ingredients</strong>';
69
   $return .= '' . $temp['ingredients'] . '';
70
   $return .= '<strong>Method:</strong> View the full recipe: <a href="' . $temp['source'] . '" target="_blank" rel="noopener noreferrer">' . $temp['publisher'] . '</a>';
71
   $link = ($attributionlink) ? '<a href="http://www.food2fork.com/" target="_blank" rel="noopener noreferrer">Food2Fork</a>' : 'Food2Fork';
72
   $return .= '<small>Recipes powered by ' . $link . '</small>';
73
 
74
 return $return;
75
 
76
}
77
add_shortcode('food2fork', 'beliefmedia_foodfork_recipe');

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

The 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

The apikey is required to make queries and can be obtained from the website .

attributionlink

Attribution is provided in the code by default. However, to create a link back to the food2fork website use 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.

■ ■ ■

 
Download our complimentary 650-page guide on marketing for mortgage brokers. We'll show you exactly how we generate billions in volume for our clients.
Finance Guide, Cropped Top and Bottom
  Timezone: 1 · [ CHANGE ]

Like this article?

Share on Facebook
Share on Twitter
Share on Linkdin
Share on Pinterest

Leave a comment