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:

How to regenerate thumbnails in wordpress

There are times when you may want to change the thumbnail or featured image dimensions in your current WordPress theme or you may have planned to switch to a new theme. In both of these cases, you would feel the need of regenerating thumbnails, compatible to new dimensions. In this post, we have shared a plugin called Regenerate Thumbnails, which allows you to regenerate the new thumbnails after changing the thumbnail sizes in WordPress.


What’s the use of this plugin?

This plugin is very useful if you have changed the thumbnail dimensions (sizes) recently in WordPress (Settings » Media) or if you have switched to a new theme, which is having different thumbnail (featured image) dimensions.

Plugin Configuration

You will be able to configure the plugin's settings for your own particular needs by doing the following:
  1. From the left hand navigation menu, click Tools > Regen. Thumbnails.
  2. On the next screen, click Regenerate All Thumbnails.

The plugin will then regenerate new image sizes as defined by your theme or on the Settings > Media page

Regenerate  Thumbnails by other plugin

find the AJAX Thumbnail Rebuild to be better than the regenerate thumbnails plugin, because it doesn't tie up your server as bad, allows you to skip certain sizes (which saves time), and gives more visual feedback.

Thursday 11 June 2015

How to solve a conflict between a plugin and a theme in wordpress?

The default transition effect between slides is a crossover effect (like this example), but the effect in my slider seems to be image fade to white, then white fades to image. Not sure why this is happening.
Here is the code that calls other scripts:

The original solution (at the bottom) worked in a specific plugin situation (with WP Touch).
I think this is the proper one:
add_action( 'wp_enqueue_scripts', 'wpse_77772_remove_theme_enqueues', 11 );

function wpse_77772_remove_theme_enqueues()
{
    if( is_front_page() || is_home() ) 
    {
        wp_dequeue_script('hatch_pro_fancybox');
        // etc
    }
}

**
 * Proper way to enqueue scripts and styles
 */
function theme_name_scripts() {
 wp_enqueue_style( 'style-name', get_stylesheet_uri() );
 wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}

add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

Sunday 7 June 2015

Wordpress Developer Documentation

WordPress is fast, lightweight, and easy to use. To ensure it stays that way, the Core Team thinks carefully about adding functionality to the core WordPress code. Still, users often find the need to graft additional functionality onto WordPress to meet their needs. This section of the Codex offers guidelines and references for anyone wishing to modify, extend, or contribute to WordPress.

Tuesday 2 June 2015

wordpress function cheat sheet



1.Theme Structure

header.php ...................... Header Section
index.php ......................... Main Section
sidebar.php .................... Sidebar Section
single.php ....................... Post Template
page.php ......................... Page Template
comments.php .................. Comment Template
search.php ...................... Search Content
searchform.php ............ Search Form Template
archive.php ................... Archive Template
functions.php ................ Special Functions
404.php .................... Error Page template
style.css .......................... Style Sheet

BlogInfo Tags

<?php bloginfo('name'); ?> ...........................Title of the blog
<?php bloginfo('charset'); ?> ........................The character set
<?php bloginfo('description'); ?> ..........The description of the blog
<?php bloginfo('url'); ?> ......................The address of the blog
<?php bloginfo('rss2_url'); ?> .............................The RSS URL
<?php bloginfo('template_url'); ?> .............The URL of the template
<?php bloginfo('pingback_url'); ?> ....................The pingback URL
<?php bloginfo('stylesheet_url'); ?> The URL for the template's CSS file
<?php bloginfo('wpurl'); ?> .............URL for WordPress installation
<?php bloginfo('version'); ?> ....Version of the WordPress installation
<?php bloginfo('html_type'); ?> ...............HTML version of the site

2.BlogInfo Tags

is_home() .......................When the user is on the blog home page
is_front_page() ......................When the user is on the home page
is_single() ..............................When the single post displayed
is_sticky() ..................................Check if a post is sticky
is_page() .....................................When a page is displayed
is_category() ..............................When a category is displayed


3.Theme Definition

/*
Theme Name: Wordpress Theme
Theme URI: http://wordpress.org/
Description: Test Blog
Version: 1.6
Author: Ekin Ertaç
Author URI: http://themesmart.org
Tags: powerful, cheat, sheet
*/

4.WordPress Template Tags

<?php the_title() ?> .....................Displays the posts/pages title
<?php the_content() ?> ............Displays the content of the post/page
<?php the_excerpt() ?> ....Displays the excerpt of the current post/page
<?php the_time() ?> ..........Displays the time of the current post/page
<?php the_date() ?> .....Displays the date of a post or set of post/page
<?php the_permalink() ?> .............Displays the URL for the permalink
<?php the_category() ?> .................Displays the category of a post
<?php the_author(); ?> ..................Displays the author of the post
<?php the_ID(); ?> ..........Displays the numeric ID of the current post
<?php wp_list_pages(); ?> ........................Displays all the pages
<?php wp_tag_cloud(); ?> ...........................Displays a tag cloud
<?php wp_list_cats(); ?> ........................Displays the categories
<?php get_calendar(); ?> ..........................Displays the calendar
<?php wp_get_archives() ?> ..........Displays a date-based archives list
<?php posts_nav_link(); ?> ...Displays Previous page and Next Page links
<?php next_post_link() ?> .....................Displays Newer Posts link
<?php previous_post_link() ?> ....................Displays previous link
<?php edit_post_link(__('Edit Post')); ?> ........Displays the edit link
<?php the_search_query();?> ................Value for search form
<?php wp_register();?> ................Displays the register link
<?php wp_loginout();?> ..............Displays the log in/out link
<?php wp_meta();?> .......................Meta for administrators
<?php timer_stop(1);?> .....................Time to load the page
<?php echo c2c_custom('test');?> ...... Displays the custom field1
<?php get_links_list(); ?> ...........Display links from Blogroll
<?php get_calendar(); ?> ..........Displays the built-in calendar
<?php comments_popup_link(); ?> .......Link of the posts comments

