loader spinner

WordPress recap in 2020

January 08, 2020 6:21pm - 12 min read

Draft: This is not complete.

What is a WordPress šŸ™‚ ?

WordPress is an open source CMS developed by Automattic and others contributors around the world. Its built with PHP and MySQL which are also open source and licensed under GPL.

When is WordPress released?

WordPress was released in 2003 with the latest version being WordPress 5.3.2 released on December 18, 2019.

What do you need to run WordPress?

We recommend servers running version 7.3 or greater of PHP and MySQL version 5.6 OR MariaDB version 10.1 or greater. We also recommend either Apache or Nginx as the most robust options for running WordPress, but neither is required.

Which is the PHP compatibility used for WordPress?

PHP 5.2+

Posts and Pages ā€“ What is the Difference?

Pages are static, they are not listed by date, and they donā€™t use tags or categories. They are permanent and timeless (unless you delete them).

Posts are entries listed in reverse chronological order (the latest appears first). The exception is sticky posts that you can create if you want them to appear at the top.

What is a WordPress Theme?

In WordPress, a theme is a collection of templates and stylesheets used to define the appearance and display of a WordPress powered website.

What are the required theme files in WordPress?

In WordPress, 4 files are required for your theme to be accepted by the theme review team of WordPress.

  1. style.css
  2. index.php
  3. comments.php
  4. screenshot.png

But there are only 2 files absolutely required in a WordPress theme:

  1. style.css
  2. index.php

What is the difference between a theme and a plugin?

The basic and fundamental differences between two are:

  • a theme controls the presentation of content; whereas
  • a plugin is used to control the behavior and features of a WordPress site.

Any theme we create should not add critical functionality. Doing so means that when a user changes their theme, they lose access to that functionality. For example, say we build a theme with a portfolio feature. Users who build their portfolio with our feature will lose it when they change themes.

By moving critical features to plugins, we make it possible for the design of your website to change, while the functionality remains the same.

What is the basic structure or header parts of the main stylesheet and child stylesheet?

The main stylesheet and child stylesheet is style.css file. Main stylesheet belongs to parent theme. Stylesheet file (style.css) must be reside in the root directory of your theme not within the CSS directory. The basic and required parts are:

/*
Theme Name: Twenty Seventeen
Author: the WordPress team
Description: Twenty Seventeen brings your site to life with immersive featured images and subtle animations. With a focus on business sites, it features multiple sections on the front page as well as widgets, navigation and social menus, a logo, and more. Personalize its asymmetrical grid with a custom color scheme and showcase your multimedia content with post formats. Our default theme for 2017 works great in many languages, for any abilities, and on any device.
Version: 1.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: twentyseventeen
*/

In a child theme, basic header parts are following where the Template line is required in style.css header.

/*
Theme Name: My Child Theme
Template: Twenty Seventeen
*/

Template hierarchy

Template hierarchy is what determines which templates are used, and in what order.

If you try to load the page for a hypothetical hosting category, for example, hereā€™s what goes on in the background:

  1. WordPress will look for a template file called category-hosting.php within your current themeā€™s directory.
  2. If thereā€™s no category-hosting.php file, WordPress will look for one that uses the categoryā€™s ID instead, such as category-2.php.
  3. Should WordPress not find either of those options, it will look for a generic category.php file instead.
  4. If if doesnā€™t find a file called category.php, WordPress will dial it back and look for the archive.php template.
  5. Finally, if all else fails, the platform will load your themeā€™s index.php file and use it as the pageā€™s template.

This is how it works for Category and Tag pages:

  1. categoryā€“{slug}.php
  2. category-{id}.php
  3. category.php
  4. archive.php
  5. index.php

The WordPress template hierarchy is organized into 7 main categories:

  1. Site front page
  2. Single posts
  3. Single pages
  4. Category and tag pages
  5. Custom post types
  6. Search results pages
  7. 404 error pages

Lets look these 7 main categories in detail.

Site Front Page

First off, letā€™s talk about your websiteā€™s front page. When WordPress loads your home page, the first thing it will look for is a front-page.php file. If that file isnā€™t available, the platform will fall back to home.php. Should both files be missing in action, WordPress will turn to the trusty index.php file, which is always there (otherwise, your theme wouldnā€™t work).

In other words, hereā€™s how this particular hierarchy breaks down:

  1. front-page.php
  2. home.php
  3. index.php

Single Posts

Hereā€™s how the hierarchy of single posts works:

  1. single-{post-type}-{slug}.php
  2. single-{post-type}.php
  3. single.php
  4. singular.php
  5. index.php

Single Pages

A single page follows this hierarchy:

  1. Custom template file
  2. pageā€“{slug}.php
  3. page-{id}.php
  4. page.php
  5. singular.php
  6. index.php

