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”
Web Development & WordPress

Install Free SSL to your domain in Hostinger VPS using terminal and SSH

Basically my client’s ssl was expired, he was using hostinger vps and i have helped him to install a new free ssl to his domain ( hosted in vps) using ssh and terminal.
Here are the steps. Just use the codes i shared step by step.

At First login to SSh

Continue reading “Install Free SSL to your domain in Hostinger VPS using terminal and SSH”