Menu bar

Showing posts with label Wordpress. Show all posts
Showing posts with label Wordpress. Show all posts

Wednesday, 30 December 2015

How to show or hide widgets on specfic pages?

The current sample is setup to only display the core ‘pages’ widget on the contact page only. Don’t forget that is_page also accepts an array of page names and id’s.


add_filter( 'widget_display_callback', 'hide_widget_pages', 10, 3 );

function hide_widget_pages( $instance, $widget, $args ) {
  
if ( $widget->id_base == 'pages' ) { // change 'pages' to widget name
     
if ( !is_page( 'contact' ) ) {    // change page name
         
return false;
     
}
  
}
}

Wordpress interview questions and answers

How to store the post title in variable?
  $page_title = get_the_title($post->post_parent);

How to Generates a random password of the specified length in WordPress?
<?php generate_random_password($len) ?>

How to hide the Admin Bar in WordPress?
add_filter('show_admin_bar', '__return_false');

 How to get the postid in wordpress?
global $wp_query;
$postid = $wp_query->post->ID;
echo $postid;

 How to get the post meta value in worpdress?
we can get the post meta value throw postid.
for example here post Custom Field name : comapny name
 get_post_meta($postid, 'Company name', true);


 How to check if the post has a Post Thumbnail assigned to it.
if ( has_post_thumbnail() )  // check if the post has a Post Thumbnail assigned to it.
   { 
the_post_thumbnail();
   }


How to get the wordpress featued image and how to change the image width and height.

we can get the featurd image wordpress function

 the_post_thumbnail();//here we can get the wordpress featured image thumbnail
  the_post_thumbnail( array(100,100) );     // here we can change the image size.

How to get the specified category post only in wordpress?
<?php query_posts("cat=106 && per_page=5");// here 106 is the category id  ?>
<?php while ( have_posts() ) : the_post(); ?>
<h3><?php the_title(); // here we can get the post title. this is the wordpress free defind function ?></h3>
<?php  endwhile; ?>

What is the prefix of wordpress tables by default?
 By default, wp_ is prefix of wordpress.

How can we backup or import our WordPress content from admin panel?
 For import content from wordpress admin panel goes to
WordPress admin -> Tools -> Import


Can wordPress use cookies?
Yes, wordpress use cookies.WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users.

How to disable wordpress comment?
 Look in to dashboard under Options –> Discussion. There is a checkbox there for “allow people to post comments on the article” Try unchecking that.

How many tables a default WordPress will have?

 default wordpress will have 11 tables. They are-
1. wp_commentmeta
2. wp_comments
3. wp_links
4. wp_options
5. wp_postmeta
6. wp_posts
7. wp_terms
8. wp_term_relationships
9. wp_term_taxonomy
10.wp_usermeta
11.wp_users

What are the features of wordpress?
1. Simplicity,Make wordpress to manage easily that is its very easy to use.
2. Free open source ,its free to use wordpress.
3. Easy theme system,In wordpress we have thousand of good free theme to use.
4. Extends with plugins, we can extends the functionality of wordpress using thousands of free plugins or will create any plugin according to your requirements.
5. Community,WordPress has vibrant and supportive community.
6. Multilingual, wordpress is available on more than 70 languages.
7. Flexibility, with wordpress you will create any type of blog or website.
8. Comment, the built in comment system also make wordpress popular as you can comment your views on website.
9. Easy installation and upgrades.
10. Full standards compliance, Easy Importing,Cross-blog communication tools.

Wednesday, 14 January 2015

Publish the post if and only if the featured image is set.

SET FEATURED IMAGE AND PUBLISH IT


Dont allow the user to publish the post until the featured image is set.

sometimes after publsihing the post user come to know that featured image is not set. and he/she will try to add featured image once publsihed.<br />
How it look if the post was unable to publish until the featured image is set?

yes, you can do this with wordpress hook.
you have to use "save_post" hook to check whether the post has thumbnail i.e. the featured image is set or not and you have to use "admin_notices" hook to display message above post title.

place this snippet in your functions.php file

add_action('save_post', 'check_thumbnail');
add_action('admin_notices', 'thumbnail_error');

