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).
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.
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:
Create the shortcode handler function A shortcode function is a function that takes optional parameters (attributes) and returns a result.
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:
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:
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:
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:
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:
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:
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:
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.
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.
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:
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
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:
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:
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):
Bypass Internet Connection during Windows Installation Process
During Windows installation process, when on the Connection screen, proceed as follow:
Press
SHIFT + F10
to open the Command PromptType
OOBE\BYPASSNRO
and press Enterthe system will restart and once you return to the
Let's connect to a network
screen, the itemI don't have internet
will appearClick
I don't have internet
and then clickContinue with limited configuration
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:
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:
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):
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):
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):
php.ini
Settings for File Upload
php.ini
Settings for File Uploadfile_uploads
: determines whether or not to allow file uploads.upload_max_filesize
: specifies the maximum size a single file can have before being uploaded.post_max_size
: sets the maximum size allowed for data sent via POST. Must be greater than upload_max_filesizeupload_tmp_dir
: the directory where uploaded files are temporarily saved, waiting to move them to the permanent folder.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
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.
To apply changes to netplan you will need to reload your Netplan network configurations.
Access Options in Different Network Types in Virtual Machine
Not attached
-
-
-
Network Address Translation (NAT)
-
-
✔
Network Address Translation Service
✔
-
✔
Bridged networking
✔
✔
✔
Internal networking
✔
-
-
Host-only networking
✔
✔
-
Last updated