Reputation: 181
So I have this code here in my main file.
require_once(plugin_dir_path(__FILE__) . 'load_functions.php');
add_shortcode( 'ccss-show-recipes', 'ccss_show_tag_recipes' );
function ccss_show_tag_recipes() {
global $post;
global $wp;
$html = '';
$url = home_url( $wp->request );
$path = parse_url($url, PHP_URL_PATH);
$pathFragments = explode('/', $path);
$currentPage = end($pathFragments);
// $html.= '<p>'.$currentPage.'</p>';
if ($currentPage == 'recipes') {
ccss_load_all_recipes();
} elseif ( in_array( $currentPage ,["cleanse","combine","commence","commit","complete","consolidate"] ) ) {
//load_phases();
} elseif ( in_array( $currentPage ,["basic-marinades","basic-stock-recipes"] ) ) {
// load_recipe_type();
}
return $html;
// Restore original post data.
wp_reset_postdata();
}
and I have function here in load_functions.php
function ccss_load_all_recipes() {
//code here that wont return
$html .= '<p>test</p>'; //--> It wont display this
echo 'test'; //---> This will be displayed
}
The problem when I call ccss_load_all_recipes() it won't return anything, any ideas on what error I made? But when I try an echo statement it returns it
Thanks, Carl
Upvotes: 2
Views: 27
Reputation: 5766
your function css_load_all_recipes()
does not know the variable $html
. In order to achieve that you should pass the $html variable into the function and return it again at the end.
// in your main file
$html = ccss_load_all_recipes($html);
// in load_functions.php
function ccss_load_all_recipes($html = '') {
$html .= '<p>test</p>';
return $html;
}
Edit: other possibilities are: declaring $html
as a global variable, or passing $html
as reference so you don't have to return the altered html back to the main file. But I would advise against both of these options, unless you run into the exact same problem many times within your application.
Upvotes: 2