function check_thumbnail($post_id) {
    if(get_post_type($post_id) != 'post')
        return;
    
    if ( !has_post_thumbnail( $post_id ) ) {
        // set a transient to show the users an admin message
        set_transient( "has_post_thumbnail", "no" );
        // unhook this function so it doesn't loop infinitely and it will not publish until featured image is set.
        remove_action('save_post', 'check_thumbnail');
        // update the post and set it to draft
        wp_update_post(array('ID' => $post_id, 'post_status' => 'draft'));
        add_action('save_post', 'check_thumbnail');
    } else {
        delete_transient( "has_post_thumbnail" );
    }
}

function thumbnail_error()
{
    if ( get_transient( "has_post_thumbnail" ) == "no" ) {
        echo "<div id='message' class='error'><p><b style="color:#F00;">Select Featured Image and Publish it.</b></p></div>";
        delete_transient( "has_post_thumbnail" );
    }

}

Saturday, 10 January 2015

Wordpress Plugins list for post meta

Excellent plugin for Meta Data


  1. Advanced Custom Fields
    This is an excellent plugin to help you add custom metaboxes to save your metadata. The user interface is very polished and all metaboxes are created from right within the WordPress Admin.
  2. Pods - Custom Content Types and Fields
    This is another excellent plugin, but is built with developers in mind. If you need to build something very advanced, and want to manage it from the WordPress dashboard, I recommend you dive into Pods. The developers are very responsive, and the documentation is excellent.
  3. Types - Custom Fields and Custom Post Types Management
    Another advanced solution that will do a lot more than meta-data related fields.
  4. Custom Metaboxes and Fields for WordPress
    This is a library that lets you manage your metabox configuration all in one place via a filter hook. Originally created by Jared Atchison and maintained by Bill Erickson and Andrew Norcross, It's development has been taken over by the crew at WebDevStudios* and is in active development. Definitely check out the wiki for in-depth examples.
  5. Custom Metadata Manager for WordPress
    Another recommended library and is by the fine folks at Automattic, the people behind wordpress.com, so you know you're in good hands.
  6. HM Custom Meta Boxes for WordPress
    Another "CMB" flavor, that was originally forked from Jared Atchison's Custom Metaboxes and Fields for WordPress. It's unique in that it has "a basic layout engine for fields, allowing you to align fields to a simple 12 column grid", among other things.
  7. Fieldmanager
    Fieldmanager is a toolkit for developers to create complex administration screens in WordPress.
    Another advanced toolkit recommended by the folks at Automattic.

Sunday, 4 January 2015

Working with custom fields

USING CUSTOM FIELDS


This function returns the values of the custom fields with the specified key from the specified post. It is a wrapper for get_metadata('post').

<?php $meta_values = get_post_meta( $post_id, $key, $single ); ?>


$key is a string containing the name of the meta value you want.


If only $post_id is set it will return all meta values in an associative array.
If $single is set to false, or left blank, the function returns an array containing all values of the specified key.
If $single is set to true, the function returns the first value of the specified key (not in an array)


Dump out all custom fields as a list
<?php the_meta(); ?>

Display value of one specific custom field
<?php echo get_post_meta($post->ID, 'key', true); ?>
"key" would be ID value of custom field


Display multiple values of same custom field ID
<?php $key_val = get_post_meta($post->ID, 'key_val', false); ?>
<h3>This post inspired by:</h3>
<ul>
<?php foreach($key_val as $key) {
echo '<li>'.$key.'</li>';
} ?>
</ul>


Display custom field only if exists
<?php 
    $url = get_post_meta($post->ID, 'reference-URL', true); 

if ($url) {
   echo "<p><a href='$url'>URL</a></p>";
}
?>

Wednesday, 31 December 2014

Redirect user after successfull registration to a required wordpress page.

REDIRECT USER ON REGISTRATION.

Have you ever wanted to redirect users to a specific wordpress page or another website page after they register to your WordPress site?

Recently i have worked on project where

user must be redirected to particular wordpress page.

Some are interested in thanking people after they register or just send them to a specific wordpress  page for newly registered members.

one can easily do tisk task with a small snippet, putting in functions.php file.

Here you have to use 'registration_redirect' filter.

code:

function redirect_user(){    return home_url( '/pagename/' );}add_filter( 'registration_redirect', 'redirect_user' );


you can return a static url instead of home_url() of any other website.

This task can be done easily through plugin " Peter’s Login Redirect "

Through plugin you can redirect user from login and even from registration page to a page were you want user to be.

Sunday, 28 December 2014

How to remove the width and height from img tag in wordpress posts and pages?


