Before I start, I want to tell you guys that I have no experience in anything WordPress related. I do have worked in PHP and Codeigniter before.
User Case
- The user enters a preferred feature in a form (Air conditioning, etc.).
- When the user submits the form, the feature will be send to a REST API.
- The results of the REST API ( a list of cars with AC ) will be shown on the page.
It should roughly look something like this.
What i have so far
An empty plugin that is shown in the WordPress admin panel.
Question(s)
- How do create the front-end for this plugin?
- How and where do I create the form action?
- How do I access the form action?
What I have/know so far
I know there are some action hooks that will place your content in the header and footer by creating something like:
<php add_action('wp_footer', 'mp_footer'); ?>
In your empty plugins php file place this:
In your plugin’s directory create a new javascript file – search.js and place this:
Now you can use shortcode
[searchbox]
in your wordpress page to get your searchbox.In php you can get same result with
<? echo do_shortcode('[searchbox]') ?>
Explanation:
[searchbox]
shortcode, it is processed and replaced with a form viaget_searchbox()
function.submit
we are sending anaction
:get_search
(defined inwp_localize_script
).wp_ajax_get_search
and processesget_search_results()
and returns an output.done(function(r){
r
is the response from server. Use that data to manipulate your html.action
is the essential part of REST api in wordpress. We need not have a url. Instead we define an action and for that action return a response.Once you understand this, modify the action and response according to your need.
Helpful articles: Daniel’s, wpmudev