Web Development & WordPress

Check if it is mobile then show something : wordpress template php code

For coders sometimes it is needed to show something only for mobile in template fiile. So here is the coding

for functions.php file

function is_mobile_device() {
    $user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
    $mobile_agents = ['Android', 'iPhone', 'iPad', 'iPod', 'Windows Phone', 'BlackBerry'];
    
    foreach ($mobile_agents as $agent) {
        if (stripos($user_agent, $agent) !== false) {
            return true;
        }
    }
    return false;
}

in template file ( like single.php, page.php etc)

 <?php
 
  $is_mobileb = wp_is_mobile() || is_mobile_device();
  if( $is_mobileb){ 
    
?> 
YOUR CODE WILL GO HERE 

<?php } ?>
Web Development & WordPress

WordPress Shortcode to Show Different Images for Desktop & Mobile

Basically client was asking to show different image in desktop view and mobile view. So the idea which hits in my mind to create a shortcode which will check if client is visiting the site from mobile or from desktop/laptop and return the image based on it

So the shortcode need to be used in the following way, put it in the img html tag where u want to show the different image

Continue reading “WordPress Shortcode to Show Different Images for Desktop & Mobile”
Web Development & WordPress

Shows number of Word Count at the bottom of page or post in wordpress

Without using any plugins if u want to show the number of words used in a page or post at the bottom, you can use the following code in function.php

function  count_words( $content ) {
    $stripped_content = strip_tags( $content );
    $wordnum = str_word_count( $stripped_content );
    $label = __( 'Number of Words', 'wordcount' );
    $content .= sprintf( '<h2>%s:%s</h2>', $label, $wordnum );
    return $content;
}
add_filter( 'the_content', 'count_words' );

Here is the result

Continue reading “Shows number of Word Count at the bottom of page or post in wordpress”
Web Development & WordPress

Macbook air m1: connect to github using terminal

The whole procedure i took help of Claude AI.

First, create a new Personal Access Token:

  • Go to GitHub.com and login
  • Click your profile picture → Settings
  • Scroll down to “Developer settings” (bottom of left sidebar)
  • Click “Personal access tokens” → “Fine-grained tokens”
  • Click “Generate new token”
  • Set a token name: “MacBook Git Token”
  • Set expiration: 90 days (or as you prefer)
  • Under “Repository access”, select “All repositories”
  • Under “Permissions”, expand “Repository permissions” and select:
    • “Contents” → “Read and write”
  • Click “Generate token”
  • COPY THE NEW TOKEN AND SAVE IT SOMEWHERE SAFE
Continue reading “Macbook air m1: connect to github using terminal”
Life Thoughts & Personal Notes

How to remain Calm when someone Praise or Criticise you

When others honor a seeker, they should not attribute it to their own deeds, virtues, or goodness. Instead, they should recognize that the praise reflects the courtesy and generosity of the one offering it. Considering another’s politeness as one’s own merit is not an act of honesty.

Conversely, if someone insults them, they should accept it as a consequence of their own actions, acknowledging that the fault lies within themselves. The insulter bears no blame; rather, they are deserving of compassion, as they unwittingly provide the seeker with an opportunity for purification by enduring the repercussions of the seeker’s misdeeds.

By adopting this mindset, a seeker can remain calm and indifferent to both honor and insult. However, if they perceive honor as a result of their own virtues and insult as another’s fault, they will struggle to maintain tranquility in the face of praise or criticism.

Collected from Gita Press Gorakhpur book Gita Sadhak Sanjivani (English)