How to get user meta information in WordPress

Hello Friends, after a long break, I am back once again. Today I am going to share with you a very simple but useful small code for getting all logged in user's meta information. Although, this tutorial is already written on WordPress official site and many other tutorial/blog websites so that I thought, I must share this on my blog too. As we know, while developing a WordPress website sometimes we need to get all user meta information, for example, user first name, last name, etc. In this case, this small code will work for you like oxygen. So without taking too much time, let's see this:

$user_meta = get_user_meta(get_current_user_id());
print_r($user_meta);

Above example will return all meta tags tags of logged in user and you can choose which one you want to display on your page. But if you want to get only one meta tag then you should use below code.

$user_last = get_user_meta( get_current_user_id(), 'last_name', true );

By using above code you will get logged in user's last name saved in database. In given example get_current_user_id() function will return current logged in user id.


Thank you for reading my post. Good Luck.

Alternate odd even table row in jQuery

We can add alternate odd even table row in jQuery, by using CSS3 pseudo element classes and some lines of jQuery codes. That will give web designers the ability to style the rows and make it attractive as we see on other website over the internet. Although, We can also add odd even classes in table rows by using php. But  in this case, we have add conditions in our code. I think jQuery is the best way to do that.

So, here is the code:
First of all create a table as below.

<table class="tg">
  <tr>
    <td>B.Tech (CSE)</td>
    <td>4 years</td>
  </tr>
  <tr>
    <td>B.Tech (IT)</td>
    <td>4 years</td>
  </tr>
  <tr>
    <td>B.Tech CSE(Cloud tech. & Inf.Security)</td>
    <td>4 years</td>
  </tr>
  <tr>
    <td>B.Tech (ECE)</td>
    <td>4 years</td>
  </tr>
  <tr>
    <td>B.Tech (E&E)</td>
    <td>4 years</td>
  </tr>
  <tr>
    <td>B.Tech (ME)</td>
    <td>4 years</td>
  </tr>
</table>
and then add some some css classes to make page look good.

