Tuesday, March 19, 2024

Install LAMP stack on Linux(Ubuntu OS, Zorin OS etc) for local development environment

Step 1:
---------------------
Install apache2 by below command:

sudo apt install apache2

Step 2:
---------------------
Install PHP 7.4:
First, need to enable/add a package for php 7.4 by below command:

sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php7.4 php7.4-cli php7.4-common php7.4-json php7.4-opcache php7.4-mysql php7.4-mbstring php7.4-curl php7.4-xml

By the below command, you can change PHP version if you have multiple versions of PHP:

sudo update-alternatives --set php /usr/bin/php7.4

Restart apache server:

sudo systemctl restart apache2

Step 3:
---------------------
Install composer by below command:

sudo apt install composer

Step 4:
---------------------
Apache document root folder is: /var/www/html
No write permission to this html folder.
Run below command to change ownership of this html folder:

sudo chown -R $USER:$USER /var/www/html

Step 5:
---------------------
Add user to apache server by below command:

sudo nano /etc/apache2/apache2.conf

Need to change these below two lines with your linux user name:
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}

To

User $USER
Group $USER

Save and restart apache service:

sudo service apache2 restart

Step 6:
---------------------
Install Mysql Database by below command:

sudo apt install mysql-server

Now secure Mysql by below command:

sudo mysql_secure_installation

N, Y, Y, Y, Y

Add Mysql root password permission:

sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'your password';
FLUSH PRIVILEGES;
exit;

Step 7:
---------------------
Install phpmyadmin
Go to phpmyadmin.net and download zip to /var/www/html folder 
and unzip it and rename to phpmyadmin.
Then access phpmyadmin with this URL: http://localhost/phpmyadmin

You will get phpmyadmin login page.

Optional:
=======
Few important commands to switch PHP version:

sudo update-alternatives --config php
sudo a2dismod php8.1
sudo a2enmod php7.4
sudo service apache2 restart

Sometimes you may face 404 not found in all inner pages of your websites 
then follow below step to solve it:
Open /etc/apache2/apache2.conf by below command:

sudo gedit /etc/apache2/apache2.conf

then edit below line specially AllowOverride none to AllowOverride All:

<Directory /var/www/html>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

Then save and restart apache.

.:The End:.

Sunday, February 25, 2024

Screen unblank in Zorin OS

If you press Super + L then Zorin OS goes to the lock screen and turns off your display.

But if you want to stay displayed ON, on your lock screen then you need to install extension manager first and then you need to install the below extension:

https://extensions.gnome.org/extension/1414/unblank/

With the above extension, you can keep the screen turned ON on your Zorin OS.

Friday, November 10, 2023

WP_LIST_TABLE - complete code for searching, listing, pagination

WP_LIST_TABLE - complete code for searching, listing, pagination:

1. First register a menu to maintain a view:

<?php
require_once 'class-people-list-table.php';

add_action( 'admin_menu', 'ashique_register_custom_menu_page' );
function ashique_register_custom_menu_page()
{
    $parent_slug = 'ashique-slug';
    $capability = 'manage_options';

    add_menu_page(
        'Title',
        'Menu Title',
        $capability,
        $parent_slug,
        'ashique_dashbard_callback',
        'dashicons-dashboard',
        6
    );

    add_submenu_page(
        $parent_slug,
        'Dashboard',
        'Dashboard',
        $capability,
        $parent_slug,
        'ashique_dashbard_callback',
    );

    add_submenu_page(
        $parent_slug,
        'People List',
        'People List',
        $capability,
        'ashique-people-list',
        'ashique_people_list_callback',
    );
}

2. Now maintain list callback function:

function ashique_people_list_callback()
{
    // Creating an instance of People_List_Table
    $people_list_table = new People_List_Table();
    $people_list_table->prepare_items();

    ?>
    <div class="wrap">
        <h2>People List Table</h2>

        <form method="get">
            <input type="hidden" name="page" value="<?php echo $_REQUEST['page']; ?>" />
            <?php $people_list_table->search_box('Search', 'search_id'); ?>
        </form>

        <form method="post">
            <input type="hidden" name="page" value="my_list_test" />
            <?php $people_list_table->display(); ?>
        </form>
    </div>
    <?php
}

3. List table class file code:

<?php
if (!class_exists('WP_List_Table'))
{
    require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}

class People_List_Table extends WP_List_Table
{
    // Define table columns
    function get_columns()
    {
        $columns = array(
            'id'               => 'ID',
            'name'             => 'Name',
            'email'       => 'Email',
        );
        return $columns;
    }

