Saturday, May 21, 2016

WordPress get ajax full url

In Wordpress we can get the full Ajax url as follows:

In PHP we can get it in the following way.

<?php echo admin_url('admin-ajax.php'); ?>

If you want to use it in Javascript then you can use it in following way.

<script type="text/javascript">
    var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
</script>


WordPress get user data

In WordPress there are many users with different roles. A user may have a administrator role, author or any other roles from the standard roles defined by WordPress.

If you want to find out user role or user information such as name,ID etc you can use the following function.

<?php
 $user_info = get_userdata(1);
          echo 'Username: ' . $user_info->user_login . "\n";
          echo 'User roles: ' . implode(', ', $user_info->roles) . "\n";
          echo 'User ID: ' . $user_info->ID . "\n";
?>

The above will give you user's username, role and ID.

WordPress get admin email address

In WordPress there is a administrator who manages pages, posts and every content of the site.

He can also manage and provide roles to other users. If you want to the get the email address of the site administrator you can use the following function.

<?php
$admin_email = bloginfo('admin_email');
?>

$admin_email will give you the email address of the site administrator.

WordPress get active menu item

In WordPress menu are dynamically managed. User can set Primary Header and footer menu from admin section.

If you want to get the current Menu item name you can get it by using the following function.

Open your active theme's functions.php and add the following function at the bottom of the file.

<?php
function Get_menu_item_name( $loc ) {
global $post;
$locs = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locs[$loc] );

if($menu) {
$items = wp_get_nav_menu_items($menu->term_id);

foreach ($items as $k => $v) {          
if ($items[$k]->object_id == $post->ID) {
$name = $items[$k]->title;
break;
}
}
}
return $name;
}
?>

The usage of the function is as follows:

<?php
$current_menu_name = Get_menu_item_name( 'primary' ); // here you enter another alias if your menu is not primary
echo $current_menu_name;
?>

$current_menu_name will contain the value of the current menu name.

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.

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...