REMOVE WIDTH AND HEIGHT ATTRIBUTES.

It would be great for the images to be able to scale proportionally within their parent container in wordpress to make perfect responsive. This effect can be take place if you remove the width and height attributes from img tag in wordpress.


In order to remove width and height , here we use filter hooks.There are two hooks we will used here. one is post_thumbnail_html and other is image_send_to_editor
 

We will replace width and height attribute with NULL i.e. blank space using preg_replace() function of php.

place this code in functions.php file.
add_filter( 'post_thumbnail_html', 'remove_wps_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_wps_width_attribute', 10 );
 function remove_wps_width_attribute( $html ) {
    $html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
    return $html;
}

Friday, 26 December 2014

Remove HTML unwanted comment tags from comment section.

REMOVE HTML COMMENT TAGS

By default WordPress includes basic HTML tags in the post comment form in order to allow commentators to use it for formatting there comments.

As you can see in the  image, comment form is listing some HTML tags in the below section.


But, many times it is not necessary to show these HTML tags in the form.

Moreover, with design perspective, displaying these tags doesn’t look good in the comment forms. So you can either remove or hide it from the comment form.

You can perform that task easily by CSS.
i.e. Display none that block.

Place this code in function.php file of your theme.

<?phpadd_action('init', 'unwantedtags');function unwantedtags() {global $allowedtags;// remove unwanted tags$unwanted = array('abbr','acronym','blockquote','cite','code','del','strike','strong','b','em','q');foreach ( $unwanted as $tag )unset( $allowedtags[$tag] );}?>


Monday, 22 December 2014

How to show or hide specific widget on specific page?


SHOW OR HIDE SPECIFIC WIDGETS ON SPECIFIC PAGES

Did you ever think to show or hide your WordPress widgets on selective pages...of course you can thought of.Because there are some wordpress widgets that should be display on certain pages to make websites look better then before.

You can do this task by putting this snippet in your function files....

Here you have to change the widgetname to name of the widget to show or hide and you can get that name by inspecting your code..

Change the pagename to the name of page on which the widget to be show or hide and this is the slug name of your page...

//Show widget on particular page..
add_filter( 'widget_display_callback', 'show_hide_widget', 10, 3 );
function show_hide_widget( $instance, $widget, $args ) {
  if ( $widget->id_base == 'widgetname' ) { 
     if ( !is_page( 'pagename' ) ) {
         return false;
     }
  }
}

//Hide widget on particular page..
add_filter( 'widget_display_callback', 'show_hide_widget', 10, 3 );
function show_hide_widget( $instance, $widget, $args ) {
  if ( $widget->id_base == 'widgetname' ) { 
     if ( is_page( 'pagename' ) ) {
         return false;
     }
  }
}

Even there is a plugin used to perform this task and the name of the plugin is "Display Widgets".

Saturday, 1 November 2014

Turn on WordPress Error Reporting


How to turn on error reporting for testing your website?


Sets error reporting to true in wordpress  indicates which PHP errors are reported

Comment out the top line there, and add the rest to your wp-config.php file to get more detailed error reporting from your WordPress site. Definitely don't do this live, do it for local development and testing.

// define('WP_DEBUG', false); define('WP_DEBUG', true); define('WP_DEBUG_LOG', true); define('WP_DEBUG_DISPLAY', false); @ini_set('display_errors', 0);
This changes will be done in wp-config.php
This will log all errors notices and warnings to a file called debug.log in wp-content (if Apache does not have write permission, you may need to create  the file first and set the appropriate permissions (i.e. use 666) )

Monday, 18 August 2014

Speed up the Wordpress.

How to speed up Wordpress

Focus on those point to boost your wordpress site.
Use Effective Caching Plugin
       W3 Total Cache
       Autoptimize

       Eliminate rendering Javascript

Simply install and activate the above mentioned plugin and check your pagespeed. Then you should make changes according to your requirement.

Use a Content Delivery Network (CDN)
       FREE-CDN 

Optimize Images Automatically
       SMUSH IT

This is a image optimizer which will provide you easy gui to optimize image near your media file in media tab of wordpress. This is provided by YAHOO! 

Optimize the Homepage to Load Faster as Possible
     
Show excerpts instead of full posts
      Reduce the number of posts on the page 
      Remove unnecessary widgets from the home page 
      Includes widget in post only
      Remove inactive plugins and widgets that you don’t need


Add an expires header to static resources

       An Expires header is a way to specify a time far enough in the future so that  the clients (browsers) don’t have to re-fetch any static content (such as css file, javascript, images etc).
       This way can cut your load time significantly for your regular users.
    

Tuesday, 15 July 2014

How to add shortcode in a widgets?

How to add shortcode means how to make shortcode runnable when paste  in a widget section.


277341190 3f098a08a4 t The Right Way To Shortcodize WordPress Widgets


Most advice on this converges to this simple line of code:
add_filter('widget_text', 'do_shortcode');
Though a wide spread method, this is not quite correct. The more correct way would be:
if (!is_admin())
  add_filter('widget_text', 'do_shortcode', 11);


if (!is_admin())
  add_filter('widget_text', 'do_shortcode', SHORTCODE_PRIORITY);


to avoid guessing what the number 11 means, but SHORTCODE_PRIORITY is a constant the WordPress team has yet to provide.

Saturday, 5 July 2014

How to disable right click , F12 key and Direct link access?

Stop users from stealing your website images, copying content, or from inspecting the source.

In order to setup a security as above , the first is to disable right click ,disable F12 key and then disable direct link access.

Direct link access means that a user get full path of image by writing 'view-source:' before the domain name.
After getting the full path, user will type the full path of image in address bar to access images, So we have to avoid this attack also.
Use this code in your header file or where ever you required.

<body oncontextmenu="return false;">
Fires when the user clicks the right mouse button in the client area, opening the context menu.


<meta http-equiv="imagetoolbar" content="no" />
This turns off Internet Explorer's image toolbar that appears when you hover over an image.
Use this code within <head > tag.


RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?yourdomain.com [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ - [NC,F,L]
Write this code in your .htaccess file

Saturday, 28 June 2014

Redirection in wordpress with authorization and without authorization?

wp_redirect


The wp_redirect function is for redirecting all users to any absolute. URI. Absolute URIs are basically full URLs and look like this:

http://momin.com/book/http://momin.com/wp-contentredirecting-users.jpgftp://wordpress.com/transfer/

To redirect users with wp_redirect, place the following PHP snippet in your theme file with appropriate URL:

<?php
wp_redirect('http://example.com/'); exit; 
 ?>

The two parameters of wp_redirect are:
1> $location = The absolute URI to which the user will be redirected. No default.
2> $status = The status code to use. For example, 301, 302, etc. The default is 302.

<?php
// redirect to home page
wp_redirect(home_url()); exit; 
?>

<?php
// redirect back to current page
wp_redirect(get_permalink()); exit; 
?>

auth_redirect:

This extremely useful function checks whether or not the current user is logged in, and redirects them to the Login Page if not. By default, the user will be redirected back to the page from whence they came . Currently this function accepts no parameters.

<?php
 auth_redirect();
?>



Monday, 16 June 2014

How to style table using CSS?

No explanation needed for you buddy :)
This is as simple as you see here..


Table 1

Html code for Table 1

<table id="gradient-style" summary="Meeting Results">
<thead>
<tr>
<th scope="col">Employee</th>
<th scope="col">Division</th>
<th scope="col">Suggestions</th>
<th scope="col">Rating</th></tr>
</thead>

<tfoot>

<tr><td colspan="4">Give background color to the table cells to achieve seamless transition</td></tr>
</tfoot>

<tbody>

<tr><td>Stephen C. Cox</td><td>Marketing</td><td>Make discount offers</td><td>3/10</td></tr>
<tr><td>Josephin Tan</td><td>Advertising</td><td>Give bonuses</td><td>5/10</td></tr>
<tr><td>Joyce Ming</td><td>Marketing</td><td>New designs</td><td>8/10</td></tr>
<tr><td>James A. Pentel</td><td>Marketing</td><td>Better Packaging</td><td>8/10</td></tr>
</tbody>
</table>


CSS code for Table 1

<style>
#gradient-style{
font-family:"Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size:12px;
width:480px;
text-align:left;
border-collapse:collapse;
margin:20px;
}
#gradient-style th{
font-size:13px;font-weight:normal;
background:#9C9 url("http://www.smashingmagazine.com/images/express-css-table-design/table-images/gradhead.png") repeat-x;
border-top:2px solid #d3ddff;
border-bottom:1px solid #fff;
color:#039;padding:8px;
}
#gradient-style td{
border-bottom:1px solid #fff;
color:#669;
border-top:1px solid #fff;
background:#9c9 url("http://www.smashingmagazine.com/images/express-css-table-design/table-images/gradback.png") repeat-x;
padding:8px;
}
#gradient-style tfoot tr td{
background:#e8edff;
font-size:12px;
color:#99c;
}
#gradient-style tbody tr:hover td{
background:#9c9 url("http://www.smashingmagazine.com/images/express-css-table-design/table-images/gradhover.png") repeat-x;
color:#339;
}
</style>


