In WP Ultimate Recipe templates are generated by and can be easily adjusted with the Template Editor. For advanced users who don’t want to use this editor it is also possible to code your own template and output that one instead.
To do so you can add the following code to your theme’s functions.php file:
function wpurp_custom_template( $content, $recipe )
{
// Change the content here.
return $content;
}
add_filter( 'wpurp_output_recipe', 'wpurp_custom_template', 10, 2 );
In this function you have access to the $recipe variable, which is a WPURP_Recipe object that can be used to output all the recipe parts.
For example, this code will output the recipe title and description:
function wpurp_custom_template( $content, $recipe )
{
ob_start();
?>
<div class="recipe">
<h2><?php echo $recipe->title(); ?></h2>
<?php echo $recipe->description(); ?>
</div>
<?php
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_filter( 'wpurp_output_recipe', 'wpurp_custom_template', 10, 2 );
An overview of all available functions can be found in the /core/helpers/models/recipe.php file.
Instead of accessing the $recipe variable directly it is also possible to use the same Template Blocks as in the Template Editor. All the different blocks can be found in the /core/addons/custom-templates/templates/ folder.
In this example we’ve added the Recipe Ingredients Block to our template:
function wpurp_custom_template( $content, $recipe )
{
ob_start();
?>
<div class="recipe">
<h2><?php echo $recipe->title(); ?></h2>
<?php echo $recipe->description(); ?>
<h3>Ingredients</h3>
<?php
$ingredient_list = new WPURP_Template_Recipe_Ingredients();
echo $ingredient_list->output( $recipe );
?>
</div>
<?php
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_filter( 'wpurp_output_recipe', 'wpurp_custom_template', 10, 2 );
Functionality like the print button and star ratings can also be added by code, but need to be surrounded with a .wpurp-container class to ensure the styling and functionality is correct.
We could add a print button and rating to our example like this:
function wpurp_custom_template( $content, $recipe )
{
ob_start();
?>
<div class="recipe">
<div class="wpurp-container">
<?php
$print_button = new WPURP_Template_Recipe_Print_Button();
echo $print_button->output( $recipe );
$star_rating = new WPURP_Template_Recipe_Stars();
echo $star_rating->output( $recipe );
?>
</div>
<h2><?php echo $recipe->title(); ?></h2>
<?php echo $recipe->description(); ?>
<h3>Ingredients</h3>
<?php
$ingredient_list = new WPURP_Template_Recipe_Ingredients();
echo $ingredient_list->output( $recipe );
?>
</div>
<?php
$output = ob_get_contents();
ob_end_clean();
return $output;
}
add_filter( 'wpurp_output_recipe', 'wpurp_custom_template', 10, 2 );