Contact Form 7 -continued

Contact form 7 save uploaded files in server and display uploaded image in mail(not as mail attachment)

Action hook: wpcf7_before_send_mail

The best way is to create a custom post type and save posted datas.

First, create contact form. Here my form name is ‘Application Form’.

Then create post type:

function create_posttype() {
   register_post_type( 'online_applications',
     'labels' => array(
         'name' => __( 'Online Applications' ),
         'singular_name' => __( 'Online Application' )
      'public' => true,
      'has_archive' => true,
      'rewrite' => array('slug' => 'online_applications'),
      'supports' => array('title','thumbnail')
add_action( 'init', 'create_posttype' );

Then call function to save posted datas:

add_action('wpcf7_before_send_mail', 'save_application_form' );
function save_application_form($wpcf7)
    global $wpdb;
    $formtitle  = $wpcf7->title;
    $submission = WPCF7_Submission::get_instance();
    if ($submission) {
        $submited = array();
        $formData       = $submission->get_posted_data();
        $uploaded_files = $submission->uploaded_files(); 
        if ( $formtitle == 'Application Form') {
          $newpost = array(
          'post_type' => 'online_applications',
          'post_title' => $formData['name']. ' - ' .$formData['email'],
          'post_status' => 'pending');

          $newpostid = wp_insert_post($newpost);
          add_post_meta($newpostid, 'name', $formData['name']);
          add_post_meta($newpostid, 'email', $formData['email']);
          add_post_meta($newpostid, 'photograph', $formData['photograph']);

          //save photograph
          $wud = wp_upload_dir();
          $image_name     = $formData['photograph'];
          $image_location = $uploaded_files["photograph"];
          $image_content  = file_get_contents($image_location);
          $upload = wp_upload_bits($image_name, null, $image_content);
          $chemin_final = $upload['url'];
          $filename = $upload['file'];
          if ($filename > '') {
              require_once(ABSPATH . 'wp-admin/includes/admin.php');
              $wp_filetype = wp_check_filetype(basename($filename), null);
              $attachment = array(
                  'post_mime_type' => $wp_filetype['type'],
                  'post_title' => preg_replace('/\.[^.]+$/', '', basename($filename)),
                  'post_content' => '',
                  'post_status' => 'inherit'
              $attach_id = wp_insert_attachment($attachment, $filename, $newpostid);
              require_once(ABSPATH . 'wp-admin/includes/image.php');
              $attach_data = wp_generate_attachment_metadata($attach_id, $filename);
              wp_update_attachment_metadata($attach_id, $attach_data);
              update_post_meta($newpostid, "_thumbnail_id", $attach_id);

          //code to display uploaded image in mail template - starts here
          $photo_url = wp_get_attachment_url( get_post_thumbnail_id($newpostid) );
          $photo_url = isset($formData['photo_url']) ? $formData['photo_url'] : $photo_url;
          $photo_url = '<img src="'.$photo_url.'" alt="photo" width="250" height="250" />';
          // do some replacements in the cf7 email body
          $mail         = $wpcf7->prop('mail');
          // Find/replace the "[photo_url]" tag as defined in your CF7 email body
          // and add changes name
          $mail['body'] = str_replace('[photo_url]', $photo_url, $mail['body']);
              "mail" => $mail
          //code to display uploaded image in mail template - ends here
        // return current cf7 instance
        return $wpcf7;

Contact Form 7

Contact form 7 multiple file upload using single file upload field

Contact form 7 set post title in textbox
Install “Contact Form 7 Dynamic Text Extension” plugin.
Then add form tag like this:

[dynamictext* dynamicname readonly "CF7_get_post_var key='title'"]

Contact form dynamic text fields

Pop up contact form 7
The easy way is show the contact form in a popup window using the plugin “Easy fancy box“.
Once you created the contact form it generate a short code something like this

[contact-form-7 id="150" title="Contact form 1"]

Now just copy the below code and paste it where you want the popup to be

<a href=“#form_pop” class=“fancybox” id=‘fancybox’ >Contact Us</a>
<div style=“display:none” class=“fancybox-hidden”>
    <div id=“form_pop”>
        [contactform7 id=“150” title=“Contact form 1”]

contact form 7 custom mail tag
Eg Code:

add_filter( 'wpcf7_special_mail_tags', 'your_special_mail_tag', 10, 3 );
function your_special_mail_tag( $output, $name, $html ) {
if ( 'yourtagname' == $name ) {
   $output = date('d/m/Y' );
return $output;

Then add [yourtagname] in your mail template.

Custom validation
Eg code:

add_filter('wpcf7_validate_text','custom_text_validation_filter', 10, 2);
add_filter('wpcf7_validate_text*', 'custom_text_validation_filter', 10, 2);
function custom_text_validation_filter($result, $tag) {
   $type = $tag['type'];
   $name = $tag['name'];
   $value = trim($_POST[$name]);
   if($name == 'subject') {
            $result->invalidate( $tag, "The field is too long.Please keep subject under 50 characters." );
         } else if (preg_match('/[\'^£$%*()}{@#~><>|=_+¬]/', $value)){
            $result->invalidate( $tag, "Invalid characters." );

Contact form 7 as registration form
Useful Links:


Install MongoDB on Ubuntu 14.04

Step 1 — Importing the Public Key

sudo apt-key adv --keyserver hkp:// --recv 7F0CEB10

Step 2 — Creating a List File

echo "deb "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list

Step 3 – Update the packages list

sudo apt-get update

Step 4 — Installing and Verifying MongoDB

sudo apt-get install -y mongodb-org

Step 5- Check status

service mongod status

If MongoDB is running, you’ll see an output like this(with a different process ID).
mongod start/running, process 8962

Start MongoDB

service mongod start

Stop MongoDB

service mongod stop

Restart MongoDB

sudo service mongod restart

Git Notes – Basics

Initial commit

mkdir my_project
cd my_project
touch .gitignore
git init
git add .
git commit -m "Initial commit"
git remote add origin
git push origin master

Clone to local repo

git clone folder-name
git add .
git commit -m "Initial commit"
git push origin master

Clone a specific Git branch

git clone -b my-branch

MySQL Export From Remote Server Through Terminal – Ubuntu

mysqldump from remote server

 mysqldump -P3306  -h192.168.20.151 -uroot -p database > /home/manager/Downloads/my.sql

mysqldump specfic table from remote server

 mysqldump -P3306  -h192.168.20.151 -uroot -p database tablename > /home/manager/Downloads/my.sql

mysqldump multiple tables from remote server

 mysqldump -P3306  -h192.168.20.151 -uroot -p database tablename1 tablename2 tablename3 > /home/manager/Downloads/my.sql

Validate xml via terminal – Ubuntu

To validate xml via terminal

install xmlstarlet or xmlint

To install xmlstarlet,
install via ubuntu software center

> apt-get install xmlstarlet

> xmlstarlet val "test.xml"


test.xml - valid


> xmllint --noout "file.xml"

Check if there is a url in the text and make the urls hyper links

Sample Code:

$text = "hi test

echo nl2br($text);

echo '<br>';

// The Regular Expression filter
$reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";

// The Text you want to filter for urls
//$text = "The text you want to filter goes here.";

// Check if there is a url in the text
if(preg_match($reg_exUrl, $text, $url)) {

       // make the urls hyper links
       echo preg_replace($reg_exUrl, '<a href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $text);


Check MySQL Database & Tables Size

Check Single Database Size in MySQL:

SELECT table_schema "Database Name", SUM( data_length + index_length)/1024/1024"Database Size (MB)" FROM information_schema.TABLES where table_schema = 'mydb';

Check ALL Database Size in MySQL:

SELECT table_schema "Database Name", SUM(data_length+index_length)/1024/1024
"Database Size (MB)"  FROM information_schema.TABLES GROUP BY table_schema;

Check Single Table Size in MySQL Database:

SELECT table_name "Table Name", table_rows "Rows Count", round(((data_length + index_length)/1024/1024),2)
"Table Size (MB)" FROM information_schema.TABLES WHERE table_schema = "mydb" AND table_name ="table_one";

Check All Table Size in MySQL Database:

SELECT table_name "Table Name", table_rows "Rows Count", round(((data_length + index_length)/1024/1024),2)
"Table Size (MB)" FROM information_schema.TABLES WHERE table_schema = "mydb";

Export MySQL Tables, Import MySQL Database, Add autoincrement to column through Terminal

Export Database

mysqldump -u root -p db > /path/db_backup.sql

Import Database

mysql -u root -ppassword db <  /var/www/tbl.sql

Export table

mysqldump -uroot -ppassword db tbl > /var/www/tbl.sql

Export table with WHERE clause

mysqldump --opt --user=username --password=password db tbl  --where="id>'207856'" > /var/www/tbl.sql

Import table

mysql -u root -ppassword db <  /var/www/tbl.sql

Add Autoincrement

ALTER TABLE tbl MODIFY COLUMN id INT auto_increment;