LoginSignup
9
8

More than 5 years have passed since last update.

手っ取り早くあなたのWordPressサイトをローカルPCで動かしたい

Last updated at Posted at 2015-02-04

はじめに

あなたが運営しているWordpressサイトを試しに自分のPC内のxampp上で動かしたいことってありますよね。

しかし、wordpressってご存知の通りドメインを変更するだけなのに、要所要所でフルパスが使われているため、いろんなテーブルをアップデートしたりと、ほんと疲れます。

そこで、とりあえずログインできて、最低限ページを確認できればいいやってかたに、試す価値のあるとてもお手軽な方法をお伝えします。
※この方法は決して万能ではございません

やり方

手順1

FTP等でコンテンツ一式ダウンロードして、ローカルのwebサーバに配置

手順2

mysqlのデータベースをエクスポート&ローカルにインポート

手順3

あなたのテーマのfunctions.phpに下記のソースを書く。
your.domain の部分を変更しましょう

wp-content/themes/your_theme/functions.php
<?php
if(preg_match('/localhost/', $_SERVER['HTTP_HOST'])):
  function replace_host_callback($html){
    $html = str_replace('your.domain', $_SERVER['HTTP_HOST'], $html);
    return $html;
  }
  ob_start("replace_host_callback");
endif;

HTMLを出力する時にドメインをローカルドメインに強引に変換してやるだけです。。

この方法は、下記のような方法でサブドメインで振り分けるような設定をしている方にとって便利です。

hosts
127.0.0.1       localhost
127.0.0.1       site1.localhost
127.0.0.1       site2.localhost
...
httpd.confなど
<VirtualHost *:80>
  DocumentRoot "C:/xampp/htdocs"
  ServerName localhost

  RewriteEngine on
  RewriteCond %{HTTP_HOST} ^[^.]+\.localhost$
  RewriteRule (.*) %{HTTP_HOST}$1  [C]
  RewriteRule ^([^.]+)\.localhost(.*) C:/xampp/htdocs/$1/$2  [L]
</VirtualHost>

おわりに

思いついた時、個人的にはヒットだったので、つい投稿しました。。

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8