0

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:

    wp-content
              --themes
                 -- myTheme

              --plugins
                 --myPlugin
                      --views
                         --page-results.php

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

   /**
 * RETURN THE SEARCH FORM
 * @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"
                   placeholder="City">
        </div>
        <div class="mb-2">
            <input type="text" class="form-control text-center" id="searchByName"
                   placeholder="Name">
        </div>
        <div class="mb-2">
            <select name="countryChoice" class="text-center">
                <option value="EN">England</option>
            </select>
        </div>

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

</div>';

    echo $html;
}

add_shortcode( 'wdv_search_form', 'add_search_form' );

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

New contributor
Fewture Webdesign is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Answer 1

0

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.

1
  • 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! Jun 24 at 22:51

Your Answer

Fewture Webdesign is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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