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, July 24, 2015

Login and Registration on 2 different pages in woocommerce

Today we will learn, How to separate Login and Registration page in woo commerce.

Edit your form-login.php under the wp-content/themes/yourthemefolder/woocommerce/myaccount folder. And seprate the Login and Registration form in 2 different section.

Now check for a GET parameter in the page which will define which section to show. By default login will be shown, if parameter is found and is "signup", show registration section. Like below 

if( isset( $_GET['action']) && $_GET['action'] == "signup"){
    Section for registration form
}else {
    Section for Login form
}


We can provide a link for registration as 
<a href="' .get_permalink(woocommerce_get_page_id('myaccount')). '?action=signup"> Register </a>

Friday, July 17, 2015

Wordpress : Adding Multiple Sidebars

Sidebars are great because they allow us to display a lot of useful info, such as related posts, author info, a blog roll, ad spaces and so on. But sidebars can quickly become very busy, and readers may be hard-pressed to find what they’re looking for. So, what about having different sidebars available and displaying the most appropriate one for the post?
The solution.
To apply this hack, duplicate your sidebar.php file and fill it with whatever information we would like to appear. Save the file as sidebar-whatyouwant.php.
Once that’s done, open single.php* file and find the call to theget_sidebar() function:
<?php get_sidebar(); ?>
Replace it with:
<?php $sidebar = get_post_meta($post->ID, "sidebar", true);
get_sidebar($sidebar);
?>
Now when we write a post, create a custom field named sidebar. Set its value as the name of the sidebar that you want to include. For example, if its value isright, WordPress will automatically include sidebar-right.php as a sidebar.

If no custom sidebar field is found, WordPress automatically includes the default sidebar.
*The same can be done with page.php.
This trick is quite simple. The first thing we did was look for a custom field namedsidebar and get its value as a variable. Then, the variable is used as a parameter for the WordPress function get_sidebar(), which allows us to specify a particular file to use as a sidebar.

Friday, July 10, 2015

Differences between Magento 1.0 and Magento 2.0

Magento 1.0Magento 2.0
Folder app/code includes subfolders: core, community, localFolder app/code includes subfolders Magento and Zend. In Magento 1.0,  Magento andZend are subfolders of the folder core
Codes created by developers are located at app/code/local or app/code/communityCodes created by developers are written directly in app/code. There is no difference between local and community
Module declaration file is a xml file in app/etc/modulesEg:  Module Checkout in Magento Core is declared in app/etc/modules/Mage_All.xmlModule declaration file is always module.xml which is written directly to folder etc in the moduleEg:  module Checkout in Magento Core is declared in app/code/Magento/Checkout/etc/module.xml
Layout and template are saved in folder app/designEg:  app/design/frontend/default/default/layoutLayout and template are saved in the folder “View” in the module. The folder is the same level with some folders like: Block, Controller, Helper, Model, etc. in the moduleEg:  app/code/Magento/Hello/view/frontend/layout

Friday, July 3, 2015

Wordpress: Introduction to hooks

What are wordpress hooks

Hooks are provided by WordPress to allow our plugin to 'hook into' the rest of WordPress; that is, to call functions in our plugin at specific times, and thereby set our plugin in motion. There are two types of hooks: Actions (Codex Action Reference) Filters (Codex Filter Reference)

Action Hooks

Action hooks are essentially placeholders. Wherever an action hook is placed, it will execute any code that has been "hooked" to it.

An example of this is the commonly used wp_head action hook, used by many themes and plugins to inject additional CSS stylesheets, processing code or anything else they require to sit between the <head> and </head> tags of their WordPress theme’s XHTML structure. This is the reason for including wp_head(); in all WordPress themes.

To hook on to an action, we create a function in our theme’s functions.php file (or in our plugin’s code) and hook it on using the add_action() function, as follows:

<?php
add_action( 'wp_head', 'wp_actionhook_example' );

function wp_actionhook_example () {

echo '<meta name="description" content="This is the meta description for this page." />' . "
";

}
?>


Filter Hooks

Filters are functions that WordPress passes data through, at certain points in execution, just before taking some action with the data (such as adding it to the database or sending it to the browser screen). Filters sit between the database and the browser (when WordPress is generating pages), and between the browser and the database (when WordPress is adding new posts and comments to the database); most input and output in WordPress passes through at least one filter. WordPress does some filtering by default, and your plugin can add its own filtering.

Custom code is added as a filter using the add_filter() function. The following code adds a sign-off to the end of each blog post, only when viewing the full blog post screen:

<?php
add_filter( 'the_content', 'wp_filterhook_signoff' );

function wp_filterhook_signoff ( $content ) {

if ( is_single() ) {

$content .= '<div class="sign-off">Th-th-th-th-th That\'s all, folks!</div>' . "
";

} // End IF Statement

return $content;

}
?>
The above code adds a new div tag to the end of the content of our blog post, only when on a single blog post screen.

A filter hook is like using the str_replace() function in PHP. You give it some data, manipulate, replace or reformat the data and return the new content out at the end.