.tg{border-collapse:collapse;border-spacing:0; width:100%;}
td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tr-even{ background:#000; color:#fff;}
.tr-odd{ background:#fff; color:#000;}
Now add jQuery code to see the the magic.

$(document).ready(function() {
$("table tr:nth-child(odd)").addClass("tr-even");
  $("table tr:nth-child(even)").addClass("tr-odd");
  });

Here is the complete code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<style>
.tg{border-collapse:collapse;border-spacing:0; width:100%;}
td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;}
.tr-even{ background:#000; color:#fff;}
.tr-odd{ background:#fff; color:#000;}
</style>
<script type="text/javascript">
$(document).ready(function() {
$("table tr:nth-child(odd)").addClass("tr-even");
  $("table tr:nth-child(even)").addClass("tr-odd");
  });
</script>
<table class="tg">
  <tr>
    <td>B.Tech (CSE)</td>
    <td>4 years</td>
  </tr>
  <tr>
    <td>B.Tech (IT)</td>
    <td>4 years</td>
  </tr>
  <tr>
    <td>B.Tech CSE(Cloud tech. & Inf.Security)</td>
    <td>4 years</td>
  </tr>
  <tr>
    <td>B.Tech (ECE)</td>
    <td>4 years</td>
  </tr>
  <tr>
    <td>B.Tech (E&E)</td>
    <td>4 years</td>
  </tr>
  <tr>
    <td>B.Tech (ME)</td>
    <td>4 years</td>
  </tr>
</table>

Remove elements from array using PHP

Hello friends, today i am going to tell you about how to remove elements from array using PHP. Although, this is not a complete tutorial but, is very tricky. To make this happen you will have use PHP unset() method and some more functions are available in PHP that can also help you. So, lets start here.

Deleting array element:
Using unset() method:
If you want to delete single array element then you will use unset() method. You can also delete multiple elements, by using unset method.
For example, i have an array $arr and i want to remove second element then i will write my code as below.
$arr = array(0 => "a", 1 => "b", 2 => "c");
unset($arr[1]); // array key 1 is second element.
and then output will be something like:
Array(
[0]=>a
[2]=>b
)
Note that unset method does not change array index.
Using array_splice() method:
If you are using array_splice() method the array keys will be re-indexed automatically. This is the major deference between array_splice and unset methods.
An example:
$arr = array(0 => "a", 1 => "b", 2 => "c");
array_splice($arr, 1, 1);
Output will be:

Array(
[0]=>a
[2]=>b
)

Simple ahref mailto syntax in html

The HTML mailto protocol allows you to make a hyperlink that will open your default email software and enables you to send messages to a specific address. For example, Outlook, but you have not installed it then if you are using chrome browser and you are logged into your Gmail account, then it will open a new message box where you type your message and send it to recipients.

Examples:

Simple mailto syntax: Sometimes, we don't have enough
<a href="mailto:akhilesh@example.com">Email Me</a>
Mailto syntax with subject: Addding 'Hi akhilesh' message as a subject.
<a href="mailto:akhilesh@example.com?subject=hi%20akhilesh">Email Me</a>
Mailto syntax with subject and body: If you want to add some predefined body content and subject then use below code. Here in this example, i am adding 'Hi akhilesh' as a subject and 'How are you' as a body content.
<a href="mailto:akhilesh@example.com?subject=hi%20akhilesh&body=How%20are%20you?">Email Me</a>
Mailto syntax with Cc and Bcc: By using below code you will be able to add cc and bcc recipients.
<a href=mailto:akhilesh@example.com?cc=john@example.com&bcc=hi@example.com>Email Me</a>
Mailto syntax with multiple addresses: If you required to send email to multiple recipient then you will add recipient email ids with comma separated. You can add any number of email ids there is no restrictions. You can also add subject content body attributes if you want.
<ahref="mailto:akhilesh@example.com,john@example.com,hi@example.com">Email Me</a>

I hope this small basic tool will help you. Thanks for reading my post.

How to assign a group to user programmatically in magento

Sometimes, we need to create an observer to assign a group to user programatically. Because there is no rule available in Magento to change user group as we set the rules for shopping cart and catalog. Although, an option is available in Magento System->Configuration->Customers->Customer Configuration-> Create New Account Option. But if you set a group then all new users will be assigned to it. And in some cases, we need to assign it programmatically.

So here is the simple three lines of code:

$customer = $observer->getEvent()->getCustomer();
$customer->setData('group_id', 3); // or whatever the group id you want to assign
$customer->save();

Hope this small tutorial will save your time. Thanks for reading my post.

Remove '?m=1' parameter from page url in .htaccess

While moving a website from blogger to Wordpress, we face this issue because blogger adds an extra parameter in url in a mobile version. And in WordPress, it does not change or add any extra parameters in URL. In this situation, we need to redirect this link to the original link.
To do this task, we need to add below code in our. htaccess file.

RewriteCond %{QUERY_STRING} ^m=1$
RewriteRule ^(.*)$ /$1? [R=301,L]

I hope that above link will work for you. Please feel free to write your opinion in comment box below. Thanks for reading my blog.

JQuery expand and hide table tr td

Expanding and hiding table tr td in jQuery is not very tough job. You can make this happen with just a some lines of jQuery codes. Here in this tutorial, I am going to create a simple accordion using table tr td.

First of all, we will create a index.html file and put below code.

   <table>
  <tr>
    <td><a class="more" href="#"><h1>What is Lorem Ipsum?</h1></a>
    </td>
  </tr>
  <tr>
    <td colspan="10"><p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p></td>
  </tr>
  <tr>
    <td><a class="more" href="#"><h1>Where does it come from?</h1></a>
    </td>
  </tr>
  <tr>
    <td colspan="10"><p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p></td>
  </tr>
</table>

And then add jQuery function like:

<script type="text/javascript">
$('.more').on('click', function(event) {
  $(this).closest('tr').next().toggle();
});
</script>

Now run this file in your favorite browser. You will see the output as below, if everything is correct.

And finally, here is the complete code:

<html>
  <head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
</head>
 <body>
  <div>
   <table>
  <tr>
    <td><a class="more" href="#"><h1>What is Lorem Ipsum?</h1></a>
    </td>
  </tr>
  <tr>
    <td colspan="10"><p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p></td>
  </tr>
  <tr>
    <td><a class="more" href="#"><h1>Where does it come from?</h1></a>
    </td>
  </tr>
  <tr>
    <td colspan="10"><p>
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
</p></td>
  </tr>
</table>
  </div>  
<script type="text/javascript">
$('.more').on('click', function(event) {
  $(this).closest('tr').next().toggle();
});
</script>
 </body>
 </html>

Does it work for you? Please do let me know, in the comment box below. Thanks.

How to add or remove class dynamically in jQuery

jQuery contains two classes addClass() and removeClass(). By using these classes we can add or remove classes dynamically. addClass() function used to add class and removeClass() function used to remove previously added classes.

For example if i have div tag (id=hiid) and i want to add 'activeclass' class then i will add addClass function as below:

$(#hiid’).addClass(‘activeclass’);

And if i need to remove above added class then i will write script as below:

$(#hiid’).removeClass(‘activeclass’);

Here is the complete example:

<html>
  <head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script>
<style>
.activeclass{background:#000;color:#fff;}
</style>
 </head>
 <body>
  <div>
   <div id="hiid">This is div tag.</div>
<input type="button" name="addclass" id="addclass" value="Add Class" />
<input type="button" name="removeclass" id="removeclass" value="Remove Class" />
  </div>  
<script type="text/javascript">
$("#addclass").click(function () {
  
   $('#hiid').addClass('activeclass');
   
});
$("#removeclass").click(function () {
  
   $('#hiid').removeClass('activeclass');
   
    });
</script>
 </body>
 </html>