Last updated on: January 08, 2020 6:21pm
January 08, 2020 6:21pm - 12 min read
Draft: This is not complete.
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.
WordPress was released in 2003 with the latest version being WordPress 5.3.2 released on December 18, 2019.
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.
PHP 5.2+
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.
In WordPress, a theme is a collection of templates and stylesheets used to define the appearance and display of a WordPress powered website.
In WordPress, 4 files are required for your theme to be accepted by the theme review team of WordPress.
But there are only 2 files absolutely required in a WordPress theme:
The basic and fundamental differences between two are:
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.
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 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:
This is how it works for Category and Tag pages:
The WordPress template hierarchy is organized into 7 main categories:
Lets look these 7 main categories in detail.
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:
Hereās how the hierarchy of single posts works:
A single page follows this hierarchy:
If you create your own error page, WordPress will look for it first, as demonstrated by this hierarchy:
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:
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
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.
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.
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.
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.
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:
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.
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…
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 );
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' );
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); }
Below are list of some Filter hooks functions
Below are list of some Action hooks functions
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
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
).
$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); }
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.
Here are the basic steps:
In WordPress, all objects are passed by value.
WordPress has six pre-defined roles:
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.
Upon installing WordPress, an Administrator account is automatically created.