PHP CodeSniffer – Coding Standard Tool

Install PHP-Coding-Standards

> pear install PHP_CodeSniffer-2.8.1

Install WordPress-Coding-Standards

> cd /var/www/html
> git clone -b master https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs
> phpcs –config-set installed_paths /var/www/html/wpcs

Install Codeigniter-Coding-Standards

> git clone https://github.com/thomas-ernest/CodeIgniter-for-PHP_CodeSniffer.git
> ant

or

> ant -Dphpcs.dir=”/usr/share/php/PHP/CodeSniffer”

Check Supported standards

> phpcs -i

Uninstall PHP-Coding-Standards

> pear uninstall PHP_CodeSniffer-2.8.1

Check Errors and Warnings

> phpcs file.php

Autofix Errors

> phpcbf file.php

Woocommerce – Display shopping cart details on a WordPress menu

Open your theme’s functions.php.

Add below code:

add_filter('wp_nav_menu_items','wc_custom_menu_cart', 10, 2);
function wc_custom_menu_cart($menu, $args) {

 // Check if WooCommerce is active and add a new item to a menu assigned to Primary Navigation Menu location
 if ( !in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) || 'primary' !== $args->theme_location )
 return $menu;

 $cart_url = WC()->cart->get_cart_url();
 $shop_page_url = get_permalink( woocommerce_get_page_id( 'shop' ) );
 $viewing_cart = __('View your shopping cart', 'exploore');
 $start_shopping = __('Start shopping', 'exploore');
 $count = (int) WC()->cart->cart_contents_count;
 $cart_contents = sprintf(_n('%d item', '%d items', $count, 'exploore'), $count);
 $cart_total = WC()->cart->get_cart_total();
 
 $menu_item = '<li>';
 if ( $count > 0 ) { 
 $menu_item.= '<a class="cart-contents" href="'. $cart_url .'" title="'. $viewing_cart .'">';
 } else {
 $menu_item.= '<a class="cart-contents" href="'. $shop_page_url .'" title="'. $start_shopping .'">';
 }
 $menu_item.= '<i class="fa fa-shopping-cart"></i> ';
 $menu_item.= $cart_contents.' - '. $cart_total;
 $menu_item.= '</a>';
 $menu_item.= '</li>';

 $menu.=$menu_item;

 return $menu;
}

function my_header_add_to_cart_fragment( $fragments ) {
 
 ob_start();
 $cart_url = WC()->cart->get_cart_url();
 $shop_page_url = get_permalink( woocommerce_get_page_id( 'shop' ) );
 $viewing_cart = __('View your shopping cart', 'exploore');
 $start_shopping = __('Start shopping', 'exploore');
 $count = WC()->cart->cart_contents_count;
 $cart_contents = sprintf(_n('%d item', '%d items', $count, 'exploore'), $count);
 $cart_total = WC()->cart->get_cart_total();

 if ( $count == 0 ) {
 ?>
 <a class="cart-contents" href="<?php echo $shop_page_url;?>" title="<?php echo $start_shopping;?>"><i class="fa fa-shopping-cart"></i> <?php echo $cart_contents.' - '. $cart_total;?></a>
 <?php } else { ?>
 <a class="cart-contents" href="<?php echo $cart_url;?>" title="<?php echo $viewing_cart;?>"><i class="fa fa-shopping-cart"></i> <?php echo $cart_contents.' - '. $cart_total;?></a>
 <?php
 }
 $fragments['a.cart-contents'] = ob_get_clean();
 
 return $fragments;
}
add_filter( 'woocommerce_add_to_cart_fragments', 'my_header_add_to_cart_fragment' );

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’.
add-new-contact-form-wordpress-site-wordpress

Then create post type:

function create_posttype() {
   register_post_type( 'online_applications',
   array(
     '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']);
          $wpcf7->set_properties(array(
              "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
https://wordpress.org/plugins/multifile-upload-field-for-contact-form-7/

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
https://wordpress.org/plugins/contact-form-7-dynamic-text-extension/

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”]
    </div>
</div>

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') {
      if(!empty($value)){
         if(strlen($value)>50){
            $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:
http://www.mootpoint.org/blog/wordpress-contact-form-7-user-registration/
https://wordpress.org/plugins/frontend-registration-contact-form-7/

 

Change WordPress URLs in Database When Site is Moved to new Host

Please run below code in your phpMyAdmin.

UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl', 'http://www.newurl') WHERE option_name = 'home' OR option_name = 'siteurl';

UPDATE wp_options SET option_value = replace(option_value, 'http://www.oldurl','http://www.newurl');

UPDATE wp_posts SET guid = replace(guid, 'http://www.oldurl','http://www.newurl');

UPDATE wp_posts SET post_content = replace(post_content, 'http://www.oldurl', 'http://www.newurl');

UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.oldurl','http://www.newurl');