Showing posts with label wordpress. Show all posts
Showing posts with label wordpress. Show all posts

Friday, July 31, 2015

Managing image size with wordpress

If we display an image at a size of 450×300 pixels, then having an image file of roughly the same size is a good idea. If the source file is 2250×1500 pixels, the image will show up just fine, but instead of loading a 50 KB image, we would be loading a 500 KB image, while achieving the same effect.

WordPress is super-smart, though, taking care of this for us by churning out different sizes for each image we upload. See the dimensions it creates by going to the media settings in the back end. We can modify these once we have the final layout.
For an image-centric website, we might want to add a couple of more sizes, to make sure we never serve an image that is bigger than needed. By putting the following code in our theme’s functions.php file, we create two extra sizes:
add_image_size( 'large_thumb', 75, 75, true );
add_image_size( 'wider_image', 200, 150 );
The first line defines an image that is cropped to exactly 75×75 pixels, and the second line defines an image whose maximum dimension is 200×150, while maintaining the aspect ratio. Note the name given in the first argument of the function, because we will be referring to it when retrieving the images, which we can do like so:
wp_get_attachment_image_src( 325, 'wider_image');
The first argument is the ID of the attachment that we want to show. The second argument is the size of the image.

Friday, June 12, 2015

Installing wordpress in root as well as in subdirectory

We have a wordpress site which is runnung in root directory. Now we want to install same site with different database name in subdirectory for testing purpose.

STARTING POINT:
http://www.mywordpress.com   /var/www/
http://www.mywordpress.com/foldername   /var/www/foldername

Step 1:

Put same code in /var/www/foldername as in /var/www/ . Connect it with new database.

Step 2:

Change .htaccess file in /var/www/foldername as given below

# BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /foldername/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /foldername/index.php [L]
</IfModule>

# END WordPress

At this point the configuration will not be working because the .htaccess directives at /var/www/.htaccess with be controlling all aspects of the URL so the directives at /var/www/foldername/.htaccess will have no bearing.

Step 3 :

The final step is to modify the default WordPress htaccess directives so that they will have control in for all variations of the URL.
Now change .htaccess file in /var/www as given below

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^/?foldername/$ /var/www/foldername/index.php [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

Step 4:

Now login to your http://www.mywordpress.com/foldername/wp-admin/ section. Go to Setting->Permalinks.

Choose Post name in common setting.

Now it will work in root as well in subdirectory.

Friday, June 5, 2015

Filters and hooks for modifying woocommerce

1. Add email recipient when order completed

function woo_extra_email_recipient($recipient, $object) {
    $recipient = $recipient . ', your@email.com';
    return $recipient; }

add_filter( 'woocommerce_email_recipient_customer_completed_order', 'woo_extra_email_recipient', 10, 2);

2.  Redirect add to cart button to checkout page

add_filter ('add_to_cart_redirect', 'redirect_to_checkout');

function redirect_to_checkout() {
    global $woocommerce;
    $checkout_url = $woocommerce->cart->get_checkout_url();
    return $checkout_url;

}

3. Add custom field to edit address page

function woo_add_edit_address_fields( $fields ) {
    $new_fields = array(
                'date_of_birth'     => array(
                'label'             => __( 'Date of birth', 'woocommerce' ),
                'required'          => false,
                'class'             => array( 'form-row' ),
            ),
        );      

    $fields = array_merge( $fields, $new_fields );    
    return $fields;  

}


add_filter( 'woocommerce_default_address_fields', 'woo_add_edit_address_fields' );

4. Change “from” email address

function woo_custom_wp_mail_from() {
        global $woocommerce;
        return html_entity_decode( 'your@email.com' );
}

add_filter( 'wp_mail_from', 'woo_custom_wp_mail_from', 99 );

5. Change shop page title

add_filter( 'woocommerce_page_title', 'woo_shop_page_title');

function woo_shop_page_title( $page_title ) {  

    if( 'Shop' == $page_title) {
        return "My new title";
    }

}

6. Rename a product tab

add_filter( 'woocommerce_product_tabs', 'woo_rename_tab', 98);

function woo_rename_tab($tabs) {
 $tabs['description']['title'] = 'More info';
 return $tabs;

}

7. Remove “Products” from breadcrumb

function woo_custom_filter_breadcrumbs_trail ( $trail ) {
  foreach ( $trail as $k => $v ) {
    if ( strtolower( strip_tags( $v ) ) == 'products' ) {
      unset( $trail[$k] );
      break;
    }
  }
  return $trail;

}

add_filter( 'woo_breadcrumbs_trail', 'woo_custom_filter_breadcrumbs_trail', 10 );

8. Empty cart

function my_empty_cart(){
    global $woocommerce;
    $woocommerce->cart->empty_cart();
}


add_action('init', 'my_empty_cart');

9. Change related products number

function woo_related_products_limit() {
  global $product;  

    $args = array(
        'post_type'             => 'product',
        'no_found_rows'         => 1,
        'posts_per_page'        => 6,
        'ignore_sticky_posts'   => 1,
        'orderby'               => $orderby,
        'post__in'              => $related,
        'post__not_in'          => array($product->id)
    );
    return $args;
}


add_filter( 'woocommerce_related_products_args', 'woo_related_products_limit' );

10. Remove breadcrumb

remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20);



Friday, May 29, 2015

Wordpress - multiple file upload from front end

Wordpress provide us multiple file upload from admin section. Now in this section we will learn, how we do the same thing from front end. 

Below is the form for multiple file uploads:

<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="multiple_attachments[]"  multiple="multiple" >

<input type="submit" name="Upload" >
</form>

Below is the simple upload php function to handle multiple files simultaneously.

if( isset($_POST['Upload'])) {
$files = $_FILES["multiple_attachments"]; 

 foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {$file = array('name' => $files['name'][$key],'type' => $files['type'][$key],'tmp_name' => $files['tmp_name'][$key],'error' => $files['error'][$key],'size' => $files['size'][$key]);

$_FILES = array ("multiple_attachments" => $file);


foreach ($_FILES as $file => $array) {$newupload = handle_attachment($file);}}}
}


In the above script, we gather the files from HTML forms and send it to another function called handle_attachment(). This function help us to store the files in our wp uploads directory.

function handle_attachment($file_handler) {
if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();
require_once(ABSPATH . "wp-admin" . '/includes/image.php');


require_once(ABSPATH . "wp-admin" . '/includes/file.php');


require_once(ABSPATH . "wp-admin" . '/includes/media.php');


$attach_id = media_handle_upload( $file_handler);        

}


Put above function in function.php file of current theme.