Table 2

Html code for Table 2


<table id="pattern-style-a" summary="Meeting Results">
<thead>
<tr>
<th scope="col">Employee</th>
<th scope="col">Salary</th>
<th scope="col">Bonus</th>
<th scope="col">Supervisor</th></tr>
</thead>
<tbody>
<tr><td>Stephen C. Cox</td><td>$300</td><td>$50</td><td>Bob</td></tr>
<tr><td>Josephin Tan</td><td>$150</td><td>-</td><td>Annie</td></tr>
<tr><td>Joyce Ming</td><td>$200</td><td>$35</td><td>Andy</td></tr>
<tr><td>James A. Pentel</td><td>$175</td><td>$25</td><td>Annie</td></tr>
</tbody>
</table>


CSS code for Table 2

<style>
#pattern-style-a{
font-family:"Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size:12px;
width:480px;
text-align:left;
border-collapse:collapse;
background:url("http://www.smashingmagazine.com/images/express-css-table-design/table-images/pattern.png");
margin:20px;
}
#pattern-style-a thead tr{
background:url("http://www.smashingmagazine.com/images/express-css-table-design/table-images/pattern-head.png");
}
#pattern-style-a th{
font-size:13px;
font-weight:normal;
border-bottom:1px solid #fff;
color:#039;
padding:8px;
}
#pattern-style-a td{
border-bottom:1px solid #fff;
color:#669;
border-top:1px solid transparent;
padding:8px;
}
#pattern-style-a tbody tr:hover td{
color:#339;
background:#fff;

}
</style>

