💻
SBDev Docs
  • Welcome
  • DOCS
    • HTML
    • CSS
    • Javascript
    • PHP
    • Python
    • Bash
    • Miscellaneous
  • EXTRA
    • Color Palette
  • Credits
    • sbdeveloper90
    • GitBook
Powered by GitBook
On this page
  • Wordpress
  • Disable Wordpress Autoremove
  • Upload SVGs in Wordpress Media Library
  • Custom Shortcode
  • Custom Widget
  • Custom Widget Area
  • MySQL
  • Reset Autoincrement
  • Web Hosting
  • .htaccess
  • Startup Folder on Windows
  • Bypass Internet Connection during Windows Installation Process
  • Paper Formats for Printing
  • ANSI vs ISO Keyboard Layout
  • Copy Docker Image to Another Host
  • Entity Relationship Diagram (ERD) Notation
  • Excel Macro for Hightlight Rows/Columns on Cell Click
  • Excel Macro for Uppercase/Lowercase Text Selection
  • php.ini Settings for File Upload
  • Docker Container Port Parameter
  • Linux Set Static IP Address
  • Access Options in Different Network Types in Virtual Machine
  1. DOCS

Miscellaneous

GitBook docs section concerning Miscellaneous context.

Wordpress

Disable Wordpress Autoremove

Insert the following piece of code in Wordpress Theme functions.php file for disabling autoremove trash items (page, post, portfolio) after 30 days (default option).

function wpb_remove_schedule_delete() {
    remove_action( 'wp_scheduled_delete', 'wp_scheduled_delete' );
}
add_action( 'init', 'wpb_remove_schedule_delete' );

Upload SVGs in Wordpress Media Library

Insert the following piece of code in Wordpress Theme functions.php file for enabling SVGs upload in Wordpress Media Library.

function add_file_types_to_uploads($file_types) {
    $new_filetypes = array();
    $new_filetypes['svg'] = 'image/svg+xml';
    $file_types = array_merge($file_types, $new_filetypes);
    return $file_types;
}
add_filter('upload_mimes', 'add_file_types_to_uploads');

Custom Shortcode

WordPress shortcodes are used to reduce the amount of code you need to write and to simplify the usage of WordPress plugins, themes, and other functions. They behave like macros, when you insert a shortcode, it is replaced with a snippet of code. It could be anything. When creating your own shortcodes, there are two things you need to do:

  1. Create the shortcode handler function A shortcode function is a function that takes optional parameters (attributes) and returns a result.

  2. Register the shortcode handler function Use the built-in WordPress add_shortcut function to register custom shortcodes.

Preparing Wordpress for Custom Shortcode

Although it’s not required, it’s a good idea to keep your custom shortcodes in their own file. Alternatively, you may add them to your theme’s functions.php file. First, create a new file named custom-shortcodes.php, and save it inside the same folder as your theme’s functions.php file. Next, open the functions.php file, and add the following line of code:

include('custom-shortcodes.php');

Basic Shortcodes

In this first example, we’re going to create a basic WordPress shortcode that inserts simple avatar image. The first step is to create the shortcode function. Inside the custom-shortcodes.php file, add the following block of code:

function dotiavatar_function() {
    return '<img src="http://mydomain.com/wp-content/uploads/avatar-simple.png" alt="doti-avatar" width="96" height="96" class="center-align" />';
}

In the code example above, the dotiavatar_function function returns a pre-determined image named avatar-simple.png. The next step is to register the shortcode with WordPress using the built-in function add_shortcode. Still inside custom-shortcodes.php, add the following line of code:

add_shortcode('dotiavatar', 'dotiavatar_function');

When you register a shortcode using the add_shortcode function, you pass in the shortcode tag and the corresponding function/hook that will execute whenever the shortcut is used. In this case, the shortcut tag is dotiavatar and the hook is dotiavatar_function.

Note: When naming tags, use lowercase letters only, and do not use hyphens; underscores are acceptable.

