2025年2月22日 (土曜日)↓における週間月間人気記事トップ3は?↓

今日は2025年2月22日です。
目次に行く・戻る
2025年2月22日 (土曜日)本日の人気記事トップ10
メルカリ招待コード:

当ブログにも、ページの欄・所にコンタクトフォームを設けていて、
emailでメール送信されるようにしています。
コンタクトフォームは、
俗に言う「お問い合わせ先」です。
今はSNSで匿名的なブログなどでも連絡手段があり得ることもありますが、
もし、何かあった場合にはやはり「お問い合わせ先」として、
コンタクトフォームがあった方が良いと思われます。
そのコンタクトフォームですが、
+これまでによく読まれている記事一覧クリックでOPEN+

151,178件の PV

106,946件の PV

75,763件の PV

67,241件の PV

47,137件の PV

40,888件の PV

38,227件の PV

37,260件の PV

36,788件の PV

36,230件の PV
プラグインなしでコンタクトフォームを作成するには、
Stinger6の場合ですと以下のコードをphpファイルとして新たに作成して、
テーマディレクトリ内にアップロードします。
(contact-form.php)必ずUTF-8形式です。
<?php
/*
Template Name: お問い合わせフォーム
*/
?>
<?php
if(isset($_POST['submitted'])) {
//項目チェック
if(trim($_POST['checking']) !== '') {
$captchaError = true;
} else {
//名前の入力なし
if(trim($_POST['contactName']) === '') {
$nameError = '名前が入力されていません';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}
//メールアドレスの間違い
if(trim($_POST['email']) === '') {
$emailError = 'メールアドレスが入力されていません';
$hasError = true;
} else if (!preg_match('|^[0-9a-z_./?-]+@([0-9a-z-]+.)+[0-9a-z-]+$|', trim($_POST['email']))) {
$emailError = 'メールアドレスが正しくありません';
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//お問い合わせ内容の入力なし
if(trim($_POST['comments']) === '') {
$commentError = 'お問い合わせ内容が入力されていません';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}
//エラーなしの場合、メール送信
if(!isset($hasError)) {
mb_language("japanese");
mb_internal_encoding("UTF-8");
$emailTo = get_option('admin_email');
$subject = 'お問い合わせ';
$body = "お名前: $name nnメールアドレス: $email nnお問い合わせ内容: $comments";
$title = get_bloginfo('name');
$from = mb_encode_mimeheader(mb_convert_encoding("$titleのお問い合わせ","UTF-8"));
$headers = 'From: '.$from.' <'.$emailTo.'>';
mb_send_mail($emailTo, $subject, $body, $headers);
//自動返信用
$subject = 'お問い合わせ受付のお知らせ';
$from = mb_encode_mimeheader(mb_convert_encoding("$title","UTF-8"));
$headers = 'From: '.$from.' <'.$emailTo.'>';
$body = "$name 様 nnお問い合わせありがとうございます。n改めて担当者よりご連絡をさせていただきます。nnお名前: $name nnメールアドレス: $email nnお問い合わせ内容: $comments";
mb_send_mail($email, $subject, $body, $headers);
$emailSent = true;
}
}
} ?>
<?php get_header(); ?>
<main>
<?php if(isset($emailSent) && $emailSent == true) { ?>
<h1 class="entry-title archive-title"><?=$name;?>様、お問い合わせありがとうございます。</h1>
メールを確認次第、返事をさせて頂きます。
<?php } else { ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h1 class="entry-title archive-title"><?php the_title(); ?></h1>
<?php the_content(); ?>
<form action="<?php the_permalink(); ?>" id="contactForm" method="post">
<ol class="forms">
<li><label for="contactName">お名前</label>
<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
</li>
<li><label for="email">メールアドレス</label>
<input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="requiredField email" />
<?php if($emailError != '') { ?>
<span class="error"><?=$emailError;?></span>
<?php } ?>
</li>
<li class="textarea"><label for="commentsText">お問い合わせ内容</label>
<textarea name="comments" id="commentsText" rows="20" cols="30" class="requiredField"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
<?php if($commentError != '') { ?>
<span class="error"><?=$commentError;?></span>
<?php } ?>
</li>
<li class="buttons"><input type="hidden" name="submitted" id="submitted" value="true" /><button type="submit">送信する</button></li>
</ol></form>
<?php endwhile; ?>
<?php endif; ?>
<?php } ?>
</main>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
そして、
固定ページの作成でテンプレート属性をお問い合わせフォームにします。
すると…。
なぜかエラー表示なってしまいました。^^;)
エラー表示内容を見ると、
なにやら1行目が云々とありました。
(すみませんスクリーンショット撮り忘れました。)
なので、
1行目にコメントアウトしてあるはずの
/*Template Name:お問い合わせフォーム*/という部分を削除します。
<?php
if(isset($_POST['submitted'])) {
//項目チェック
if(trim($_POST['checking']) !== '') {
$captchaError = true;
} else {
//名前の入力なし
if(trim($_POST['contactName']) === '') {
$nameError = '名前が入力されていません';
$hasError = true;
} else {
$name = trim($_POST['contactName']);
}
//メールアドレスの間違い
if(trim($_POST['email']) === '') {
$emailError = 'メールアドレスが入力されていません';
$hasError = true;
} else if (!preg_match('|^[0-9a-z_./?-]+@([0-9a-z-]+.)+[0-9a-z-]+$|', trim($_POST['email']))) {
$emailError = 'メールアドレスが正しくありません';
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//お問い合わせ内容の入力なし
if(trim($_POST['comments']) === '') {
$commentError = 'お問い合わせ内容が入力されていません';
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['comments']));
} else {
$comments = trim($_POST['comments']);
}
}
//エラーなしの場合、メール送信
if(!isset($hasError)) {
mb_language("japanese");
mb_internal_encoding("UTF-8");
$emailTo = get_option('admin_email');
$subject = 'お問い合わせ';
$body = "お名前: $name nnメールアドレス: $email nnお問い合わせ内容: $comments";
$title = get_bloginfo('name');
$from = mb_encode_mimeheader(mb_convert_encoding("$titleのお問い合わせ","UTF-8"));
$headers = 'From: '.$from.' <'.$emailTo.'>';
mb_send_mail($emailTo, $subject, $body, $headers);
//自動返信用
$subject = 'お問い合わせ受付のお知らせ';
$from = mb_encode_mimeheader(mb_convert_encoding("$title","UTF-8"));
$headers = 'From: '.$from.' <'.$emailTo.'>';
$body = "$name 様 nnお問い合わせありがとうございます。n改めて担当者よりご連絡をさせていただきます。nnお名前: $name nnメールアドレス: $email nnお問い合わせ内容: $comments";
mb_send_mail($email, $subject, $body, $headers);
$emailSent = true;
}
}
} ?>
<?php get_header(); ?>
<main>
<?php if(isset($emailSent) && $emailSent == true) { ?>
<h1 class="entry-title archive-title"><?=$name;?>様、お問い合わせありがとうございます。</h1>
メールを確認次第、返事をさせて頂きます。
<?php } else { ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h1 class="entry-title archive-title"><?php the_title(); ?></h1>
<?php the_content(); ?>
<form action="<?php the_permalink(); ?>" id="contactForm" method="post">
<ol class="forms">
<li><label for="contactName">お名前</label>
<input type="text" name="contactName" id="contactName" value="<?php if(isset($_POST['contactName'])) echo $_POST['contactName'];?>" class="requiredField" />
<?php if($nameError != '') { ?>
<span class="error"><?=$nameError;?></span>
<?php } ?>
</li>
<li><label for="email">メールアドレス</label>
<input type="text" name="email" id="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" class="requiredField email" />
<?php if($emailError != '') { ?>
<span class="error"><?=$emailError;?></span>
<?php } ?>
</li>
<li class="textarea"><label for="commentsText">お問い合わせ内容</label>
<textarea name="comments" id="commentsText" rows="20" cols="30" class="requiredField"><?php if(isset($_POST['comments'])) { if(function_exists('stripslashes')) { echo stripslashes($_POST['comments']); } else { echo $_POST['comments']; } } ?></textarea>
<?php if($commentError != '') { ?>
<span class="error"><?=$commentError;?></span>
<?php } ?>
</li>
<li class="buttons"><input type="hidden" name="submitted" id="submitted" value="true" /><button type="submit">送信する</button></li>
</ol></form>
<?php endwhile; ?>
<?php endif; ?>
<?php } ?>
</main>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
そうすると、
固定ページを作成する際にテンプレート属性で「お問い合わせフォーム」と選択できた部分が、
選択不能になりますが、
ブログ上ではちゃんとしっかりとコンタクトフォームとして機能しています。
(お問い合わせ先とも言う…。)
なので、
まだまだ零細的な当ブログでは機能しているので問題ありません。
一応スタイルcssは以下です。(CSS)
/*コンタクトフォーム*/
.forms {
margin:16px
}
.forms li {
margin:16px;
list-style:none
}
.forms label {
cursor:pointer;
display:block;
font-weight:800
}
.forms input,.forms textarea {
border:1px solid #7E8AA2;
border-radius:8px;
padding:8px;
width:240px;
font-size:100%
}
.forms li .error {
font-size:80%;
color:red
}
.forms li.textarea .error {
display:block;
position:absolute;
right:0;
top:0
}
.forms li.buttons button {
cursor:pointer;
padding:8px
}
/*コンタクトフォーム*/
特にCSSは指定しなくても普通に機能します。
また、若干のバグなのか右サイドバーがカラム落ちしちゃってますが…。
気にしません。
何より機能するかしないかが一番重要です!!
以上、
プラグインなしでコンタクトフォーム(お問い合わせ先)を追加・作成する方法でした。
参考になれば幸いです。
追記)2016年2月17日の記事で訂正版を修正して投稿しました。
お手数ですが、
固定ページでの属性の選択ができるように修正したphpコードをお試ししてみてください。m(__)m
以下です。
続、プラグインなしでコンタクトフォームを作成方法 | せきろぐ
よろしくお願いします。
[…] WordPressプラグインなしでコンタクトフォームを作成方法 – WordPressデビュー津々浦々 […]