Naresh
Naresh

Reputation: 2781

wordpress plugin redirects instead of performing search

I am creating a plugin; the plugin has item listings, and I want to search for items in the listings.

Here is my code:

<form method="get" action="<?php echo admin_url('admin.php?page=listing'); ?>" id="search" >
<input type="text" name="q" placeholder='Search'/>
</form>

When I submit the form, the URL redirects to:

http://localhost/wordpress/wp-admin/admin.php?q=shock

And no search occurs! How do I fix this?

Upvotes: 0

Views: 54

Answers (1)

scriptin
scriptin

Reputation: 3120

Your page parameter gets erased.

You are using a GET method and q parameter overrides all other parameters in a query string, which is page=listing.

Two possible solutions:

  1. use POST by setting method="post" attribute, in which case q will be passed as a POST parameter to a page with URL .../admin.php?page=listing.

  2. include the page parameter inside your form (<input type="hidden" name="page" value="listing"/>) and replace admin_url('admin.php?page=listing'); with admin_url('admin.php');, in which case you'll end up on .../admin.php?page=listing&q=shock, because your form will contain both q and page parameters.

The choice really depends on how your plugin obtains the search string: GET or POST parameter.

Upvotes: 1

Related Questions