Saturday, 19 March 2016

How To Load text domain in wordpress theme?

With this function you take the first step towards making your theme available for translation.

Its best to call this function from within the after_setup_theme() action hook i.e. after the setup, registration,

How to Add feed links (instead of old RSS code in head)

This one is simple. If you’ve been building WordPress themes for a while, you’ll remember the days when you had to manually include code to output the RSS feed right in the header.php. This approach is cleaner and relies on the wp_head()

Wordpress Attachment display settings


Once we’ve defined the above image sizes (regular, medium and large) — and since by default WordPress doesn’t do it for us

How to enable Featured image function in Wordpress theme?

The featured image function, as the codex explains, allows the author to choose a representative image for Posts, Pages or

Custom Post Types.

To enable this functionality, include the following code in your functions.php:

add_theme_support( 'post-thumbnails' );

We could stop there and leave it up to WordPress to define the thumbnail sizes or we could take control and define them
ourselves. We’ll do the latter, obviously!

How to add Post formats inWordpress theme ?

The post formats feature allows you to customize the style and presentation of posts. As of this writing, there are 9 standardized post formats that users can choose from: aside, gallery, link, image, quote, status, video, audio, and chat.

How to add Custom avatar in wordpress theme?

Most people commenting on blogs online have an avatar associated with them. If, however, they don’t and you don’t particularly like the WordPress default avatar options, you can define your own.

Style the WordPress TinyMCE visual editor

This function allows you to use custom CSS to style the WordPress TinyMCE visual editor.

Create a CSS file named editor-style.css and paste your styles inside. As a placeholder, I like to start with styles in

How to support Navigation menu in Wordpress theme ?

The navigation menu is very important esencial part of  wordpress theme.Navigation menu feature, introduced in WordPress

3.0, allows for the intuitive creation and maintaining of navigation menus in themes.

Monday, 23 November 2015

Best 40 WordPress Interview Questions and answers

1) What is WordPress?
Word press is a best Open Source CMS which allows it to be used free of cost.  You can use it on any kind of personal or commercial website without have to pay a single penny for it. It is built on PHP/MySQL (which is again Open Source) and licensed under GPL.'

WordPress CMS Interview Questions & Answers

Q: What is WordPress?
Ans: WordPress is an open source Content Management System which can be used to built websites and Blogs. WordPress was released in 2003 and it is based on PHP & MYSQL.

Saturday, 21 November 2015

Examples WordPress of Hooks in Action

More than 200 hooks exist in WordPress. Below you will find a few examples of common hooks in use.
Register a Custom Menu in the Admin

How to Add and Remove Your Own Functions

If you would like to hook in your own functions, the process is quite simple. You first need to know a few pieces of information. For actions, you’ll want to know the name of the hook, as well as when exactly it runs.

Types of Action and Filter Hooks

The WordPress Codex has two important pages that can help you orient yourself with what hooks are available in WordPress.

WordPress Action Hooks and Filter Hooks

Introduction

This page documents the API (Application Programming Interface) hooks available to WordPress plugin developers, and how to use them.

Monday, 31 August 2015

Does Contact Form 7 support HTML5 input types?

Yes. Contact Form 7 3.4 and higher support form-tags corresponding to these HTML5 input types: email, tel, url, number, range and date.

How to add a contact form into my post content?

Open the settings page for Contact Form 7, and then open the contact form you want to add.

Each contact form has its own tag (shortcode), such as [contact-form-7 id="1234" title="Contact form 1"]. To insert the contact form into your post, copy the shortcode and paste it into the post content.

How to Hide WooCommerce "Free" Price Label

A common WooCommerce question lately has been how to hide the “Free” price label in the product/category/shop pages. Thanks to WooCommerce’s judicious use of filters/actions, this is very easy

Friday, 12 June 2015

How to Set default thumbnails size & creating multiple thumbnail sizes?

Registers a new image size. This means WordPress will create a copy of the image with the specified dimensions when a new image is uploaded.
To set the default Featured Image (formerly Post Thumbnail) dimensions use: set_post_thumbnail_size()
<?php add_image_size$name$width$height$crop ); ?>
if ( function_exists( 'add_theme_support' ) ) {
   add_theme_support( 'post-thumbnails');
   set_post_thumbnail_size( 200, 200, true );
}

What does above code really mean?

Does it mean all my thumbnails will be 800x600 or smaller? Because even after setting set_post_thumbnail_size get_post_thumbnail_id($post->ID) points to full-sized thumbnails (like 2500x1200). I don't really need that big images...

When do WordPress create (custom) thumbnails?

Set the image size by cropping the image (not showing part of it):
Set the image size by resizing the image proportionally (without distorting it):

add_image_size( 'custom-size', 220, 220, array( 'left', 'top' ) ); // Hard crop left top

When setting a crop position, the first value in the array is the x axis crop position, the second is the y axis crop position.
  • x_crop_position accepts 'left' 'center', or 'right'.
  • y_crop_position accepts 'top', 'center', or 'bottom'.
By default, these values default to 'center' when using hard crop mode.

