Load jQuery library from Google CDN

WordPress itself uses the jquery in all wordpress themes and loads automatically. But it is always good to load the jquery library through google CDN. It saves your sites bandwidth. Many people already written about same so here I am just putting the method.

Load jQuery library from Google CDN

Load the jQuery library from Google CDN is always good for seo and better performance of site. WordPress itself uses the jquery in all wordpress themes and loads automatically.

For more libraries you can visit the following URL:
https://developers.google.com/speed/libraries/devguide

What you need is, just copy and paste the following code into your functions.php file.

function jquery_from_google_cdn() {
   if (!is_admin()) {
      wp_deregister_script('jquery');
      wp_register_script('jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js', false, '1.11.1');
      wp_enqueue_script('jquery');
   }
}
add_action('init', 'jquery_from_google_cdn');
Load jQuery library from Google CDN
Load jQuery library from Google CDN

Load jQuery in wordpress footer section will be best

Many latest framework are loading all javascript in footer. They reduce the page loading the errors. But wordpress load the jquery and other javascript in header.

loading javascript libraries in footer will be always best for every web or mobile application. Here in this article, I will show to Load jQuery in wordpress footer section.
But using following code you can load your javascripts and jquery in footer area. You just need to copy and paste the following code into the functions.php file.

Load jQuery in wordpress footer section

function wpai_add_jquery_in_footer( &$scripts) {
	if ( ! is_admin() )
		$scripts->add_data( 'jquery', 'group', 1 );
}
add_action( 'wp_default_scripts', 'wpai_add_jquery_in_footer' );

After doing the this you js scripts will load in footer. This js will load in footer for only normal users, Not for admin user.

Load jQuery in wordpress footer section will be best
Load jQuery in wordpress footer section will be best

 

Count post views without wordpress plugin

There are many wordpress plugin which will give you the post views using custom tables. But here using following code you can track the post views of your wp site.

Using external plugin, you can get the views and report but it will add more sql quries and extra load to your site. So I suggest use following code for getting the post views.

Count post views without wordpress plugin

Following code will track post views or count views of your wp site. You just need to copy and paste the following code into functions.php file first.


