3

Beginner Wordpress developer here... I am currently developing a Wordpress site for a device of mine. My idea is to be able to connect to the device which acts as a web server. I should be able to access the site from the local device hosting the site (Raspberry Pi from here on referenced to as the server) as well as from any machine/device connected on the same network as the server (mobile phone/laptop etc from here on referenced to as the client).

Connecting directly on the server to the site works fine connecting with http://localhost/mysite. All links and actions work as expected.

Connecting to the site from a client works partially. I can connect and browse wordpress links like pages etc but the below process does not work:

The Process

My plugin creates shortcodes which I can add to any page. all functions of the shortcodes basically calls a php file with the following code:

http://localhost/mysite/wp-content/plugins/my-plugin/api/mydatasourcephpfile.php

and then creates an html form on the page using the values it got from the above request. The form contains a submit button and when clicked, the form posts it values to:

http://localhost/mysite/wp-content/plugins/my-plugin/api/mypostphpfile.php

The mypostphpfile then takes the form values posted to it, writes it to a database and returns to the original page adding a url parameter of "?success=true". This is done using the following code in the mypostphpfile.php file right at the end after all data has been written:

$success="true";
header('Location: http://localhost/mysite/myoriginalformpage/?success=', $success);

I know exactly what the problem is. The problem is the localhost in my URL's which I have so to speak "hardcoded" which I post to/return to. When performing the post on the server and it posts to the localhost php file which is available on the localhost (in the context of the server). When posting from a client, the post does not work because it is posting to a localhost php file which the client browser is looking for on the client computer and not the server.

It seems to be a URL issue. I'm not very familiar with .htaccess files etc and I don't even know if it will be the solution to my problem. I need to be able to post to the php file where it's URL is relative to the server no matter from which device I am browsing the site. I am very new to Wordpress development and searches on the internet leads to not much which makes kind of sense because I don't even know how to describe the problem in a google search bar (as you can see in the question description :). I would like to prevent hard-coding ip addresses as these can change as well as hostnames as I have read these don't always work (on a raspberry pi at least).

So in a nutshell, is there any way that I can pick up which URL wordpress is running from to enable all clients to finish the process. The URL to the php files should be relative to the server.

Any help on this or pointing me in the right direction will be highly appreciated.

3

I'm not positive I understand the question, but if you're just trying to get the site URL, you can use the get_site_url() WP function. So, like this:

$siteURL = get_site_url();
header("=Location: $siteURL" . "/myoriginalformpage/?success=", $success);

And you can change the parameters of get_site_url() to specify the path and to display 'https' or 'http'.

But if you just need the current site's url, use get_site_url();

  • This is something to look into. I will test this and revert back. – Joachim Prinsloo Dec 21 '17 at 18:15
  • get_site_url() does not work. It still redirects to localhost when accessing the website on my pi from my laptop/device. even when I access it using the IP address to access the site, get_site_URL still returns localhost/MySite. :-( – Joachim Prinsloo Dec 23 '17 at 14:15
  • What do you have set as the URL in the WordPress settings? – Coltvant Dec 28 '17 at 19:06
  • I had localhost. I recently changed that to the static ip of the pi after setting it up as a hotspot. After doing this I may give get_site_url another go. Im having some trouble with the external php files when deploying it to the pi. I have not yet had a chance to debug this though. I just finishe setting up the access point after weeks of struggling. – Joachim Prinsloo Dec 28 '17 at 19:11
  • Yeah cause get_site_url() is going to pull the domain from the current site's URL settings...so if it's set to localhost that's what it will return. – Coltvant Dec 28 '17 at 19:36

Your Answer

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.