You shouldn’t link directly to plugin/theme PHP files to handle requests for this exact reason – the WordPress environment won’t be loaded, hence your undefined function error.
Since you’ve implemented a shortcode-approach to integrate with the theme, you could just point the form to itself (i.e. remove the action
attribute) and then dynamically handle the output based on if the form has been submitted, for example:
function add_search_form() {
if ( ! empty( $_GET['countryChoice'] ) ) {
// Return search results HTML
} else {
// Return form HTML
}
}
If you want the theme to have more control over the entire template for search results, you could override the template that WordPress will load like so:
add_filter( 'template_include', function ( $template ) {
if ( ! empty( $_GET['countryChoice'] ) ) {
$template = locate_template( 'my-plugin-search-results.php' );
}
return $template;
});
This would load the theme template file my-plugin-search-results.php
if there is a non-empty countryChoice
URL parameter – you might want to add more conditions to ensure the template isn’t overloaded for other WP requests.
These are just a couple of ideas – if you update your question with more context, we might be able to offer more specific help/suggestions.