function getPostViews($postID){
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
        return "0 View";
    }
    return $count.' Views';
}
function setPostViews($postID) {
    $count_key = 'post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}
// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

After adding the above code for tracking the post views you need to add following code into single.php file. Note: add following code inside the wp post loop.

<?php
          setPostViews(get_the_ID());
?>

For showing the post views you need to put following code in single.php file. Note: add following code inside the wp post loop.

<?php
          echo getPostViews(get_the_ID());
?>
Count post views without wordpress plugin
Count post views without wordpress plugin

Ref taken from: http://wpsnipp.com/index.php/functions-php/track-post-views-without-a-plugin-using-post-meta/

Show the image attachments count in Post list section

Many people wants to see the number of attachments which are used for posts. In Post list page people wants to see the number of image and other attachments. Using following code you will be able to see the attachment count in post list section.

image attachments count in Post

You just need to copy and paste the following code into functions.php file.

add_filter('manage_posts_columns', 'posts_columns_attachment_count', 5);
add_action('manage_posts_custom_column', 'posts_custom_columns_attachment_count', 5, 2);
function posts_columns_attachment_count($defaults){
    $defaults['wps_post_attachments'] = __('Attached');
    return $defaults;
}
function posts_custom_columns_attachment_count($column_name, $id){
	if($column_name === 'wps_post_attachments'){
	$attachments = get_children(array('post_parent'=>$id));
	$count = count($attachments);
	if($count !=0){echo $count;}
    }
}
image attachments count in Post
image attachments count in Post

Ref Url: http://wpsnipp.com/index.php/functions-php/display-post-attachment-count-in-admin-column/

After WP registration send user to specific page

Many wordpress website users allow user to register on there site. After new user registration if we want to show them specific instructions or registration success page then you can use the following code. Put following code into functions.php file.

In this article I explained you about how can we redirect the user after WP registration and send user to specific page.

WP registration send user to specific page

Before adding the following code create your “registration-done” page.

function wpapi_registration_redirect(){
    return home_url( '/registration-done/' );
}
add_filter( 'registration_redirect', 'wpapi_registration_redirect' );

list all wordpress roles in selectbox for Admin secion

In your theme code backend or plugin code you can list your wp roles. You need to display wordpress users roles many times in theme configuration and plugins page. For fetching the wp users roles you can use the following code.

If you need to show User roles in theme configuration and plugins page. For list all wordpress roles in selectbox, you can use our code.

list all wordpress roles in selectbox

$roles_obj = new WP_Roles();
$roles_names_array = $roles_obj->get_names();
echo '<select name="role">';
foreach ($roles_names_array as $role_name) {
	echo '<option>'.$role_name.'</option>';
}
echo '</select>';

 

add custom background wordpress functionality support to theme

In new wordpress version we can add the background image or color to wordpress websites. Many older wp themes has no support for custom background functionality.

add custom background wordpress

You can very easily add the custom background support to your wordpress theme. You just need to copy following code and put in your functions.php file.

add custom background wordpress
add custom background wordpress

add_filter( 'body_class', 'wpapi_body_class' );
function wpapi_body_class( $classes ) {
	$background_color = get_background_color();
	$background_image = get_background_image();

	if ( empty( $background_image ) ) {
		if ( empty( $background_color ) )
			$classes[] = 'custom-background-empty';
		elseif ( in_array( $background_color, array( 'fff', 'ffffff' ) ) )
			$classes[] = 'custom-background-white';
	}

	return $classes;
}

// Activate custom background and set callback function
if ( function_exists( 'add_theme_support' ) ) {
    $defaults = array(
    'default-color' => '000000',
    'default-image' => get_template_directory_uri() . '/img/background.png',
    'wp-head-callback' => 'my_theme_background_cb',
    'admin-head-callback'    => '',
    'admin-preview-callback' => ''
    );
    add_theme_support( 'custom-background', $defaults );
}

wordpress login window moves if we put wrong username or password

When we try to use the wrong username or password in wp login windows. It started shaking the login window.
Some times it looks weird. wordpress login window moves if we put wrong username or password. Due to shaking effect user knows username and password is wrong. some hackers use this for hacking.

wordpress login window moves

Using following easy hack we cna remove this effect. You just need to copy and paste the following code into the functions.php which you will find in your active theme folder.

function wpapis_login_error() {
        remove_action('login_head', 'wp_shake_js', 12);
}
add_action('login_head', 'wpapis_login_error');
wordpress login window moves
wordpress login window moves

Add custom text to wordpress dashboard help section

WordPress is most popular cms these days and many companies are working on wp development. Many times they wanted to add or build custom help section for there clients. In wordpress admin section in the top-right corner. you are able to see the help drop down. If you click on help button then it will open the help section. You can modify or customize the help section as per your need.

You can modify or customize the help section as per your need. we have given code snippet to Add custom text to wordpress dashboard help section.

Add custom text to wordpress dashboard help section

You can just use the following code to customize the help section. You need to copy and paste the following code into functions.php file. which you will find in active wp theme directory.

Add custom text to wordpress dashboard help section
Add custom text to wordpress dashboard help section
//hook loading of new page and edit page screens
add_action('load-page-new.php','add_custom_help_page');
add_action('load-page.php','add_custom_help_page');

function add_custom_help_page() {
   //the contextual help filter
   add_filter('contextual_help','custom_page_help');
}

function custom_page_help($help) {
   //keep the existing help copy
   echo $help;
   //add some new copy
   echo "<h5>Custom Features</h5>";
   echo "<p>You can add your custom text over here. It will apprear in help section.</p>";
}

If you have any further doubts or questions then you can write to me.

Get back single-column dashboard in Latest WordPress

From WordPress 3.8 version came with new dashboard style. For Get back single-column dashboard in Latest WordPress, So you can use our code which can useful for every wordpress devloper.

Get back single-column dashboard in Latest WordPress

For getting back the single-column dashboard in WordPress 3.8, you need to add the following code in functions.php file.

// force one-column dashboard
function wpapi_single_screen_layout_columns($columns) {
$columns['dashboard'] = 1;
return $columns;
}
add_filter('screen_layout_columns', 'wpapi_single_screen_layout_columns');

function wpapi_single_screen_layout_dashboard() { return 1; }
add_filter('get_user_option_screen_layout_dashboard', 'wpapi_single_screen_layout_dashboard');
Get back single-column dashboard
Get back single-column dashboard