Table 3

Html code for Table 3

<table id="pattern-style-b" summary="Meeting Results">
<thead>
<tr>
<th scope="col">Nation</th>
<th scope="col">Capital</th>
<th scope="col">Language</th>
<th scope="col">Unique</th>
</tr>
</thead>
<tbody>
<tr><td>Japan</td><td>Tokyo</td><td>Japanese</td><td>Karate</td></tr>
<tr><td>South Korea</td><td>Seoul</td><td>Korean</td><td>Ginseng</td></tr>
<tr><td>China</td><td>Beijing</td><td>Mandarin</td><td>Kung-Fu</td></tr>
<tr><td>Indonesia</td><td>Jakarta</td><td>Indonesian</td><td>Batik</td></tr>
</tbody>
</table>


CSS code for Table 3

<style>
#pattern-style-b{
font-family:"Lucida Sans Unicode", "Lucida Grande", Sans-Serif;
font-size:12px;
width:480px;
text-align:left;
border-collapse:collapse;
background:url("http://www.smashingmagazine.com/images/express-css-table-design/table-images/patternb.png");
margin:20px;
}
#pattern-style-b thead tr{
background:url("http://www.smashingmagazine.com/images/express-css-table-design/table-images/patternb-head.png");
}
#pattern-style-b th{
font-size:13px;
font-weight:normal;
border-bottom:1px solid #fff;
color:#039;
padding:8px;
}
#pattern-style-b td{
border-bottom:1px solid #fff;
color:#669;
border-top:1px solid transparent;
padding:8px;
}
#pattern-style-b tbody tr:hover td{
color:#339;
background:#cdcdee;
}
</style>

Sunday, 1 June 2014

How to optimize page speed of your wordpress website quickly?

To make your wordpress website load better then ever, you need to download the plugin named W3 Total Cache , install and activate it....
Now goto Google developer tool to check your pagespeed result on Desktop as well as on Mobile Platform.This is to check that after installing plugin and doing following setting  as shown in the following images , you will get the better result then ever.
After installing the plugin , see below 5 images and do setting  accordingly in your plugin menu tab.

Step : 1 General Setting Tab



Step :2 Page Cache Tab





Step: 3 Minify Tab





Step:4 Database Cache Tab




Step : 5 Browser Cache Tab






Remain all other tab as it is in your plugin setting and check the google page speed again you will definitely have a smile on your face :)