Monday, May 16, 2016

Wordpress get current logged in user information

In WordPress we can get the logged in user information such as firstname, lastname, displayname and ID in the following way.

<?php
$current_logged_user = wp_get_current_user();
?>


Now you have all the user information in the $current_logged_user variable. You can access the information in the following way.

<?php
echo 'Username: ' . $current_logged_user->user_login . '<br />';
    echo 'User email: ' . $current_logged_user->user_email . '<br />';
    echo 'User first name: ' . $current_logged_user->user_firstname . '<br />';
    echo 'User last name: ' . $current_logged_user->user_lastname . '<br />';
    echo 'User display name: ' . $current_logged_user->display_name . '<br />';
    echo 'User ID: ' . $current_logged_user->ID . '<br />';
?>

Get Logged In user ID in WordPress

To get the currently logged in user ID in WordPress use the following code.

<?php
$current_user_id = get_current_user_id();

echo $current_user_id;
?>


$current_user_id will give you the ID of the currently logged in user.

Redirect to custom page after comment in WordPress

In WordPress when a user comments on an Article, post or page he is redirected to the same page after commenting.

But in some scenarios it is required that user should be redirected to some other URL after commenting. This can be done with a simple piece of code.

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

function wp_comment_redirect_url($location) {    
        return "http://www.yourdomain.com";
    }

add_filter('comment_post_redirect', 'wp_comment_redirect_url');


Now user will be redirected to the URL to have set.

Disable Profile and Group Cover Image in BuddyPress

In BuddyPress most of the times Group and Profile cover images are not required. We can disable Profile and Group Cover images in the following way.

Open your theme's function.php and at the end of the file before ?> add the following 2 lines.

add_filter( 'bp_is_profile_cover_image_active', '__return_false' );
add_filter( 'bp_is_groups_cover_image_active', '__return_false' );


After adding the above two lines, save the file and upload the file back to the server.

Now, Profile and Group Cover image will be disabled and will not be visible in the front-end.

Update Wordpress without FTP

Sometimes when WordPress site is updated ftp details are asked while updating. We can surpass the FTP authentication and directly update WordPress, Plugins and Themes to latest version.
To do so open your wp-config.php file (file is present on the root of wordpress installation).

After opening the file in an editor find the following line

require_once(ABSPATH . 'wp-settings.php');


After you find the above line in the wp-config.php file place the following line below it.

define('FS_METHOD','direct');


So, your final file should look like below.

require_once(ABSPATH . 'wp-settings.php');
define('FS_METHOD','direct');


After this save you wp-config.php file and upload back to server. Now WordPress, Plugins, Themes can be updated to their latest version without the need to enter FTP details.

Disable lost password email in WordPress

In WordPress users can recover their passwords through the lost password feature available. But some times this options is used by users to spam your website by using the functionality which may result your domain to be blacklisted in Google or your hosting provider may suspend your domain for mass emailing.

 To Avoid this recover password functionality can be disabled. You add the following lines in your function.php file to stop any email from sending from your website.

  function wp_password_change_notification() 
   { 
      return false; 
   }

add_filter('allow_password_reset', 'wp_password_change_notification');

This will disable lost password email in WordPress

Thursday, December 24, 2015

Wordpress get permalink of post from ID

Permalink of a page or post using ID can be retrieved as follows:

$url = get_permalink($post_id);

Where $post_id  is the ID of the post or page whose permalink is to be get.

Wordpress get acf field value of another post

To get values of ACF field from another page or post use the following method.

$value = get_field('field_name',$post_id);

Here $post_id  is of another post or page whose ACF value you want to fetch in the current page or post.

Wednesday, December 23, 2015

Wordpress site url

To get the website URL use the following inbuilt function of WordPress.

$site_url  = site_url();

You will value of site URL in the above variable and can be used as following:

<?php echo $site_url; ?>

Wordpress home url

As we know WordPress is a very powerful blogging open source CMS, so is development easy and flexible.

To get the home page URL use the following function.

$home_url = home_url();

<?php echo $home_url; ?>

You will get the home URL in the above variable.

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