Saturday, May 21, 2016

WordPress get active plugin url

In WordPress to get the active plugin URL we can use the following code.

<?php $plugin_url = plugin_dir_url( $file ); ?>

$plugin_url will give you the current active plugin URL.

WordPress get current plugin directory path

While working with plugins sometimes we need the plugin directory path. WordPress provides a way to get the plugin current directory path.

use the following code which gives the plugin current directory path.

<?php $plugin_dir_path = dirname(__FILE__); ?>

$plugin_dir_path will give you the current plugin directory path.

WordPress get active theme directory

To get the full directory path of the active theme use the following piece of code.

<?php echo get_template_directory(); ?>

If you are using a child theme then it will return the path of the Parent theme only. To get the child theme directory path use the following code.

<?php echo get_stylesheet_directory(); ?>

This will give you the path of the child theme.

WordPress get active theme name

In Wordpress there can be multiple themes installed. But only one theme at a time can be active. If you want to get the name of the active theme in WordPress you can get it.

To get the active theme name use the following code is useful.

<?php
$active_theme_name = wp_get_theme();
?>

$active_theme_name will give you the active theme name.

WordPress get uploaded image path

To get the full path of an attachment use the following method.

<?php $path = get_attached_file( $attachment_id ); ?>


The above will give you full path of the file/attachment.

If you just want the filename and not path then use the following code.

<?php $filename_only = basename( get_attached_file( $attachment_id ) ); ?>

This will give you just the filename.

WordPress get Author info from post ID

If you want to get the Author information such as name, profile image and name from post ID then it can be retrieved in the following way.

<?php $author_id=$post->post_author; ?>

Now suppose you want to get the Author Avatar. The avatar is fetched in the following way.

<img src="<?php echo the_author_meta( 'avatar' , $author_id ); ?> " width="140" height="140" class="avatar" alt="<?php echo the_author_meta( 'display_name' , $author_id ); ?>" />

This will display the author avatar.

To display the author name use the following code.

<?php echo the_author_meta( 'user_nicename' , $author_id ); ?>


This will display the author name.

WordPress get current page number in Pagination

WordPress Supports pagination and has default functions which can be used to display pagination. For Example: In Blog page pagination is displayed by default. But sometimes you needs to add pagination to a custom template. In Some scenarios we needs to get the current page number in the pagination.

Current page number in Pagination can be retrived in the following way.

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
?>


Here $paged will contain the page number.

WordPress get current page url

To get the current active page URL in WordPress use the following function.

<?php
$current_page_url = add_query_arg( $wp->query_string, '', home_url( $wp->request ) );
?>


Now you can use $current_page_url any where you like as it contains the current page URL of the active page.

WordPress get current category id

To the the Category ID on Current active page in WordPress use the following code.

<?php
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
echo category_id;
?>


Your Category ID will be present in category_id.

Total Post Count in WordPress

To Display total post count on any page in WordPress the following code will be helpful.

<?php
$total_posts = wp_count_posts();
echo number_format($total_posts->publish);
?>


This will display total posts in WordPress in number format.

MS SQL : How to identify fragmentation in your indexes?

Almost all of us know what fragmentation in SQL indexes are and how it can affect the performance. For those who are new Index fragmentation...