Jump to content

Wordpress plugin with Redirect to URL


1eye

Recommended Posts

Hi all,

I am trying to create a plugin that shows a simple form with a few input fields i.e. departure date, return date, number of adults etc. and based on these inputs load an external URL e.g.   www.booking/Specific_Id/20-8-108/23-8-2018/2 .

I have created the form and I have also created some settings on the admin side which contains the specific_id input, the problem is I  don't know how to get the input values of the form and then load the specific URL.

I tried to post the form to a PHP file and then use the header function to load the URL but it's not working for me.

I would appreciate any help/advice you could give.

Link to comment
Share on other sites

Using a form to gather the inputs is the expected way.  Your php script needs to validate the form's inputs to be sure the user has provided proper values.  Then you can use the header call to follow thru on whatever url you have built from this.

Good idea - show us the code that gathers the form inputs and then what you do to build the url and call it.  That would be a great help to anyone here that wants to help you.  Otherwise I don't know what you are expecting from the forum.  Error messages are a good thing to provide as well.

Link to comment
Share on other sites

You're going top have to describe the problem better.

  1. Are you saying you want the user to fill out the form then immediately be redirected to the new page?
    1. If so, there are a couple other things to consider, but ginerjm is correct in his statement about gathering the values and using a header redirect.
  2. Are you saying you've got the form saving the information properly to the database and need to know how to get the data from the database?
    1. If so, it's going to depend on how and where you saved the data.

Either way, it's probably going to involve using an action hook at some point, so where is this code happening? Are you creating a custom theme, a child theme, or a plugin?

Basically, what does

3 hours ago, 1eye said:

I have also created some settings on the admin side which contains the specific_id input, the problem is I  don't know how to get the input values of the form and then load the specific URL.

mean exactly?

Link to comment
Share on other sites

My bad, sorry for not being specific enough.

I want the user to fill out the form then immediately be redirected to the new page. The user doesn't enter the specific id, this is a value saved to the wp_options table by the admin.

The path I was providing for the php script was incorrect so when I submitted the form it could not find the php script used to validate the forms inputs and call the header function! Stupid of me I know.

After putting in the correct path the next problem I face is getting access to the specific id.

For the specific id,  I created a menu page for the plugin with a settings section. Here the admin can enter an id which is saved to the wp_options table.

When I try to access the id in the aforementioned php script I'm getting a; call to undefined function get_option() error.

I know the specific id exists because I used database-browser to look it up. I also know the get_option() function exists in the options.php file in the wp-includes folder.

I hope that gives you a clearer understanding of what I am trying to achieve and maybe an idea of what I am doing wrong.

 

Link to comment
Share on other sites

After reading your reply i changed my code slightly but it still doesn't work.

I'm trying to use admin_post hook but i must be doing something wrong. I've added some snippets below.

hookandreg.PNG.580dc08eb68c9a2db985dc43496433f4.PNG

The above image shows the hook and the inclusion of the file that contains the prefix_admin_add_foobar function.

function.PNG.eddf3eb668ac44ed4d8216dd2d4055c4.PNG

The following snippet shows the function but its not getting called.

form.PNG.84dbf650e12f0322a8ef96a14da3b50c.PNG

This is the form.

When i submit the form I'm getting a 404 page not found error.

error.PNG.c01037102a5b280b36acf341738040ab.PNG

Thanks.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.