5.The Loop

<?php if(have_posts());?>
<?php while(have_posts()); the_post();?>
// The Stuff... Custom HTML & PHP Code
<?php else;?>
<?php endif;?>

6.The Category Based Loop

<?php query_posts('category_name=
Category&showposts=10'); ?>
<?php while (have_posts()) : the_post(); ?>
// The Stuff... Custom HTML & PHP Code
<?php endwhile;?>

7.Template Include Tags

< ?php get_header(); ?>
< ?php get_sidebar(); ?>
< ?php get_footer(); ?>
< ?php comments_template(); ?>

8.Navigation Menu

Category Based Navigation
<ul id="menu">
<li <?php if(is_home()) { ?> class="current-cat"< ?php } ?>>
<a href="<?php bloginfo('home'); ?>">Home</a></li>
< ?php wp_list_categories('title_li=&orderby=id'); ?>
</ul>
Pages based Navigation
<ul id="menu">
<li <?php if(is_home()) { ?> class="current_page_item"< ?php } ?>>
<a href="<?php bloginfo('home'); ?>">home</a></li>
< ?php wp_list_pages('sort_column=menu_order&depth=1&title_li='); ?>
</ul>

Wednesday 27 May 2015

How to create custom theme options in wordpress ?


How to add custom logo in wordpress theme

The WordPress theme customizer is famous for being able to change your theme colors in real time, but did you know it can handle almost any type of theme option?
You can add layout options, image uploaders, text fields, and lots more. The possibilities are endless!
There is almost no need for theme options pages anymore. I personally like the idea of having no theme options page, and limiting everything to the theme customizer.
I’m going to show you how to add a custom logo uploader to your theme customizer, then display it in place of the site title and tagline.

Create the logo uploader setting

This may be review for some of you, but I’ll go over my basic setup for using the theme customizer. In my custom theme folder most of the code is going into /inc/customizer.php, and then we will include that file via functions.php. You could put all the code into functions.php and it will work, but it’s nice to keep functions.php uncluttered.
First, we create our function to register the new settings. I’ve prefixed my function name with m1, but you can use whatever you want:

function m1_customize_register( $wp_customize ) {
    // All the code in this tutorial goes here
}
add_action( 'customize_register', 'm1_customize_register' );
Inside our function we need to register a setting. This saves the logo url in the database so we can use it in our theme.
function m1_customize_register( $wp_customize ) {
    $wp_customize->add_setting( 'm1_logo' ); // Add setting for logo uploader
}
add_action( 'customize_register', 'm1_customize_register' );
Next, we add the uploader and assign it to a section. You could create a new section, but I think it goes nicely with the title and tagline. If you are using a different name for your setting, make sure you change all instances of m1_logo to something else.
function m1_customize_register( $wp_customize ) {
    $wp_customize->add_setting( 'm1_logo' ); // Add setting for logo uploader
         
    // Add control for logo uploader (actual uploader)
    $wp_customize->add_control( new WP_Customize_Image_Control( $wp_customize, 'm1_logo', array(
        'label'    => __( 'Upload Logo (replaces text)', 'm1' ),
        'section'  => 'title_tagline',
        'settings' => 'm1_logo',
    ) ) );
}
add_action( 'customize_register', 'm1_customize_register' );
That’s it for the code in customizer.php. To make sure our theme uses this file, simply add this one line of code in functions.php:
require get_template_directory() . '/inc/customizer.php';
That’s it! We kept our functions.php file clean as a whistle.
Next we need to display our logo.

Displaying the logo

We are going to work in our theme’s header.php file to display the logo. If there is no logo, we’ll just display the site title and description.
To get the logo url from our theme customizer, you use this code:

get_theme_mod( 'm1_logo' )
You can store that in a variable if you want, or echo it out as is. Since we are only using this value once in our theme, we don’t need to store it in a variable.
Wherever your site title is displayed in header.php, you’ll want to replace it with this code:

<?php if ( get_theme_mod( 'm1_logo' ) ) : ?>
    <a href="<?php echo esc_url( home_url( '/' ) ); ?>" id="site-logo" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home">
        <img src="<?php echo get_theme_mod( 'm1_logo' ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>">
    </a>
    <?php else : ?>
               
    <hgroup>
        <h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></h1>
        <p class="site-description"><?php bloginfo( 'description' ); ?></p>
    </hgroup>
               
<?php endif; ?>
So let’s talk through that chunk of code. First, we create an if statement that says, “if a theme logo has been uploaded, display this logo code. Otherwise, display the site title and site description.”
The logo is linked to the homepage, it has some alt text, blah blah blah. The important part is this:

src="<?php echo get_theme_mod( 'm1_logo' ); ?>"
Make sure to tell the user a recommended logo size, otherwise they can upload a huge file that doesn’t fit quite right. You might also want to give the container around the logo a constrained width, so it will fit no matter what.



Popular Articles