Using the Shortcodes

Now that you have the shortcode created and registered, it’s time to try it out! Whenever you want the DOTI avatar to appear inside the post content, you can use the shortcode instead:

[dotiavatar]

Shortcodes with parameters (attributes)

In the previous example, there wasn’t much room to change things up. Let’s say, instead of pushing a single image, we’d like to be able to set which image to use using a parameter. You can do that by adding some attributes ($atts). Once again, inside custom-shortcodes.php, add another function, like so:

function dotirating_function( $atts = array() ) {

    // set up default parameters
    extract(shortcode_atts( array('rating' => '5'), $atts) );

    return '<img src="http://mydomain.com/wp-content/uploads/$rating-star.png" alt="doti-rating" width="130" height="188" class="center-align" />';
}

The function above accepts a single parameter: rating. If a rating value is not passed, it uses a default string value of 5. It does this by unwrapping the array of attributes using the built-in shortcode_atts function, and combining the default values with values that may have been passed into the function. Don’t forget to register the shortcode, like above. With the shortcode function created, and the hook added, the shortcode is now ready to be used inside your post content:

[dotirating rating=3]

Alternatively, you can omit the rating, and just go with the default value.

A word about Widgets

By default, shortcodes are only supported in posts, pages, or custom post types; they are not supported in sidebar widgets. To add support for widgets, you need to add the following code to the functions.php file:

add_filter( 'widget_text', 'shortcode_unautop' );
add_filter( 'widget_text', 'do_shortcode' );

Once you do that, you can use shortcodes in widgets, just like you do in posts/pages.

Custom Widget

By default, WordPress comes with a standard set of widgets that you can use with any WordPress theme. WordPress also allows developers to create their own custom widgets. WordPress comes with a built-in WordPress Widget class. Each new WordPress widget extends the WordPress widget class. we will be focusing on the following methods:

  • __construct(): This is the part where we create the widget ID, title, and description.

  • widget: This is where we define the output generated by the widget.

  • form: This part of the code is where we create the form with widget options for backend.

  • update: This is the part where we save widget options in the database.

// Creating the widget
class wpb_widget extends WP_Widget {
    function __construct() {
        parent::__construct(
            // Base ID of your widget
            'wpb_widget',
            // Widget name will appear in UI
            __('WPBeginner Widget', 'wpb_widget_domain'),
            // Widget description
            array( 'description' => __( 'Sample widget based on WPBeginner Tutorial', 'wpb_widget_domain' ), )
        );
    }

    // Creating widget front-end
    public function widget( $args, $istance ) {
        $title = apply_filters( 'widget_title', $istance['title'] );

        // before and after widget arguments are defined by themes
        echo $args['before_widget'];
        if ( ! empty( $title ) )
            echo $args['before_title'] . $title . $args['after_title'];

        // This is where you run the code and display the output
        echo __( 'Hello, World!', 'wpb_widget_domain' );
        echo $args['after_widget'];
    }

    // Widget Backend
    public function form( $instance ) {
        if ( isset( $instance[ 'title' ] ) ) {
            $title = $instance[ 'title' ];
        }
        else {
            $title = __( 'New title', 'wpb_widget_domain' );
        }
        // Widget admin form
        ?>
            <p>
                <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
            </p>
        <?php
    }

