Change a Admin Color Scheme for All Users in WordPress

If you want to change user color scheme for all users on your site and change user experience. You can change default admin color scheme without any plugin. You need need to add small code snippet into your theme’s functions.php file. You can find this file in your theme folder.

Open functions.php file and put following code in that file.


function set_default_admin_color($user_id) {
	$args = array(
		'ID' => $user_id,
		'admin_color' => 'sunrise'
	);
	wp_update_user( $args );
}
add_action('user_register', 'set_default_admin_color');

There are many wordpress admin related methods and code snippet written in my site. simply use search functionality for more code snippets or check wordpress tutorials section.

 

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 );
}

how to inject html code into wordpress head

Question, how to inject html code into wordpress head section If you don’t want plugin then use code snippet for inject the code into wordpress head section.

how to inject html code into wordpress head

Many wp developers asked me same question multiple times. How to push the some HTML code into the wordpress head section. It is quite easy to push or inject the the some HTML code into the head section.There are multiple WP plugins which are giving this facility the push the HTML code into the head section. If you don’t want to use the WP plugin then you can use my code snippet for inject the code into wordpress head section.

If you are WP theme developer then you can use following code.

 function WPAPI_init() {
echo "YOUR_HTML_GOES_HERE";
if (is_admin()) {
echo "YOUR_HTML_GOES_HERE";
}
 }
 add_action('wp_head', 'WPAPI_init');

You need to put above code into functions.php file which you find in themes folder.

If you are WP plugin developer then you can put above code into your plugin file.

how to display your posts words count in wordpress

WordPress tutorial, how to display your posts words count in wordpress. Here in this article we given code for adding in your theme functions.php file.

how to display your posts words count in wordpress

 

how to display your posts words count in wordpress
how to display your posts words count in wordpress

If you want to display the words count in your post. you should use the following code in functions.php file.

If you are not wordpress developer then dont use this code in your theme files.

function wcount(){
 ob_start();
 the_content();
 $content = ob_get_clean();
 return sizeof(explode(" ", $content));
}

 

In single.php file put following code for showing the word count.


echo wcount();