• Blog Reading Progress

  • Make a PHP Package & Publish On Packagist

    I was curious how composer get those packages form online.
    When we run a command in cmd where it goes and get that package !!
    Can I make a package for me?

    Let’s start with very basic

    Create a composer.json file and paste this code modify according to your package name

    You can also run composer init command.

    {
        "name": "ifat/bengalina",
        "type": "library",
        "description": "A package for nerd php programmer",
        "keywords": ["test","practice"],
        "homepage": "https://github.com/iftakharul-islam/bengalina",
        "license": "MIT",
        "authors": [
            {
                "name": "Iftakharul Islam",
                "email": "iftakharulislam881@gmail.com",
                "homepage": "https://ifatwp.wordpress.com",
                "role": "Developer"
            }
        ],
        "require": {
            "php": ">=7.4"
        },
        "minimum-stability": "dev"
    }

    your other php file will be there we’re not using any autoloader or extra configuration. our goal is to move simply create a package.

    Don’t forget to run composer validate.
    It will check the composer.json file data is correct or not.

    Now make a github repository and push this code.

    Go to packagist create an account. You can make it using github but preference is your.

    Navigate to to submit package page.

    Paste github repository url here then submit

    We got our package live.

    It will better if you attach a Readme.md file with it user can simply grab the detail of your package documentation from the bottom here a simple example below.
    There is some markdown editor online.

    Here is your packages data and it insights

    We need to setup automatic update for our package from github.

    Go to github repo which is connected with this package add a webhook

    Copy this api token for github repo.

    Paste this link https://packagist.org/api/github 

    Setup webhook and set the event trigger Just the push event.

    We set our package for automatic updates now whenever we release a new version from github it will be automatically updated.

    Well that’s it . no pain you published a PHP package!!

  • Add PHPCS and WPCS in Windows Auto configure using comoposer | Add to project level

    Setup CLI

    Install PHPCS via Composer

    composer global require "squizlabs/php_codesniffer=*"
    The global keyword will add the package into global package directory “Composer\vendor\”

    Install WPCS via Composer

    composer global require "wp-coding-standards/wpcs"

    Add WPCS to PHPCS

    This command paths can be modified if you’re using custom directory to install PHPCS

    phpcs --config-set installed_paths %APPDATA%\Composer\vendor\wp-coding-standards\wpcs

    Check it!

    phpcs -i

    Use in project level

    Create composer.json file and run composer install command

    {
      "require-dev": {
        "dealerdirect/phpcodesniffer-composer-installer": "*",
        "wp-coding-standards/wpcs": "*",
        "phpcompatibility/phpcompatibility-wp": "*"
      }
    }
    

    Setup Editor

    VSCode

    • If you haven’t installed phpcs (PHP CodeSniffer for Visual Studio Code) by Ioannis Kappas, please install it.
    • You can do it by pressing Ctrl Shift X and search for phpcs and then press Install.
  • Let’s Make a New Product By Rest API

    Create a REST API (No code)

    Go to WooCommerce settings click Advance tab then click Add key button

    Enter the fields data and Generate API key

    Congrats We’ve Generated!!

    We can see the description and other stuff appeared ok let’s move to main part.

    To communicate with the WooCommerce REST API we will use WooCommerce API Client

    Install the WooCommerce API client for PHP using Composer:

    
    
    
    
    
    {
        "require": {
            "automattic/woocommerce": "^3.1"
        }
    }

    or run the command

    composer require automattic/woocommerce

    require __DIR__ . '/vendor/autoload.php';
    
    use Automattic\WooCommerce\Client;
    
    $woocommerce = new Client(
        'https://yourstore.com',
        'consumer_key',
        'consumer_secret',
        [
            'wp_api' => true,
            'version' => 'wc/v3',
            'verify_ssl' => false, // Only use this if your server doesn't have a valid SSL certificate
        ]
    );
    

    create a php file create-product.php and replace the details

    https://yourstore.com’,
    ‘consumer_key’,
    consumer_secret’,

    Setup data to create products.

    $data = [
        'name' => 'My Product', // Product name
        'type' => 'simple', // Product type (simple, grouped, external, variable)
        'regular_price' => '9.99', // Regular price
        'description' => 'Product description', // Product description
        'short_description' => 'Short description', // Short description
        'categories' => [
            [
                'id' => 1 // Category ID
            ],
        ],
        'images' => [
            [
                'src' => 'https://1.bp.blogspot.com/-46qbH_DIDIY/XWu-zjswsDI/AAAAAAAAC0o/ADi0VEv7N00M6XRyPp_8Umk5nPnxQkoqgCLcBGAs/s1600/gambar_kata.jpg',
            ],
        ],
    ];
    
    $product = $woocommerce->post('products', $data);
    
    var_dump($product);

    My Product Created Successfully !!

  • Fix Deprecated: Creation of dynamic property Class::$attr is deprecated

    <?php

    class Ifat {

         function __construct(){

            $this->name = “ifat”;

            echo $this->name;

         }

    }

    new Ifat(); // Deprecated: Creation of dynamic property Ifat::$name is deprecated in patha/tempCodeRunnerFile.php on line 4

    class FixedIfat {

        private $name;

        function __construct(){

           $this->name = “ifat”;

           echo $this->name;

        }

    }

    new FixedIfat();

    In php 8.2 dynamic attributes assignment is deprecated
    Just define the attributes before using
    Cheers !!

  • Regex Character Class

    1. Matching digits:
    javascriptCopy codeconst str = 'There are 123 apples in the basket';
    const regex = /\d+/;
    
    console.log(str.match(regex)); // Output: ['123']
    
    1. Matching non-digits:
    javascriptCopy codeconst str = 'My phone number is (555) 123-4567';
    const regex = /\D+/;
    
    console.log(str.match(regex)); // Output: ['My phone number is (', ') ', '-', '']
    
    1. Matching whitespace:
    pythonCopy codeconst str = 'The quick brown fox jumps over the lazy dog';
    const regex = /\s+/;
    
    console.log(str.match(regex)); // Output: [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']
    
    1. Matching non-whitespace:
    javascriptCopy codeconst str = 'The_quick_brown_fox_jumps_over_the_lazy_dog';
    const regex = /\S+/;
    
    console.log(str.match(regex)); // Output: ['The_quick_brown_fox_jumps_over_the_lazy_dog']
    
    1. Matching alphanumeric characters:
    pythonCopy codeconst str = 'I have 3 cats and 2 dogs';
    const regex = /\w+/;
    
    console.log(str.match(regex)); // Output: ['I', 'have', '3', 'cats', 'and', '2', 'dogs']
    
    1. Matching non-alphanumeric characters:
    pythonCopy codeconst str = 'Hello! How are you?';
    const regex = /\W+/;
    
    console.log(str.match(regex)); // Output: ['!', ' ', ' ', '?']
    

    In the first example, the regular expression \d+ matches one or more digits in the string, which is 123.

    In the second example, the regular expression \D+ matches one or more non-digits in the string, which includes parentheses, spaces, and hyphens.

    In the third example, the regular expression \s+ matches one or more whitespace characters in the string, which includes spaces.

    In the fourth example, the regular expression \S+ matches one or more non-whitespace characters in the string, which includes underscores.

    In the fifth example, the regular expression \w+ matches one or more alphanumeric characters in the string, which includes letters and digits.

    In the sixth example, the regular expression \W+ matches one or more non-alphanumeric characters in the string, which includes punctuation marks and spaces.

  • Basic Regex

    1. Matching a single character:
    javascriptCopy codeconst str = 'hello world!';
    const regex = /\w/;
    
    console.log(str.match(regex)); // Output: ['h']
    
    1. Matching a specific character:
    javascriptCopy codeconst str = 'hello world!';
    const regex = /o/;
    
    console.log(str.match(regex)); // Output: ['o']
    
    1. Matching a range of characters:
    javascriptCopy codeconst str = 'hello world!';
    const regex = /[a-z]/;
    
    console.log(str.match(regex)); // Output: ['h']
    
    1. Matching any character:
    javascriptCopy codeconst str = 'hello world!';
    const regex = /.*/;
    
    console.log(str.match(regex)); // Output: ['hello world!']
    

    In the first example, the regular expression \w matches the first alphanumeric character in the string, which is h.

    In the second example, the regular expression o matches the first occurrence of the character o in the string.

    In the third example, the regular expression [a-z] matches the first lowercase letter in the string, which is h.

    In the fourth example, the regular expression .* matches any character sequence in the string, from the beginning to the end of the string.

  • Regex Topics and short details

    1. Basic patterns:
    • Matching a single character: /\w/
    • Matching a specific character: /a/
    • Matching a range of characters: /[a-z]/
    • Matching any character: /.*/
    1. Character classes:
    • Matching digits: /\d/
    • Matching non-digits: /\D/
    • Matching whitespace: /\s/
    • Matching non-whitespace: /\S/
    • Matching alphanumeric characters: /\w/
    • Matching non-alphanumeric characters: /\W/
    1. Quantifiers:
    • Matching zero or more: /*/
    • Matching one or more: /+/
    • Matching zero or one: /a?/
    • Matching a specific number of times: /a{3}/
    • Matching a range of times: /a{2,4}/
    1. Anchors:
    • Matching the beginning of a string: /^hello/
    • Matching the end of a string: /world$/
    • Matching the boundary between word and non-word characters: /\bhello\b/
    • Matching the boundary between non-word and word characters: /\Bhello\B/
    1. Groups:
    • Capturing a group: /(hello)/
    • Non-capturing a group: /(?:hello)/
    • Positive lookahead: /hello(?=world)/
    • Negative lookahead: /hello(?!world)/
    • Backreferences: /(hello) \1/
    1. Alternation:
    • Matching multiple alternatives: /hello|world/
    • Using the | symbol to match multiple patterns: /hello|world/
    1. Flags:
    • Case-insensitive matching: /hello/i
    • Global matching: /hello/g
    • Multiline matching: /hello/m
  • How to make next and previous in wordpress blog page.

    Making a next previous section for blog is very interesting feature it’s tells your reader what content you’re providing next page if user can see the title or the thumbnail of the next blog it brings more trust as reader perspective here is the code sample you can modify the design and code as your own.

    Inside your currently active wordpress theme open the single.php file in code editor that you’re using:

    There will be a block called have_post() loop on the next line of the loop past the code given below.

     <div class="container">
            <?php
            $prev_post = get_previous_post();
            if ($prev_post):
                $prev_title = strip_tags(str_replace('"', '', $prev_post->post_title));
                $prev_thumb = get_the_post_thumbnail($prev_post->ID, 'thumbnail');
                ?>
                <a href="<?php echo get_permalink($prev_post->ID); ?>" class="prev-post">
                    <?php if ($prev_thumb): ?>
                        <div class="prev-thumb">
                            <?php echo $prev_thumb; ?>
                        </div>
                    <?php endif; ?>
                    <div class="prev-title">
                        <?php echo $prev_title; ?>
                    </div>
                    <div class="prev-arrow"><i class="fa fa-angle-left"></i></div>
                </a>
            <?php endif; ?>
    
            <?php
            $next_post = get_next_post();
            if ($next_post):
                $next_title = strip_tags(str_replace('"', '', $next_post->post_title));
                $next_thumb = get_the_post_thumbnail($next_post->ID, 'thumbnail');
                ?>
                <a href="<?php echo get_permalink($next_post->ID); ?>" class="next-post">
                    <?php if ($next_thumb): ?>
                        <div class="next-thumb">
                            <?php echo $next_thumb; ?>
                        </div>
                    <?php endif; ?>
                    <div class="next-title">
                        <?php echo $next_title; ?>
                    </div>
                    <div class="next-arrow"><i class="fa fa-angle-right"></i></div>
                </a>
            <?php endif; ?>
        </div>

    Open style.css paste this css code on that file

     /* Next previous style */
      .prev-post, .next-post {
        display: flex;
        align-items: center;
        margin: 20px 0;
        padding: 20px;
        background-color: #f8f8f8;
        border: 1px solid #e5e5e5;
        border-radius: 5px;
      }
      
      .prev-post:hover, .next-post:hover {
        background-color: #e5e5e5;
      }
      
      .prev-thumb, .next-thumb {
        width: 80px;
        height: 80px;
        margin-right: 20px;
        overflow: hidden;
        border-radius: 5px;
      }
      
      .prev-thumb img, .next-thumb img {
        width: 100%;
        height: auto;
      }
      
      .prev-title, .next-title {
        font-size: 1.2em;
        font-weight: bold;
        color: #333;
        margin-bottom: 5px;
      }
      
      .prev-arrow, .next-arrow {
        font-size: 1.5em;
        color: #333;
        margin-left: auto;
      }
      
      .prev-arrow i, .next-arrow i {
        transform: translateY(-2px);
      }
      /* Next prevous css end */

    Easy right now check the navigation of the code in in single blog page.

  • Month sorting problem in php

    <?php
     $all_months = [
        "jan","feb","mar","apr","may","jun","july","aug","sep","oct","nov","dec"
     ];
     $sortable_months = [
        "apr"=>70,
        "jun"=>45,
        "aug"=>34,
        "jan"=>34
     ];
    
    $sortable_months = array_keys($sortable_months);
    // var_dump($sortable_months); uncomment to see the value
    $new_month = [];
    foreach($all_months as $month){
       if(in_array($month,$sortable_months)){
          array_push($new_month, $month);   
       }
    }
    
    print_r($newMonth);
Design a site like this with WordPress.com
Get started