WordPressのメールフォームプラグイン

2007/11/09 | SNS/CMS/ブログ

WordPressでお問い合わせフォームを組み込む必要が出てきた。
プラグインが豊富なところがWordPressの魅力だが、今回は、メールフォーム自体はいくつか見つけたけれど、希望のものが見当たらなかった。で、自作してみた。まだまだアルファ版だが、いちおうプラグインとしてインストールし、メールフォームが必要な箇所の本文中に{cwcf}とかけば動いてくれているっぽい。初期値はメッセージとかは全部英語だが、管理画面から書き換えることができる。もちろん日本語OK。興味のある人は以下のURLからどうぞ。

CWCF contact form plugin for WordPress

<?php
/*
Plugin Name: CW Contact Form
Plugin URI: http://php.tekmemo.com/
Description: Creates a contact form. Enter <strong>{cwcf}</strong> when you need contact form.
Author: kaorun
Author URI: http://php.tekmemo.com/
Version: alpha
*/
  @session_start();
  
  class cwcf
  {
    var $cwcfname_rcpt, $cwcfname_name, $cwcfname_mail, $cwcfname_subj, $cwcfname_addi, $cwcfname_text, $cwcfname_subm;
    var $cwcfreq_rcpt, $cwcfreq_name, $cwcfreq_mail, $cwcfreq_subj, $cwcfreq_addi, $cwcfreq_text, $cwcfreq_subm;
    var $cwcfdef_rcpt, $cwcfdef_name, $cwcfdef_mail, $cwcfdef_subj, $cwcfdef_addi, $cwcfdef_text, $cwcfdef_subm;
    var $params=array(‘name’,’mail’,’addi’,’subj’,’text’,’subm’);
    var  $prefixes=array(‘cwcfname’,’cwcfreq’,’cwcfdef’);
    
    function cwcf()
    {
      foreach($this->prefixes as $prefix){
        foreach($this->params as $param){
          $this->{$prefix.’_’.$param}=get_option($prefix.’_’.$param);
        }
      }
    }
    
    function process($content)
    {
      if(!preg_match(‘|{cwcf}|’,$content)){
        return $content;
      }else{
        $values=array();
        $errors=array();
        $opType=’form';
        if(strtolower($_SERVER[‘REQUEST_METHOD’])==’post’){
          foreach($this->params as $param){
            $values[‘val_’.$param]=(isset($_POST[‘val_’.$param])? $_POST[‘val_’.$param]:”);
            if($this->{’cwcfreq_’.$param} && !strlen($values[‘val_’.$param]) && $this->{’cwcfname_’.$param}){
              $errors[]=$this->{’cwcfname_’.$param}.’ … ‘.get_option(‘cwcfmsg_required’);
            }
          }
          if($values[‘val_mail’]){
            if(!preg_match(“/^[a-z0-9.+-_]+@([a-z0-9-]+(.[a-z0-9-]+)+)$/i”,$values[‘val_mail’])){
              $errors[]=$this->cwcfname_mail.’ … ‘.get_option(‘cwcfmsg_format’);
            }
          }
          
          $now=intval(date(“U”));
          $limit=5;
          $sendFlag=(isset($_SESSION[‘cwcf’][‘send’])? intval($_SESSION[‘cwcf’][‘send’]):0);
          $sentFlag=(isset($_SESSION[‘cwcf’][‘sent’])? intval($_SESSION[‘cwcf’][‘sent’]):0);
          if(!$errors && $sendFlag && ($now – $limit > $sentFlag)){
            $mailFrom=get_option(‘cwcfmail_from’);
            if(!$mailFrom){$mailFrom=$values[‘val_mail’];}
            if(!$mailFrom){$mailFrom=get_option(‘admin_email’);}
            $mailRcpt=get_option(‘cwcfmail_rcpt’);
            if(!$mailRcpt){$mailRcpt=get_option(‘admin_email’);}
            $mailSubj=get_option(‘cwcfmail_subj’);
            if(!$mailSubj){$mailSubj=$values[‘val_subj’];}
            
            $mailBody=”;
            foreach($this->params as $param){
              if($param!=’subm’ && $this->{’cwcfname_’.$param}){
                $mailBody.=’* ‘.$this->{’cwcfname_’.$param}.” : ¥n”;
                $mailBody.=(($values[‘val_’.$param])? $values[‘val_’.$param]:”).”¥n¥n”;
              }
            }
            $mailBody.=”–¥n”;
            $mailBody.=$_SERVER[‘REMOTE_ADDR’].”¥n”;
            $mailBody.=date(“Y-m-d H:i:s”);
            
            $mailBody=str_replace(“¥r¥n”,”¥n”,$mailBody);
            $mailBody=str_replace(“¥r”,”¥n”,$mailBody);
  &nbsp
;         
            if(function_exists(‘mb_language’)){mb_language(‘Japanese’);}
            if(function_exists(‘mb_internal_encoding’)){mb_internal_encoding(get_option(‘blog_charset’));}
            if(function_exists(‘mb_send_mail’)){
              $result=mb_send_mail($mailRcpt,$mailSubj,$mailBody,”From: “.$mailFrom.”¥r¥n”);
            }else{
              $result=mail($mailRcpt,$mailSubj,$mailBody,”From: “.$mailFrom.”¥r¥n”);
            }
            if($result){
              $opType=”rslt”;
              $_SESSION[‘cwcf’][‘sent’]=$now;
            }
          }
        }else{
          foreach($this->params as $param){
            $values[‘val_’.$param]=$this->{’cwcfdef_’.$param};
          }
        }
        
        /*—————————————————————–*/
        $cwcfOutput=array();
        /*—————————————————————–*/
        $cwcfOutput[‘form’]=”;
        $cwcfOutput[‘form’].='<form method=”post” id=”cwcf” action=”‘.$_SERVER[‘REQUEST_URI’].'”>';
        if($errors){
          $cwcfOutput[‘form’].='<ul>';
          foreach($errors as $error){
            $cwcfOutput[‘form’].='<li>’.$error.'</li>';
          }
          $cwcfOutput[‘form’].='</ul>';
        }
        $cwcfOutput[‘form’].='<dl>';
        if($this->cwcfname_name){
          $cwcfOutput[‘form’].='<dt>’.$this->cwcfname_name.(($this->cwcfreq_name)? ‘<span class=”required”>*</span>':”).'</dt>';
          $cwcfOutput[‘form’].='<dd><input type=”text” name=”val_name” value=”‘.$values[‘val_name’].'” size=”30″ /></dd>';
        }
        if($this->cwcfname_mail){
          $cwcfOutput[‘form’].='<dt>’.$this->cwcfname_mail.(($this->cwcfreq_mail)? ‘<span class=”required”>*</span>':”).'</dt>';
          $cwcfOutput[‘form’].='<dd><input type=”text” name=”val_mail” value=”‘.$values[‘val_mail’].'” size=”30″ /></dd>';
        }
        if($this->cwcfname_addi){
          $cwcfOutput[‘form’].='<dt>’.$this->cwcfname_addi.(($this->cwcfreq_addi)? ‘<span class=”required”>*</span>':”).'</dt>';
          $cwcfOutput[‘form’].='<dd><input type=”text” name=”val_addi” value=”‘.$values[‘val_addi’].'” size=”30″ /></dd>';
        }
        if($this->cwcfname_subj){
          $cwcfOutput[‘form’].='<dt>’.$this->cwcfname_subj.(($this->cwcfreq_subj)? ‘<span class=”required”>*</span>':”).'</dt>';
          $cwcfOutput[‘form’].='<dd><input type=”text” name=”val_subj” value=”‘.$values[‘val_subj’].'” size=”30″ /></dd>';
        }
        if($this->cwcfname_text){
          $cwcfOutput[‘form’].='<dt>’.$this->cwcfname_text.(($this->cwcfreq_text)? ‘<span class=”required”>*</span>':”).'</dt>';
          $cwcfOutput[‘form’].='<dd><textarea name=”val_text” cols=”35″ rows=”8″>’.$values[‘val_text’].'</textarea></dd>';
        }
        $cwcfOutput[‘form’].='<dt>&nbsp;</dt>';
        $cwcfOutput[‘form’].='<dd><input type=”submit” value=”‘.$this->cwcfdef_subm.'” /></dd>';
        $cwcfOutput[‘form’].='</dl>';
        $cwcfOutput[‘form’].='</form>';
        /*—————————————————————–*/
        $cwcfOutput[‘rslt’].=get_option(‘cwcfmsg_success’);
        /*—————————————————————–*/
        $_SESSION[‘cwcf’][‘send’]=1;
     return preg_replace(‘|{cwcf}|’, $cwcfOutput[$opType], $content);
   }
    }
    
    function admin()
    {
      add_options_page(‘options-general.php’, ‘CWCF’, 8, basename(__FILE__), array(&$this,’processAdmin’));
    }
    
    function processAdmin()
    {
      $fields=array();
      $fields[‘name’]=array(‘title’=>’Name’,    ‘value’=>”,  ‘required’=>’1′);
      $fields[‘mail’]=array(‘title’=>’Mail’,    ‘value’=>”,
  ‘required’=>’1′);
      $fields[‘addi’]=array(‘title’=>’Additional’,’value’=>”,  ‘required’=>’0′);
      $fields[‘subj’]=array(‘title’=>’Subject’,  ‘value’=>”,  ‘required’=>’1′);
      $fields[‘text’]=array(‘title’=>’Message’,  ‘value’=>”,  ‘required’=>’1′);
      foreach($fields as $field => $fieldValue){
        add_option(‘cwcfname_’.$field,addslashes($fieldValue[‘title’]));
        add_option(‘cwcfreq_’.$field,’0′);
        add_option(‘cwcfname_’.$field,addslashes($fieldValue[‘value’]));
      }
      add_option(‘cwcfdef_subm’,’submit’);
      $otherOptions=array();
      $otherOptions[‘cwcfmsg_required’]  =array(‘title’=>’Error message of “Required”‘,    ‘value’=>’This field is required.’);
      $otherOptions[‘cwcfmsg_format’]    =array(‘title’=>’Error message of “Email format”‘,  ‘value’=>’The format is wrong.’);
      $otherOptions[‘cwcfmsg_success’]  =array(‘title’=>’Thank you message’,        ‘value’=>’Thank you for your contact.’);
      $otherOptions[‘cwcfmail_from’]    =array(‘title’=>’Mail sender of contact form’,    ‘value’=>get_option(‘admin_email’));
      $otherOptions[‘cwcfmail_rcpt’]    =array(‘title’=>’Mail recipient of contact form’,  ‘value’=>get_option(‘admin_email’));
      $otherOptions[‘cwcfmail_subj’]    =array(‘title’=>’Mail subject of contact form’,    ‘value’=>’Contact from [‘.get_option(‘blogname’).’]’);
      foreach($otherOptions as $option => $optionData){
        add_option($option,$optionData[‘value’]);
      }
      
      if(strtolower($_SERVER[‘REQUEST_METHOD’])==’post’){
        foreach(array_keys($fields) as $field){
          update_option(‘cwcfname_’.$field,  $_POST[‘cwcfname_’.$field]);
          update_option(‘cwcfdef_’.$field,  $_POST[‘cwcfdef_’.$field]);
          update_option(‘cwcfreq_’.$field,  ($_POST[‘cwcfreq_’.$field]?$_POST[‘cwcfreq_’.$field]:’0′));
        }
        update_option(‘cwcfdef_subm’,$_POST[‘cwcfdef_subm’]);
        foreach(array_keys($otherOptions) as $option){
          update_option($option,$_POST[$option]);
        }
      }
      
      $values=array();
      foreach($this->prefixes as $prefix){
        foreach($this->params as $param){
          $values[$prefix.’_’.$param]=htmlspecialchars(get_option($prefix.’_’.$param));
        }
      }
      foreach(array_keys($otherOptions) as $option){
        $values[$option]=htmlspecialchars(get_option($option));
      }
      
      $cwcfOutput=”;
      $cwcfOutput.='<div class=”wrap”>';
      $cwcfOutput.='<h2>CWCF setting</h2>';
      $cwcfOutput.='<form name=”form1″ method=”post” action=”‘.$_SERVER[‘PHP_SELF’].’?page=cwcf.php”>';
      $cwcfOutput.='<table cellspacing=”2″ cellpadding=”5″ class=”editform”>';
      foreach($this->params as $param){
        if($param!=’subm’){
          $cwcfOutput.='<tr valign=”top”>';
          $cwcfOutput.='<td>Label of “‘.$fields[$param][‘title’].'”</td>';
          $cwcfOutput.='<td>';
          $cwcfOutput.='<input type=”text” name=”cwcfname_’.$param.'” value=”‘.$values[‘cwcfname_’.$param].'” size=”30″ /> ‘;
          $cwcfOutput.=’Require?:<input type=”checkbox” name=”cwcfreq_’.$param.'” value=”1″ ‘.(($values[‘cwcfreq_’.$param])? ‘checked ‘:”).’/><br />';
          $cwcfOutput.='</td>';
          $cwcfOutput.='</tr>';
          $cwcfOutput.='<tr valign=”top”>';
          $cwcfOutput.='<td>– Default value of “‘.$fields[$param][‘title’].'”</td>';
          $cwcfOutput.='<td>';
          $cwcfOutput.='<input type=”text” name=”cwcfdef_’.$param.'” value=”‘.$values[‘cwcfdef_’.$param].'” size=”40″ />';
          $cwcfOutput.='</td>';
          $cwcfOutput.='</tr>';
        }
      }
      $cwcfOutput.='<tr>';
      $cwcfOutput.='<td>Label of “Submit”</td>';
      $cwcfOutput.='<td><input type=”text” name=”cwcfdef_subm” value=”‘.$values[‘cwcfdef_’.$param].'” size=”40″></td>';
      $cwcfOutput.='</tr>';
      foreach($otherOptions as $option => $optionData){
        $cwcfOutput.='<tr>';
        $cwcfOutput.='<td>’.$optionData[‘title’].'</td>';
        $cwcfOutput.='<td><input type=”text” name=”‘
.$option.'” value=”‘.$values[$option].'” size=”40″ /></td>';
        $cwcfOutput.='</tr>';
      }
      $cwcfOutput.='</table>';
      $cwcfOutput.='<input type=”submit” value=”submit” />';
      $cwcfOutput.='</form>';
      $cwcfOutput.='</div>';
      echo $cwcfOutput;
    }
    
    function css()
    {
      $css=”;
      $css.='<style type=”text/css” media=”screen”>’.”¥n”;
      $css.=”#cwcf dl{margin:0em;font-size:100%;margin-top:0.5em}”.”¥n”;
      $css.=”#cwcf dt{float:left;width:10em;margin-bottom:0.8em;color:#555;text-align:right;font-weight:normal;position:relative;}”.”¥n”;
      $css.=”#cwcf dt .required{color:#f00;}”.”¥n”;
      $css.=”#cwcf dd{margin-left:10.5em;margin-bottom:0.8em;font-size:100%;font-style:normal;padding-left:0.5em;}”.”¥n”;
      $css.=”#cwcf dd.submit{margin-top:2em;}”.”¥n”;
      $css.=”</style>”;
      print($css);
    }
  }
  
  $cwcfObj=& new cwcf;
  add_action(‘admin_menu’,  array(&$cwcfObj,’admin’));
  add_filter(‘wp_head’,    array(&$cwcfObj,’css’), 2);
  add_filter(‘the_content’,  array(&$cwcfObj,’process’), 2);

1件のコメント

  1. サイト管理者のよんよん日記 :

    CW Contact FormをWordPress MU対応にしてみた

    PHPのテクメモさんの手によるメールフォームプラグイン、CW Contact FormをWord Press MU対応にしてみた記録。 ネタ的にも手法的にも『またかよ』ってネタでスマソ。例によって、偉大な…

コメントを残す


守谷市(まちの情報ポータル) 無料アンケートレンタルjpForm.net