    // Updating widget replacing old instances with new
    public function update( $new_instance, $old_instance ) {
        $instance = array();
        $instance['title'] = ( !empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
        return $instance;
    }

    // Class wpb_widget ends here
}

// Register and load the widget
function wpb_load_widget() {
    register_widget( 'wpb_widget' );
}
add_action( 'widgets_init', 'wpb_load_widget' );

After adding the code you need to head over to Appearance » Widgets page. You will notice the new WPBeginner Widget in the list of available widgets. You need to drag and drop this widget to a sidebar.

Custom Widget Area

When creating your own shortcodes, there are three steps you need to follow: registering the widget area, inserting widgets, and showing the widget area on the website.

Registering the widget area

The first step to adding widget areas to your WordPress website is the registration. Every widget area must be first registered using the register_sidebar function. This requires inserting a code which is similar to the one given below inside the functions.php file of your theme.

function register_custom_widget_area() {
    register_sidebar(
        array(
            'id' => 'new-widget-area',
            'name' => esc_html__( 'My new widget area', 'theme-domain' ),
            'description' => esc_html__( 'A new widget area made for testing purposes', 'theme-domain' ),
            'before_widget' => '<div id="%1$s" class="widget %2$s">',
            'after_widget' => '</div>',
            'before_title' => '<div class="widget-title-holder"><h3 class="widget-title">',
            'after_title' => '</h3></div>'
        )
    );
}
add_action( 'widget_init', 'register_custom_widget_area' );
  • id: a unique identifier of a widget area

  • name: the name of the widget area, which will be displayed in Appearance » Widgets

  • description: a description of the widget area that is displayed within it and visible in Appearance » Widgets

  • class: additional CSS class that can be assigned to the widget area, which will help you customize the widget area with CSS later

  • before_widget: a block of HTML code added before every widget that belongs to the widget area

  • after_widget: a block of HTML code added after every widget that belongs to the widget area

  • before_title: a block of HTML code added before the widget area title when it is displayed

