0

First, I know basically zero php, I usually do jsp but I have to modify a footer for a wordpress site we have.

The site currently is hosted at www.mysite.com/wordpress and has the following line:

<?php readfile('http://www.mysite.com/css-imports.jsp'); ?>

and because I am doing this on development I keep having to change it to:

<?php readfile('http://local.mysite.com/css-imports.jsp'); ?>

When I do that the wordpress url is local.mysite.com/wordpress.

How can I from php insert local.mysite.com or www.mysite.com based on the domain that is serving the page?

Thanks.

casolorz
  • 8,486
  • 19
  • 93
  • 200

4 Answers4

1

You need a base URL variable (of which I'm certain Wordpress will have one, most frameworks do). Have a look at this answer here.

Hard coding a domain in like that when it relates to your server is not a good idea, because as soon as you move your code - breaky breaky. So to use their example, you'd do something like this:

<?php readfile(get_bloginfo('wpurl') . '/css-imports.jsp'); ?>

Might need a bit of playing around with, have a look at the Wordpress Dev Docs and find something that suits you if this doesn't:

Community
  • 1
  • 1
scrowler
  • 24,273
  • 9
  • 60
  • 92
  • Thanks, all those functions were returning the correct server but including /wordpress in them. I ended up fixing it with `$_SERVER[HTTP_HOST]` – casolorz Feb 04 '14 at 20:25
0

You could set up a simple config file that will differ between the local and production servers. All you'll need to do is make sure the local copy doesn't get included when new code is pushed to the server.

Local copy of serverconfig.php:

<?php
define('SITE_HOSTNAME','http://local.mysite.com/');
?>

Off-site copy of serverconfig.php:

<?php
define('SITE_HOSTNAME','http://www.mysite.com/');
?>

Everything else will just need to include the config file and reference the constant(s) defined in it.

<?php
require_once 'serverconfig.php';
... code ...
readfile(SITE_HOSTNAME. 'css-imports.jsp'); 
?>

Note: if you're just reading static files that are located on the same webserver, you should just reference the local files, readfile('/css-imports.jsp');.

404 Not Found
  • 3,635
  • 2
  • 28
  • 34
  • You **should** reference local files, but Wordpress doesn't - they seem to insist on absolute URLs throughout the entire app – scrowler Feb 04 '14 at 20:05
0

I usually just end up modifying my hosts file. I work with too many disparate customer systems locally (including WordPress and Drupal) and it seems like every system I've seen has templates/themes/plugins that don't reference the framework's base url variables, or uses hard-coded absolute paths, or uses relative paths that don't jive well with my server, or just something.

When you're done developing and need to load it into production, just comment out the hosts entry.

/etc/hosts

# map mysite.com to local host for dev reasons.
www.mysite.com 127.0.0.1
Matthew Poer
  • 1,682
  • 10
  • 17
0

I think this will do the try for me

  $_SERVER[HTTP_HOST]
casolorz
  • 8,486
  • 19
  • 93
  • 200