Category and Tag Pages

See the first example

Custom Post Types

  1. archive-{post_type}.php
  2. archive.php
  3. index.php

Search Results Pages

  1. search.php
  2. index.php

404 Error Pages

If you create your own error page, WordPress will look for it first, as demonstrated by this hierarchy:

  1. 404.php
  2. index.php

What is a Template Tag?

Template tags are used within themes to retrieve content from your database. The content could be anything from a blog title to a complete sidebar. Template tags are the preferred method to pull content into your theme because:

  • they can print dynamic content;
  • they can be used in multiple theme files; and
  • they separate the theme into smaller, more understandable, sections.

For example, the template tag get_header() tells WordPress to get the header.php file and include it in the current theme file. Similarly, get_footer() tells WordPress to get the footer.php file.

Below are the list of some generally used template tags in WordPress

  • get_header()
  • wp_register()
  • get_sidebar()
  • wp_title()
  • wp_enqueue_script()
  • get_the_author()
  • wp_list_authors()
  • category_description()
  • get_bookmarks()

Custom post type

How to write a custom widget?

What is a Parent Theme?

What is a Child Theme?

The extension of a parent theme is a child theme. In case you make changes to the parent theme, then any update will undo the changes. When working on a child theme, the customizations are preserved on an update.

Referencing or Including Other Files in child theme

When you need to include files that reside within your child themeā€™s directory structure, you will need to use get_stylesheet_directory(). Since the style.css is in the root of your child themeā€™s subdirectory, get_stylesheet_directory() points to your child themeā€™s directory (not the parent themeā€™s directory). To reference the parent theme directory, you would use get_template_directory() instead.

Below is an example illustrating how to use get_stylesheet_directory() when referencing a file stored within the child theme directory:

require_once( get_stylesheet_directory(). '/my_included_file.php');

Meanwhile, this example uses get_stylesheet_directory_uri() to display an image that is stored within the /images folder in the child theme directory.

get_stylesheet_directory_uri()/images/my_picture.png

Unlike get_stylesheet_directory(), which returns a file path, get_stylesheet_directory_uri() returns a URL, which is useful for front-end assets.

How to debug the theme? Or How will you display error messages during development in WordPress?

To display error messages in WordPress. Open WordPress wp-cofig.php file and change WP_DEBUG constant value to true

In WordPress, WP_DEBUG is a PHP constant (a permanent global variable) that can be used to trigger the ā€œdebugā€ mode throughout the website.

What are WordPress Taxonomies?

A taxonomy is a way to group things together.

Default taxonomies in WordPress are Category, Tag, Link Category, Post Formats.

However, you donā€™t have to limit yourself to default taxonomies; you are free to create custom ones as well.

What Are Custom Fields in WordPress?

Custom fields are also known as post meta. Post meta is a feature in WordPress which allows post authors to add additional information at the time writing a post. WordPress stores this information as metadata in key-value pairs. Users can later display this metadata by using template tags in their WordPress themes if required.

Here are some examples of custom fields:

  • Weather: Drizzle and Moderate Breeze
  • Currently reading: To Kill a Mockingbird
  • Favorite color: Green

What are the types of hooks in WordPress?

There are two types of hooks 1) Action hooks 2) Filter hooks. To use either, you need to write a custom function known as a Callback, and then register it with WordPress hook for a specific Action or Filter.

Why we need hooks? What’s its purpose?

We use hooks to extend or modify the existing functionality of WordPress core. According to WordPress codex documentation:

Hooks are a way for one piece of code to interact/modify another piece of code. They make up the foundation for how plugins and themes interact with WordPress Core…

What is an action hooks?

Action hook does not return a value. Action hook function is performing some kind of action just by being called.

For example:

function frontlash_remove() {
remove_meta_box( 'dashboard_primary', get_current_screen(), 'side' );
}
add_action( 'wp_network_dashboard_setup', 'frontlash_remove', 20 );

What is a filter hooks?

A filter is defined as a function that takes in some kind of input, modifies it, and then returns it.

For example:

add_filter('the_title', 'strrev');

We use PHP function strrev(). It takes a string as an argument, and then returns the reverse of that string. We could use it as a filter function in WordPress, easily. Like, to reverse all your titles.
Another example:

function nirvana_filter_example( $title ) {
	return 'Hooked: ' . $title;
}
add_filter( 'the_title', 'nirvana_filter_example' );

Difference between action and filter hook

Filters filter things. Actions do not. Filter Hooks behave very similar to Action Hooks but their intended use is to receive a value and potentially return a modified version of the value.

Action hooks can modify data but doesn’t return a value. Filter hooks are used mainly to modify data so it depends on input and return that modified version of the input value.