if ( function_exists( 'add_theme_support' ) ) {
  add_theme_support( 'post-thumbnails');
  set_post_thumbnail_size( 900, 600, true );
  add_image_size( 'foo', 400, 300, true );
  add_image_size( 'bar', 200, 250, true ); 
}


When are these custom thumbnails created (like image-900x600.jpg / image-400x300.jpg etc.) ? Only during upload process? What if I'm just changing themes to theme based on add_image_size functions with different thumbnail sizes, do I have to reupload all my images to get right sizes? If yes, then using timthumb over add_image_size wasn't that bad idea..

How to set different thumbnail sizes per custom post type?

Registers a new image size. This means WordPress will create a copy of the image with the specified dimensions when a new image is uploaded.
To set the default Featured Image (formerly Post Thumbnail) dimensions use: set_post_thumbnail_size().

Usage

<?php add_image_size$name$width$height$crop ); ?>

if you have multiple post types, maybe this will help
(not tested)(in the loop)

<?php
$post_type = get_post_type(); // get's the post type of a post in the loop
$args=array(
  'public'   => true,
  '_builtin' => false
);
$output = 'names'; // names or objects
$operator = 'and'; // 'and' or 'or'
$post_types = get_post_types($args,$output,$operator); // gets all post types you registered
$post_types = array_values($post_types); // makes it a numerically indexed array

if(in_array( $post_type, $post_types)){ // post is a custom post type -> show post type size thumbnail
  $custom_post_thumbnail = get_the_post_thumbnail($post->ID, $post_type.'-thumbnail');
  if($custom_post_thumbnail != ''){ // there is a custom post thumbnail size
    echo $custom_post_thumbnail;
  } else { // no custom post thumbnail size -> show normal thumbnail
    echo get_the_post_thumbnail($post->ID);
  }
} else { // show normal thumbnail for posts and pages
  echo get_the_post_thumbnail($post->ID);
}
?>


You still have to add the image sizes manually in your theme's functions.php

If you have custom post types review and movies:

add_image_size( 'review-thumbnail', 150, 200, true );
add_image_size( 'movies-thumbnail', 400, 9999 );


How to Display Featured Post with Thumbnails Without Plugin in Homepage

On home page, we generally show a fixed number of recent posts with summary. Although, WordPress provides Sticky Posts functionality but not so useful when number of featured posts are more. So I decided to implement this without any plugin.

1. Display featured post with thumbnail.
2. Title will be placed below the thumbnail.
3. Featured posts will be randomly selected and must show default thumbnail if no thumbnail is defined for the post.
4. All selected posts must appear together, no slider to navigate.
5. It must not use any script(Not good for SEO) or plugin.

1. To implement this, First, You need to create a category (say featuredcategory) and add featured posts in the category.
2. Write following code in function.php of your WordPress theme.

<?php
function getFeaturePosts(){
 ?>             
                <ul class="related-posts">
                <?php
                $default_thumbnail = 'http://img.techbrij.com/techbrij%20logo.gif';
                $the_query = new WP_Query('showposts=5&orderby=rand&category_name=featuredcategory');
                while ($the_query->have_posts()) : $the_query->the_post(); ?>
                        <li>
                             <div class="related_thumbnail">
            <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">                           
                            <?php
                            if (has_post_thumbnail()):
                             the_post_thumbnail();
                            else:
                            ?>
                            <img src="<?php echo $default_thumbnail; ?>" alt="<?php the_title(); ?>" />
                            <?php endif; ?>
                            </a>
        </div>
        <div style="clear:both;"></div>
            <div class="related_permalink">
            <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>      
        </div>
                             
                        </li>            
 
                <?php endwhile; ?>
                <?php wp_reset_query(); ?>
                </ul>
<?php
}
 ?>

In the above code, you need to define $default_thumbnail parameter and assign default thumbnail link. If there is no thumbnail then defined $default_thumbnail link will be displayed.
Following line is the most important line.
$the_query = new WP_Query(‘showposts=5&orderby=rand&category_name=featuredcategory’);
showposts=5: define number of posts to be displayed
orderby=rand: posts are randomly selected
category_name=featuredcategory: define category of featured posts.
3. Add following styles in your css file:
?
/* Related OR Feature Posts */
.related-posts {
    list-style:none;
    margin:0;
    padding:0;
         
}
  
.related-posts li {
    display:block;
    text-align:center;
    float:left;
    margin:0 auto;
    padding-left:3px 10px 3px 0px;   
    width:150px;
    height:175px;
}
.related-posts li :hover{
 
}
.related_permalink{  
    text-align:center;
    padding-top:5px;   
    padding-bottom:5px;
margin-bottom:2px
 
}
 .related_permalink a{
color:black;
font-weight:bold;
 text-decoration:none;
 }
 .related_permalink a:hover{
 text-decoration:underline;
 }
.related_thumbnail{
     text-align:center;
}
.related_thumbnail img
{ border-width:0px;
width:100px;
float:none;
margin:0 auto;
}
/* Related Posts End */
you can change thumbnail size by defining height and width in .related_thumbnail img style.
4. Now call method where you want to display featured posts.
?
<?php  getFeaturePosts(); ?>
See following sample display of featured posts:

Popular Articles