  • after_title: a block of HTML code added after the widget area title when it is displayed

Inserting the widgets inside the widget area

After registration, the second step should be very familiar to any WordPress user. It involves dragging and dropping widgets inside the newly created widget area. To do this, navigate to Appearance » Widgets, locate the widget area you created and add the widgets you want to it.

Showing the widget area on the website

However, even after you register the widget area and make sure it isn’t empty, it still won’t automatically show on your site. For it to appear, you need to add additional code that “calls” the specific sidebar, i.e. widget area, that you created. The code you need to insert is similar to this one:

<?php if ( is_active_sidebar( 'new-widget-area' ) ) : ?>
    <div id="secondary-sidebar" class="new-widget-area">
        <?php dynamic_sidebar( 'new-widget-area' ); ?>
    </div>
<?php endif; ?>

You need to place this code inside one of the theme’s template files. Which file you use depends entirely on where you wish your new widget area to show. The most common choices are sidebar.php, header.php, or footer.php files. The function dynamic_sidebar is the one responsible for showing the widget area. It accepts a previously registered name or id of a widget area as valid parameters. Wrapping the widget area with a div tag isn’t necessary, but we recommend it as it makes the HTML of your pages more structured. Finally, by further wrapping the code with the is_active_sidebar conditional tag, we are making sure the widget area is only shown if it has widgets inserted.

MySQL

Reset Autoincrement

ALTER TABLE tablename AUTO_INCREMENT = 1

For InnoDB you cannot set the auto_increment value lower o equal to the highest current index.

Web Hosting

.htaccess

The .htaccess file can be used for redirecting to web pages following particular errors; or even to protect certain folders with passord (encrypted with MD5 on Apache). Here are some examples:

ErrorDocument 404 "path/to/page/error-404.html"
AuthType Basic
AuthName "Restricted Area"
AuthUserFile "path/to/file/.htpasswd"
Require valid-user
test:$apr1$gbx5fvw6$gcTXN.nGIYPiJt4d9Gy57.

Startup Folder on Windows

The Startup folder in Windows is no longer listed directly in the Start menu, although both the user-specific folder and the common folder remain in the Start menu directory. The exact paths through which to open both folders are as follows:

C:\Users\USER\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup

Both folders are well hidden inside the directory structures of the Windows partition, so the operating system offers the possibility to call them through a shell command. Press Windows + R and enter the following code (depending on whether you want to access the user's individual folder or the common folder):

shell:startup

shell:common startup

Bypass Internet Connection during Windows Installation Process

During Windows installation process, when on the Connection screen, proceed as follow:

  1. Press SHIFT + F10 to open the Command Prompt

  2. Type OOBE\BYPASSNRO and press Enter

  3. the system will restart and once you return to the Let's connect to a network screen, the item I don't have internet will appear

  4. Click I don't have internet and then click Continue with limited configuration

  5. At this point you will be able to complete the installation with local account

Paper Formats for Printing

ANSI vs ISO Keyboard Layout

Copy Docker Image to Another Host

You will need to save the Docker image as a tar file:

docker save -o <path-for-generated-tar-file/tar-file-name.tar> <image name>

Then copy your image to a new system with regular file transfer tools such as cp, scp or rsync (preferred for big files). After that you will have to load the image into Docker:

docker load -i <path-for-generated-tar-file/tar-file-name.tar>

PS: You may need to `sudo` all commands.

Entity Relationship Diagram (ERD) Notation

Excel Macro for Hightlight Rows/Columns on Cell Click

The VBA code below hightlights entire row and entire column on cell click (remember to save the Excel file as .xlsm to enable macro execution):

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Cells.Interior.ColumnIndex = xlColorIndexNone
    Target.EntireColumn.Interior.ColorIndex = 39
    Target.EntireRow.Interior.ColorIndex = 39
    Target.Interior.ColorIndex = xlColorIndexNone
End Sub

Excel Macro for Uppercase/Lowercase Text Selection

The VBA code below would go through all the cells in the selected range and convert all the text strings into upper case (remember to save the Excel file as .xlsm to enable macro execution):

Sub UCaseConverter()
    Dim rng As Range
    Set rng = Selection
    For Each Cell In rng
        Cell.Value = UCase(Cell)
    Next Cell
End Sub

The VBA code below would go through all the cells in the selected range and convert all the text strings into lower case (remember to save the Excel file as .xlsm to enable macro execution):

Sub LCaseConverter()
    Dim rng As Range
    Set rng = Selection
    For Each Cell In rng
        Cell.Value = LCase(Cell)
    Next Cell
End Sub

php.ini Settings for File Upload

  1. file_uploads: determines whether or not to allow file uploads.

  2. upload_max_filesize: specifies the maximum size a single file can have before being uploaded.

  3. post_max_size: sets the maximum size allowed for data sent via POST. Must be greater than upload_max_filesize

  4. upload_tmp_dir: the directory where uploaded files are temporarily saved, waiting to move them to the permanent folder.

  5. max_file_uploads: the maximum number of files that can be uploaded via a single HTTP request.

Docker Container Port Parameter

These parameters are separated by a colon and indicate <external>:<internal> respectively. For example, -p 8080:80 would expose port 80 from inside the container to be accessible from the host's IP on port 8080 outside the container.

Linux Set Static IP Address

sudo nano /etc/netplan/00-install-config.yaml

The following is an example Netplan file with a network interface that has a static IP address. The interface's name is en01 and it has been assigned static IP addresses 192.168.1.25/24 for IPV4.

DNS Name servers are also defined in this file.

network:
    version: 2
    renderer: networkd
    ethernets:
        en01:
            addresses:
            - 192.168.1.25/24
            gateway4: 192.168.1.1
            nameservers:
                addresses:
                - 8.8.8.8
                - 8.8.4.4

To apply changes to netplan you will need to reload your Netplan network configurations.

sudo netplan apply

Access Options in Different Network Types in Virtual Machine

Network type
Access Guest -> other Guests
Access Host -> Guest
Access Guest -> external Network

Not attached

-

-

-

Network Address Translation (NAT)

-

-

✔

Network Address Translation Service

✔

-

✔

Bridged networking

✔

✔

✔

Internal networking

✔

-

-

Host-only networking

✔

✔

-

PreviousBashNextColor Palette

Last updated 2 months ago

paper-print-formats
ansi-vs-iso
erd-notation