LoginSignup
1
1

More than 1 year has passed since last update.

WordpressにURLコピー機能を設けてみた

Last updated at Posted at 2022-11-28

(手順を間違えなければ)誰でも簡単に WordpressのURLを
増殖できるものを作ってみました。

特徴

  • 年ごとにURLが変わるサイトに有効(abc2013.example.com とか)
  • 一応 Wordpressの管理画面から操作できる

テーマに入れてみる

functions.php
$host=$_SERVER['HTTP_HOST'];

// リダイレクトがphpなのはXServer用に .htaccessを触れたくないため
if(preg_match('/example.com/i', $host)) {
	$redirectphp="<?php\n\$url='https://example%04d.example.com'  . \$_SERVER['REQUEST_URI'];\nheader('HTTP/1.1 301 Moved Permanently');\nheader('Location: ' . \$url);\n";
	$basedir="/home/username/example.com/public_html/example%04d.example.com";
}

/* Wordpressのコピー&転送設定 */
function wordpresscopy($url) {
	global $basedir, $redirectphp;

	$urlyear=$url;
	$urlyear=preg_replace('/example/i', '', $urlyear);
	$urlyear=preg_replace('/\..*/i', '', $urlyear);

	$domain=$url;
	$domain=preg_replace('/example\d\d\d\d\./i', '', $domain);

	// 誤って消さないように 0の場合は実行しない
	if($url != "" || $urlyear + 0 == 0) {
		return;
	}

	$zerodir=sprintf($basedir, 0);
	echo "<br>";
	for($i=1900; $i<$urlyear; $i++) {
		$dir=sprintf($basedir, $i);
		if(file_exists($dir)) {
			exec("cd $dir; rm -rf .htaccess; rm -rf *");
			file_put_contents("$dir/index.php", sprintf($redirectphp, $urlyear));
			echo "example$i.$domain ($dir)に転送URLを設定しました<br>";
		}
	}

	$dir=sprintf($exampledir, $i);
	if(file_exists($dir)) {
		exec("cd $dir; rm -rf .htaccess; rm -rf *");
		file_put_contents("$dir/index.php", sprintf($redirectphp, $urlyear));
		echo "example.$domain ($dir)に転送URLを設定しました<br>";
	}

	$dir=sprintf($basedir, $urlyear);
	$base=sprintf($basedir, 0);

	if(file_exists($dir)) {
		exec("cd $dir; rm -rf .htaccess; rm -rf *");
		exec("cd $dir; cp -pR $base/* $dir");
		exec("cd $dir; cp -pR $base/.htaccess $dir");
		echo "example$urlyear.$domain ($dir)にWordpressを設置しました<br>";
	}
}

function create_myurl_settings_page() {
	$options = get_option( 'myurl_options' );
	$option = isset( $options['url'] ) ? esc_attr( $options['url'] ) : '';
	wordpresscopy($option);

 ?>

  <div class="wrap">

    <h1>URL設定</h1>

    <?php /* 設定用のフォームを作成します。 */ ?>
    <form method="post" action="options.php">

      <?php /* 設定値のグループを指定し、設定値を取得します。 */ ?>
      <?php settings_fields( 'myurl_options_group' ); ?>
      <?php do_settings_sections( 'myurl_options_group' ); ?>
      <?php $options = get_option( 'myurl_options' ); ?>

      <?php /* 設定値を表示します。 */ ?>
      <table class="form-table">
        <tr valign="top">
          <th scope="row">URL</th>
          <td>
            <?php $option = isset( $options['url'] ) ? esc_attr( $options['url'] ) : '' ?>
            <input type="text" name="myurl_options[url]" value="<?php echo $option; ?>">
          </td>
        </tr>
      </table>

      <?php submit_button(); ?>

    </form>
  </div>
<?php }


function sanitize_myurl_options( $options ) {

  $new_options = array();

  if ( isset( $options['url'] ) ) {
    $new_options['url'] = sanitize_text_field( $options['url'] );
  }


  return $new_options;
}

function register_myurl_settings(){
  register_setting(
    'myurl_options_group',
    'myurl_options',
    'sanitize_myurl_options'
  );
}

function add_myurl_menu() {
  add_menu_page(
    'URL設定', // メニューページのタイトル文字列
    'URL設定', // メニュー文字列
    'manage_options', // メニューの表示権限
    'myurl-settings', // メニューのスラグ(URLのパス)
    'create_myurl_settings_page', // メニューページの表示用の関数
	'',
	80
  );
}

if ( is_admin() ) {
	add_action( 'admin_menu', 'add_myurl_menu' );
	add_action( 'admin_init', 'register_myurl_settings' );
}

使い方

  • まずベースとして、0000年度のホスト (example0000.example.com) にWordpressをインストールする
  • URL設定をして example2024.example.com にする
  • (それ以前のURLに転送設定がされ、example2024.example.comにはWordpressがコピーされる)
  • 設定→一般のURL2か所を変更して、同じパスワードで再ログインする

0000に戻すには

  • 設定→一般のURL2か所を0000に変更して、同じパスワードで再ログインする

ついでに

これもあるとよいかもしれません

functions.php
function urlshort($buffer) {
	$search = array(
		'/href="https?:\/\/' . $_SERVER['HTTP_HOST'] . '/i',
		'/href=\'https?:\/\/' . $_SERVER['HTTP_HOST'] . '/i',
		'/src(.*?)="https?:\/\/' . $_SERVER['HTTP_HOST'] . '/i',
		'/src(.*?)=\'https?:\/\/' . $_SERVER['HTTP_HOST'] . '/i',
	);
	$replace = array(
		'href="',
		'href=\'',
		'src\1="',
		'src\1=\'',
	);
	$buffer = preg_replace($search, $replace, $buffer);
	return $buffer;
}

ob_start("urlshort");

さいごに厳重注意

恐怖のコマンド rm -rf * が存在します。
きちんと環境で正しく動作するか、十分なテストをお願いします。

1
1
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
1
1