Also don’t mix it. When you mix the two up, then you stop us to interact with your code.
For example

function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
    return add_filter($tag, $function_to_add, $priority, $accepted_args);
}

Common action and filter hooks

Below are list of some Filter hooks functions

  • has_filter()
  • add_filter()
  • apply_filters()
  • apply_filters_ref_array()
  • current_filter()
  • remove_filter()
  • remove_all_filters()
  • doing_filter()

Below are list of some Action hooks functions

  • has_action()
  • add_action()
  • do_action()
  • do_action_ref_array()
  • did_action()
  • remove_action()
  • remove_all_actions()
  • doing_action()

Give me an example of a custom callback function that you can use as hook?

change_admin_footer is a custom function known as a callback.

function change_admin_footer(){
    // Your custom code
}
add_filter('admin_footer_text', 'change_admin_footer');

We can also create our own HOOKS so that other developers can extend and modify our plugin or theme.
For example, a plugin has following function to add a settings form to administrative panels. We can use Actions to allow other plugins to add their own settings to it. This concepts are called Custom Hooks.

function nirvana_settings_page_html()
{
   // some codes here
    do_action('nirvana_after_settings_page_html');
}

Now another plugin can register a callback function for the nirvana_after_settings_page_html hook and inject new settings:

function my_add_settings() // callback
{
    // Your codes here
}
add_action('nirvana_after_settings_page_html', 'my_add_settings'); // custom hook

What is a custom hooks?

Custom hooks are created and called in the same way that WordPress Core hooks are. To create a custom hook, use do_action() for Actions and apply_filters() for Filters.
See the above example (how we modified the nirvana_settings_page_html function using custom hook using add_action with callback my_add_settings).

What is $wpdb?

$wpdb is a global variable that contains of WordPress database object. It is used to perform custom database actions in the WordPress database. It provides the safest way to interact with database.

For example, following code is unsafe in WordPress context:

function perform_database_action(){
    mysql_query(ā€œINSERT into table_name (col1, col2, col3) VALUES ('$value1','$value2', '$value3');
}

The code above doesnā€™t follow WordPress best practices which strongly discourages the use of any mysql_query call. WordPress provides easier and safer solutions through $wpdb.

The above code can be modified to be as follows:

function perform_database_action(){
    global $wpdb;
    $data= array('col1'=>$value1,'col2'=>$value2,'col3'=>$value3);
    $format = array('%s','%s','%s');
    $wpdb->insert('table_name', $data, $format);
}

What is Shortcodes?

Shortcodes are a type of filter. They take in content from the shortcode, they return replacement content of some sort. They are filters, by definition.

Also keep in mind that shortcodes are supposed to return the replacement content, not just echo it out.

How Can I Develop a WordPress Plugin?

Here are the basic steps:

  • You need to come up with a unique name,
  • create the plugin folder with a subfolder for PHP files,
  • create the main plugin file,
  • create functions for activation and deactivation,
  • create an uninstall script and
  • readme.txt file.

In WordPress, objects are passed by value or by reference?

In WordPress, all objects are passed by value.

How many pre-defined user roles in WordPress?

WordPress has six pre-defined roles:

  1. Super Admin,
  2. Administrator,
  3. Editor,
  4. Author,
  5. Contributor and
  6. Subscriber.

Each role is allowed to perform a set of tasks called Capabilities. There are many capabilities including ā€œpublish_postsā€œ, ā€œmoderate_commentsā€œ, and ā€œedit_usersā€œ. A default set of capabilities is pre-assigned to each role, but other capabilities can be assigned or removed using the add_cap() and remove_cap() functions. New roles can be introduced or removed using the add_role() and remove_role() functions.

Summary of Roles

  • Super Admin ā€“ somebody with access to the site network administration features and all other features.
  • Administrator (slug: ā€˜administratorā€™) ā€“ somebody who has access to all the administration features within a single site.
  • Editor (slug: ā€˜editorā€™) ā€“ somebody who can publish and manage posts including the posts of other users.
  • Author (slug: ā€˜authorā€™) ā€“ somebody who can publish and manage their own posts.
  • Contributor (slug: ā€˜contributorā€™) ā€“ somebody who can write and manage their own posts but cannot publish them.
  • Subscriber (slug: ā€˜subscriberā€™) ā€“ somebody who can only manage their profile.

Upon installing WordPress, an Administrator account is automatically created.

Basic understanding of WP Transient

Basic understanding of WP REST API

Basic understanding of WP Cron

Basic understanding of WP object Cache

Basic understanding of browser cachce vs page cache vs object cache

How WordPress makes http requests

Last updated on: January 08, 2020 6:21pm