This create a single page view of a recipe in WP Recipe Maker.
- Add the code in this gist
- Create a page with the permalink
/recipe
- Flush permalinks
- You can now go to
/recipe/<id>
to view the embedded recipe. For example/recipe/1
.
<?php | |
// Your themes functions.php | |
add_action( 'init', function() { | |
add_rewrite_rule( | |
'recipe/([0-9]+)/?$', | |
'index.php?pagename=recipe&recipe_id=$matches[1]', | |
'top' ); | |
}); | |
add_filter( 'query_vars', function( $query_vars ){ | |
$query_vars[] = 'recipe_id'; | |
return $query_vars; | |
}); | |
add_filter('the_content', function($content) { | |
$recipe_id = (int)get_query_var('recipe_id'); | |
if(!$recipe_id) { | |
return $content; | |
} | |
// Check if we're inside the main loop in a single post page. | |
if ( is_page('recipe') && in_the_loop() && is_main_query() ) { | |
return $content . do_shortcode("[wprm-recipe id='{$recipe_id}'/]"); | |
} | |
return $content; | |
}); |
This create a single page view of a recipe in WP Recipe Maker.
/recipe
/recipe/<id>
to view the embedded recipe. For example /recipe/1
.