    // Get table data
    private function get_table_data($args = [])
    {
        global $wpdb;
        $people_table = $wpdb->prefix . 'people';

        $search = $args['s'];
        $offset = $args['offset'];
        $number = $args['number'];

        if ($search != '')
        {
            $sql = "SELECT id, name, email
                FROM $people_table
                WHERE
                name LIKE '%$search%'
                OR
                email LIKE '%$search%'
                ORDER BY email
                LIMIT $offset, $number";
        }
        else
        {
            $sql = "SELECT id, name, email
                FROM $people_table
                ORDER BY email
                LIMIT $offset, $number";
        }

        return $wpdb->get_results($sql, ARRAY_A);
    }

    private function get_table_data_total_rows($args = [])
    {
        $search = $args['s'];
        global $wpdb;
        $donar_table = $wpdb->prefix . 'people';

        if ($search != '')
        {
            $sql = "SELECT id, name, email
                FROM $people_table
                WHERE
                name LIKE '%$search%'
                OR
                email LIKE '%$search%'
                ORDER BY name";
        }
        else {
            $sql = "SELECT id, name, email
            FROM $people_table
            ORDER BY name";
        }
       
        return count($wpdb->get_results($sql, ARRAY_A));
    }

    function column_default($item, $column_name)
    {
        switch ($column_name)
        {
        case 'name':
        case 'email':
        default:
            return $item[$column_name];
        }
    }

    // Bind table with columns, data and all
    function prepare_items()
    {
        //data
        $per_page = 30;
        $current_page = $this->get_pagenum();

        // TODO: Implement your search logic here
        $search = (isset($_REQUEST['s'])) ? sanitize_text_field($_REQUEST['s']) : '';

        $args = array(
            'offset' => ($current_page - 1) * $per_page,
            'number' => $per_page,
            's' => $search,
        );

        // TODO: Implement your data retrieval logic based on $args
        $data = $this->get_table_data($args); // Replace with your data array
        $total_items = $this->get_table_data_total_rows($args); // Replace with your data array

        $this->set_pagination_args(array(
            'total_items' => $total_items,
            'per_page' => $per_page,
        ));

        $columns = $this->get_columns();
        $hidden = array();
        $sortable = array();

        $this->_column_headers = array($columns, $hidden, $sortable);

        $this->items = $data;
    }
}

.:Happy coding:.


Tuesday, November 7, 2023

Hide calculate shipping for a specific product in woocommerce

Hide calculate shipping for a specific product in woocommerce:

Write the following code in your functions.php:

// Turn off calculate shipping if cart has service product
add_filter('woocommerce_product_needs_shipping', function() {
    foreach (WC()->cart->get_cart() as $cart_item)
    {
        $pid = $cart_item['data']->get_id();
        $product = get_post($pid);
        $slug = $product->post_name;
        if ($slug == 'YOUR-PRODUCT-SLUG') {
            return false;
        }
        else {
            return true;
        }
    }
});


.:Happy coding:.

Saturday, September 9, 2023

Laragon - mysql issue during upgrading mysql V5 to V8

1. Stop Laragon.

2. Remove all files and folders from mysql-8 in laragon -> data -> mysql-8.

3. Open terminal and go to this directory: laragon -> bin -> mysql -> mysql-8 -> bin

4. Run this command: mysqld --initialize --console

5. Run this command: start mysqld

6. Enter in to mysql: mysql -u root -p

7. Password has given in step 42

8. Run this command: ALTER USER 'root'@'localhost' IDENTIFIED BY '';

9. Close mysqld window and start laragon.

Enjoy mysql 8 in laragon!

Tuesday, May 9, 2023

Mysql import from a SQL file by command line way

Mysql import from a SQL file by command line way:

Open command line and login to your mysql database by typing below command:

mysql -u root -p password then press enter.

mysql > source path_of_your_sql_file.sql Eg: if your file in D drive then path will be D:\mysql.sql

Then press enter all your tables will be imported soon.

Tuesday, April 4, 2023

gitignore is not ignoring already committed files or folders

When gitignore is not working for already committed files or folders.

In that time you need to run the below comment first:

git rm --cached file_name

Or

git rm --cached folder_name

Or 

git rm --cached -r folder_name

It will clear committed stuff from git.

Then you can commit and push your branch.

Install LAMP stack on Linux(Ubuntu OS, Zorin OS etc) for local development environment

Step 1: --------------------- Install apache2 by below command: sudo apt install apache2 Step 2: --------------------- Install PHP 7.4: Firs...