Skip to content

Instantly share code, notes, and snippets.

@khromov
Created April 7, 2020 19:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khromov/d7c5b184c6df0a4b2dac80182f52b28e to your computer and use it in GitHub Desktop.
Save khromov/d7c5b184c6df0a4b2dac80182f52b28e to your computer and use it in GitHub Desktop.
WP Recipe Maker
<?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.

  • 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment