I am in desperate need of help, regarding the following issue:

I need to establish an API Connection to Salesforce, so that a user can search and retrieve certain data out of Salesforce. I thought, that a plugin would be the best choice, so that the functionality is not dependent on a certain theme.

Here comes my issue: I want the search form to be added via short-code, so that it can be reused. So far so good. Then I gave the form an action attribute to call another php file, which retrieves the entered data to search for, and list the search results.

Now when I call that second file, I get the error Call to undefined function get_header(). But I need the header of the selected theme to be displayed on that page, so that the whole website has the same look and feel.

It seems, as if I am going horribly wrong with my approach.

My file structure looks as follows:

                 -- myTheme


And this is how I add the shortcode for the form to be displayed:

 * @return string
function add_search_form() {

    $html = '
    <div class="col-sm-6 col-md-offset-3 d-flex justify-content-center text-center form-column">

    <form class="wdv_search_form w-75 mt-3" method="get" action="wp-content/plugins/myPlugin/views/page-results.php">
        <div class="mb-2">
            <input type="text" class="form-control text-center" id="searchByLocation"
        <div class="mb-2">
            <input type="text" class="form-control text-center" id="searchByName"
        <div class="mb-2">
            <select name="countryChoice" class="text-center">
                <option value="EN">England</option>

        <button type="submit" class="btn btn-transparent text-light mt-3">GO</button>


    echo $html;

add_shortcode( 'wdv_search_form', 'add_search_form' );

Is my approach right and what would be best-practice for my use-case?

1 Answer 1


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.

  • Sorry for the late response. You helped me a lot, thank you for that! I went with a Class apporach which renders my templates for me now, while keeping my shortcode-apporach!
    – Fewture
    Jun 24, 2022 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.