Sunday, September 11, 2022

Interview questions and answers for CodeIgniter

 1 : CodeIgniter current version : till 11 September 2022

CodeIgniter 3 : 3.1.13.

CodeIgniter 4 : 4.2.6

2 : Explain the difference between helper and library in CodeIgniter.

Helper Library
Helper is a collection of common functions which we can use within Models, Views as well as in Controllers. Once we include the helper file, we can get access to the functions.Library is a class that has a set of functions that permits for creating an instance of that class by $this->load->library() function.
It is not written in object-oriented format.It is written in an object-oriented format.
It can be called in the same manner you call PHP functions.You must create an object of the class to call library functions by using the $this->library_name->method().
All built-in helper file names are suffixed with a word _helper (ex: email_helper.php).All built-in library files do not have a specific suffix.
 
3 : What is routing in CodeIgniter?
  • Routing is a technique used in CodeIgniter, by which you can define your URLs based on the requirement instead of using the predefined URLs. So, whenever there is a request made and matches the URL pattern defined by us, it will automatically direct to the specified controller and function.
  • A URL string and its corresponding controller class or method are in a one-to-one relationship here. The URI segments usually follow this pattern: example.com/class/function/id/. All routing rules are defined in the application/config/routes.php file of CodeIgniter.

4 : Why CodeIgniter is called a loosely based MVC framework?

Codeigniter is called a loosely based MVC framework because it does not need to obey a strict MVC pattern during application creation. It is not important to create a model, we can use only view and controllers for creating an application. In addition, one can modify CodeIgniter to utilize HMVC(Hierarchical Model View Controller) as well.

5. What is a helper in CodeIgniter?

  • Helpers are the group of functions that are useful in assisting the user to perform specific tasks.
  • There are three types of helper files. They are:
    • URL helpers: Used for creating the links.
    • Text helpers: Used for the formatting of text.
    • Cookies helpers: Used to read and manage cookies.
6. How to load a helper in CodeIgniter?
  • You need to load the helper files for using it. Once loaded, it will be globally available to your controller and views. They can be obtained at two places in CodeIgniter. A helper file will be searched by CodeIgniter in the application/helpers folder and if it is not available in that folder then it will check in the system/helpers folder.
  • Helper file can be loaded by adding the following code to the constructor of the controller or inside any function that wants to use: $this->load->helper('file_name');
    Write your file name at the place of file_name.
  • To load URL helper we can use the code given below: $this->load->helper('url');
  • You are allowed to auto-load a helper if your application needs that helper globally by including it in the application/config/autoload.php file.
  • Loading multiple helpers is also possible. For doing this, specify them in an array as given below:
$this->load->helper(  
   array('helper1', 'helper2', 'helper3')  
); 
7 : Give the list of hooks available in CodeIgniter.

The list of available hook points are given below:

  • pre_system: It is called initially during system execution.
  • pre_controller: It is called immediately before any of the controllers being called. Example:
$hook['pre_controller'] = array(
   'class'    => 'ExampleClass',
   'function' => 'Examplefunction',
   'filename' => 'ExampleClass.php',
   'filepath' => 'hooks',
   'params'   => array('mango', 'apple', 'orange')
   );
  • post_controller_constructor: It is called soon after instantiating your controller, but before any occurrence of the method call.
  • post_controller: It is called immediately after the complete execution of the controller.
  • display_override: It overrides the _display() method.
  • cache_override: It enables calling of the user-defined method instead of _display_cache() method which is available in the Output Library. This permits you for using your own cache display mechanism.
  • post_system: It is called soon after the final rendered page has been submitted to the web browser, at the end of system execution when the final data has been sent to the browser

8 : What is meant by a library? How can you load a library in CodeIgniter?
  • Libraries are packages created in PHP that give higher-level abstractions and thus contribute to faster development. This removes the necessity of focusing on small, minute details by taking care of those by themselves.
  • Three methods are available to create a library:
    • Create an entirely new library
    • Extend native libraries
    • Replace native libraries
  • To load a library in CodeIgniter, you have to include the below code inside a controller: $this->load->library(‘class_name’);
  • All pre-defined libraries developed by CodeIgniter can be obtained at the system/libraries directory.
  • For loading multiple libraries at the same time, you can make use of the same code. But replace the parameter with an array for loading multiple libraries.
$this->load->library(array(‘library1’, ‘library2’));
9 : How to extend the class in CodeIgniter?

You have to create a file with the name Example.php under application/core/ directory and declare your class with the below code:

Class Example extends CI_Input {
     // Write your code here
}
10 :What is the difference between Laravel and CodeIgniter?
Based on LaravelCodeIgniter
Database model It is object-oriented.It is relational object-oriented.
Built-in moduleIt comes along with a built-in module.It does not come with a built-in module.
StructureFollows MVC structure of filing with a command-line tool known as Artisan.Follows the MVC structure but it provides easier boarding based on object-oriented programming.
Development and Template It is a good option for front-end developers and it comes along with the Blade template engine.It is easier to use and there is no template engine provided.
Utilized by OctoberCMS, LaracastsPyroCMS, Expression engine
LibrariesProvide their own official documentation which is very helpful.Provides a lot of built-in functionality
Routing Supports Explicit routingSupports both Explicit and Implicit routing.

11 :List various databases supported by the CodeIgniter framework.

Following Databases are supported by the CodeIgniter framework:

  • MySQL (version 5.1+) database that uses MySQL (deprecated), mysqli, and PDO drivers
  • Oracle database that uses oci8 and PDO drivers
  • PostgreSQL database that uses Postgre and PDO drivers
  • ODBC database that uses ODBC and PDO drivers
  • SQLite database that uses SQLite version 2, SQLite3 version 3, along with PDO drivers
  • MS SQL database that uses Sqlsrv (version 2005 and above), MsSQL, and PDO drivers
  • Interbase/Firebird database that uses iBase and PDO drivers
  • CUBRID database that uses Cubridand PDO drivers
12 : How to deal with Error handling in CodeIgniter?

CodeIgniter enables you to develop error reporting into your applications by using the below-given functions. Also, it has a class dedicated to error logging that permits messages related to error and debugging to be saved as text files.

Functions related to error handling are:

  • This function will display the error message provided by the application/errors/errorgeneral.php template.
show_error(‘message’ [, int $statuscode= 500 ] )
  • This function shows the 404 error message supplied to it by using the application/errors/error404.php template.
show_404(‘page’ [, ‘logerror’])
  • This function permits you to write messages onto your log files. You must provide anyone among three “levels” in the first parameter that indicates the message type (debug, error, info), with the message itself in the second parameter.
log_message(‘level’, ‘message’)
13 : How you can add or load a model in CodeIgniter?
  • In CodeIgniter, models are loaded as well as called inside your controller methods. For loading a model, you must use the below-given method:
$this->load->model('name_of_the_model');
  • Include the relative path from the directory of your model, if your model is placed inside a sub-directory. Consider an example, you have a model which is placed at application/models/blog/AllPosts.php you can load it by using: $this->load->model('blog/AllPosts');
  • You can access the methods provided by the model, once the model gets loaded by using an object which has the same name as your controller:
class MyBlogController extends CI_Controller
{
   public function MyblogModel()
   {
           $this->load->model('blog_model');
           $data['que'] = $this->blog_model->get_last_five_entries();
           $this->load->view('blog_model', $data);
   }
}
14 :How to pass an array from the controller to view in CodeIgniter?

A view is a webpage that shows each element of the user interface. It cannot be called directly, you need to load the views via the controller. You can pass an array from the controller to view in CodeIgniter using below given steps:

  • Create a view:

Create a new text file and name it ciblogview.php. Save the created file in the application/views/ directory. Open the ciblogview.php file and add the below-given code to it:

<html>
<head>
   <title>Blog</title>
</head>
<body>
   <h1>Welcome to Blog in CodeIgniter</h1>
</body>
</html>
  • Load the view:

Loading a view is executed using the following syntax: $this->load->view('name');

Where ‘name’ represents the name of the view.

The below code creates a controller named Blog.php. This controller has the method for loading the view.

<?php
class Blog extends CI_Controller 
{
   public function index()
   {
       $this->load->view('ciblogview');
   }
}
?>
  • Passing an array from the controller to view:

You are allowed to paste the below-given controller code within your controller file or put it in the controller object.

$data['mega_header'][] = (object) array('title' => 'image portfolio' , 'img' => 'https://complete_image_path' );
$this->load->view('multiple_array', $data);

Arrays are displayed as a brick[‘…’] and objects as an arrow(->). You are allowed to access an array with the brick[‘…’] and object using the arrow (->). Therefore, add the below-given code in the view file:

<?php
   if (isset($mega_header)){
       foreach ($mega_header as $key) {
           ?>
           <div class="header_item">
               <img alt="<?php echo($key['title']); ?>" src="<?php echo($key->img); ?>"/>
           </div>
           <?php
       }
   }
?>
  • Add dynamic data to views:

Usually, data transfer from the controller to view is done through an array or an object. The array or the object is passed as the second parameter of the view load method similar to the below-given method:

$data = array(
   'title' => 'TitleValue',
   'heading' => 'HeadingValue'
);
$this->load->view('ciblogview', $data);

The controller will look like this:

<?php
   class Blog extends CI_Controller {
   public function index()
   {
       $data['title'] = "TitleValue";
       $data['heading'] = "HeadingValue";
       $this->load->view('ciblogview', $data);
   }
}
?>

The view file will look like this:

<html>
<head>
   <title><?php echo $title;?></title>
</head>
<body>
   <h1><?php echo $heading;?></h1>
</body>
</html>
15 :What are the sessions in CodeIgniter? How to handle sessions in CodeIgniter?

In CodeIgniter, you are allowed to maintain a user’s “state” by Session class and keep an eye on their activity while they browse your website.

  • Loading a session in CodeIgniter:
    For using session, your controller should be loaded with your Session class by using the $this->load->library(‘session’);.
    Once the Session class is loaded, the Session library object can be obtained using $this->session.
  • Read session data in CodeIgniter:
    $this->session->userdata(); method of Session class is used to read or obtain session data in CodeIgniter.
    Usage: $this->session->userdata('name_of_user');
    Also, the below-given method of the Session class can be used to read session data.
    Usage: $this->session->key_item
    Where an item represents the key name you want to access.
  • Create a session in CodeIgniter:
    The set_userdata() method that belongs to the Session class is useful in creating a session in CodeIgniter. This method uses an associative array that has the data you want to include in the session.
    Adding session data:
    Example:
$sessiondata = array(
   'name_of_user'  => 'lekha',
   'email'     => 'lekha@interviewbit.com',
   'log_state' => TRUE
);
$this->session->set_userdata($sessiondata);

If you want to add a single user data at a time, set_userdata() supports this syntax:

$this->session->set_userdata('demo_username', 'demo_value');
  • Remove session data in CodeIgniter:
    The unset_userdata() method that belongs to the Session class is useful for removing session data in CodeIgniter. Usage examples are given below:
    Unset particular key:
$this->session->unset_userdata('name_of_user');

Unset an array of item keys:

$arr_items = array('name_of_user', 'email');
$this->session->unset_userdata($arr_items);
16 : Explain CodeIgniter folder structure.

The CodeIgniter folder structure is given below:

  • application: This directory will have your application logic. All of your application codes will be held in this directory. Internal subdirectories in the CodeIgniter directory structure are given below:
    • cache – It stores cached files.
    • config – It keeps configuration files.
    • controller – All application controllers are defined under this controller.
    • core – It consists of custom core classes that extend system files. For example, if you create a base controller that other controllers should extend, then you should place it under this directory.
    • helpers – This directory will be used for user-defined helper functions.
    • hooks – It is used for custom hooks in the CodeIgniter folder structure.
    • language – It is used to store language files for applications that use multiple languages.
    • libraries – It is used to store custom-created libraries.
    • logs – Application log files are placed in this directory.
    • models - All application models must be defined under this directory.
    • third_party – This is used for custom many packages that are created by you or other developers.
    • views – application views will be stored in this directory.
  • system: It consists of the framework core files. It is not advised to make any modifications in this directory or put your own application code into this directory. System subdirectories in CodeIgniter are given below:
    • core – This is considered to be the heart of the CodeIgniter Framework. All of the core files that construct the framework are located here. If you would like to extend the core file functionality, then you must
    • create a custom core file in the application directory. After this, you are allowed to override or add new behavior that you wish. You should never make any changes directly in this directory.
    • database – It stores the files such as database drivers, cache, and other files that are needed for database operations.
    • fonts – This directory contains fonts and font-related information.
    • helpers – This directory consists of helper functions that come out of the box.
    • language – It contains language files that are used by the framework
    • libraries – It contains the source files for the different libraries that come along with CodeIgniter out of the box.
  • user_guide: This directory consists of a user manual for CodeIgniter. You should not upload this directory during application deployment.
  • vendor: This directory consists of composer packages source code. The composer.json and composer.lock are the other two files related to this directory.
  • index.php: This is considered as the entry point into the application. It is placed inside the root directory.

17. What is the security parameter for XSS in CodeIgniter?

  • Codeigniter has got a Cross-Site Scripting(XSS) hack prevention filter. This filter either automatically runs or you can run it based on item, to filter all data related to POST and COOKIE.
  • The XSS filter will target the frequently used methods to trigger JavaScript code or other types of code that attempt to hijack cookies or do any other malicious activity. If it identifies anything suspicious or anything disallowed is encountered, then it will convert the data to character entities.
  • To filter data through the XSS filter, we will make use of the xss_clean() method as given below:
$data = $this->security->xss_clean($data);

This function is used only when you are submitting data. The second Boolean parameter is optional and used to check the image files for the XSS attacks. This is very useful for file upload. If its value is true, that means the image is safer and not otherwise.

18. What is the default controller in CodeIgniter?

  • When the name of the file is not mentioned in the URL then the file will be specified in the default controller that is loaded by default. By default, the file name will be welcome.php, which is known as the first page to be seen after the installation of CodeIgniter.
  • localhost/codeigniter/  In this case, the welcome.php will be generally loaded as the file name is not mentioned in the provided URL. Generally, the programmers can change the default controller that is present in the application/config/routes.php file as per their needs.
  • $route['default_controller'] = ' ';In the above-given syntax, the programmer has to specify the file name that he/she wants to get loaded as the default one.

19. List all the auto-loadable resources available in CodeIgniter.

  • The below-given items can be automatically loaded in CodeIgniter:
    • Classes obtained in the directory named libraries/
    • Custom config files obtained in the directory named config/
    • Helper files obtained in the directory named helpers/
    • Models obtained in the directory named models/
    • Language files obtained in the directory named system/language/
  • For resource autoloading, you should open the file application/config/autoload.php and include the item that you want to be get loaded into the array of autoloads. In the file related to each type of item, you can find instructions

20 : How to connect multiple databases in CodeIgniter?

To connect more than one database simultaneously, do the following,

  1. $db1 = $this->load->database('group_one', TRUE);  
  2. $db1 = $this->load->database('group_two', TRUE); 
21 : How can you print SQL statement in CodeIgniter model?
  1. $this>db>insertid();  
22 : What is an inhibitor of CodeIgniter?

In CodeIgniter, Inhibitor is an error handler class that uses native PHP functions like set_exception_handler, set_error_handler, register_shutdown_function to handle parse errors, exceptions, and fatal errors.

23 :What are the various functions used in Codeigniter?

Ans: The functions in Codeigniter are globally defined and freely available. Loading of libraries or helpers is not required when using these functions.

is_really_writable($file)
is_php($version)
html_escape($var)
config_item($key)
set_status_header($code[, $text = ”])
is_https()
remove_invisible_characters($str[, $url_encoded = TRUE])
is_cli()
get_mimes()
24 : How is the default timezone set in Codeigniter?

Ans: The default timezone in Codeigniter is set by opening the application using the config.php file and adding this code to it.

date_default_timezone_set('your timezone');
25. What is the procedure to add/link JavaScript/Images?CSS in Codeigniter?

Ans: To link or add the images/CSS/JavaScript in Codeigniter absolute path to your resources is used.

For example

// Refers to $config['base_url']
<img src="<?php echo site_url('images/myimage.jpg'); ?>" />
26 :How to extract the last inserted id in Codeigniter?

Ans: The DB Class insert_id() method is used in Codeigniter to get the last insert id.

Usage:

function add_post($post_data){
$this->db->insert('posts', $post_data);
$insert_id = $this->db->insert_id();
return $insert_id;
}

27 : How do you remove index.php from the URL in Codeigniter?

Ans: To remove the index.php from URL in Codeigniter the following steps are used
Step 1: Open config.php and replaces

$config['index_page'] = "index.php" to $config['index_page'] = "" 

and

$config['uri_protocol'] ="AUTO" to $config['uri_protocol'] = "REQUEST_URI"

Step 2: Change your .htaccess file to

RewriteEngine on
RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

28 : How we can print SQL statement in codeigniter model?

You can use $this->db->last_query(); to display the query string.
You can use print_r($query); to display the query result.



29 : How to get last inserted id in codeigniter?

You can use $lastInsertID = $this->db->insert_id(); after insert query.



Interview questions and answers for Wordpress

1:  Wordpress latest version : 6.0.2

2 : How to create a plugin in WordPress?

The following steps will guide you through creating your first plugin:

  • Connect to your site via FTP (File Transfer Protocol).
  • Navigate to the WordPress plugins folder after gaining access to your site via FTP. This folder is usually located in /wp-content/plugins.
  • After you've navigated into the plugins folder, you can create a folder of your own. Create a new folder with a unique name that uses lowercase letters and dashes, for example, first_plugin. Once you've done that, choose your new folder and continue.
  • You will then need to create the main plugin file.
  • Then, create a file named very-first-plugin.php in your new plugin folder.
  • The plugin information can now be copied and pasted into your newly created main plugin file. Make sure to modify plugin details such as Plugin URL and Plugin Name.

3 : What is required to run WordPress?

 WordPress has the following minimal requirements:

  • PHP version 7.2 or higher.
  • A MySQL version of 5.6 or higher OR a MariaDB version of 10.0 or higher.
  • Support for HTTP.
  • The best servers for running WordPress are Nginx or Apache, but any server that supports MySQL and PHP will work.

4. How many tables are there by default in WordPress?

In WordPress databases, the shelves are called tables. By default, WordPress websites contain 12 tables. Only certain data can be stored in each table. WordPress comments tables, for example, contain information regarding IP addresses, comment author slugs, etc., of people who have commented on a post. In this way, data can be stored and retrieved more quickly.

5. What are the basic steps we should take when our WordPress website is hacked?

Since WordPress is one of the most popular CMS platforms on the web, its security is paramount. Upon analyzing the existing loopholes, standard security policies should be implemented in WordPress to protect WordPress data. The steps that must be taken when your WordPress platform is hacked are as follows:

  • Keep calm and seek an optimal solution.
  • Track down the hack.
  • Contact your web hosting provider.
  • Get an InfoSec expert to help you.
  • Download and install a security plugin, such as WordPress security.
  • Locate, scan, and fix/remove malware.
  • Ensure that your access permissions are correct.
  • Also, update WordPress to its latest version. 
  • Update all user IDs and passwords.
  • Check to make sure that your plugins and themes are up to date.

6. How to increase WordPress website security?

Despite being a safe CMS (content management system), WordPress is susceptible to attacks, just like any other CMS. WordPress security can be enhanced in many ways, as follows:

  • Login procedures should be secure.
  • Consider using a secure WordPress hosting provider.
  • Update the WordPress version.
  • Upgrade the PHP version.
  • Download and install security plugins.
  • Select a secure theme for WordPress.
  • Enable SSL (Secure Socket Layer) or HTTPS (HyperText Transfer Protocol Secure) for your site.
  • Set up a firewall.
  • Make a backup of your site.
  • Manage user permissions on WordPress.
  • Change the default login URL of WordPress.
  • Disable xmlrpc.php file.
  • Disable editing files within the WordPress dashboard.
  • Modify the database file prefix.
  • Delete the default WordPress administrator account.
  • Hide the WordPress version.

7:  Can you create custom post types in WordPress?

WordPress developers are able to create custom post types. The register_post_type() function may be used to add a custom post type. Plugins allow us to create custom post types as well.

  • Custom Post Type UI
  • Custom Field

8 : Where is the WordPress content stored?

Wp_posts is a table in your MySQL database that holds the content of your WordPress posts and pages. WordPress displays your content using dynamic PHP templates, so it doesn't store a separate HTML file for each page. In the database, you can also find "Pages" (or other custom post types available in WordPress) in the wp_posts table. Typically, these can be accessed through phpMyAdmin.

9 : List a few template tags that can be used in WordPress.

The following are some template tags you can use in WordPress.

  • get_header()
  • get_footer()
  • get_sidebar()
  • wp_login_url()
  • get_calendar()
  • allowed_tags()
  • the_author()
  • get_the_author()
  • wp_list_bookmarks()
  • get_bookmark()
  • the_category()
  • the_category_rss()
  • comment_author()
  • comment_author_email()
  • the_permalink()
  • user_trailingslashit()
  • permalink_anchor()
  • post_class()
  • post_password_required()
  • get_post_thumbnail_id()
  • the_post_thumbnail()
  • wp_nav_menu()
  • walk_nav_menu_tree(), etc

10 : What do you mean by WordPress Hooks?

Whenever you wish to alter or customize something in WordPress, you can almost certainly make use of a hook. WordPress hooks enable you to manipulate the behavior of a procedure without having to modify the WordPress core files. With hooks, you can modify or add new features to WordPress without modifying the core files.

11 : What is Action and Filter Hooks?

Action and Filter are types of WordPress Hooks.

  • Action Hooks: Actions enable you to perform a task at predefined points during the WordPress runtime. In the WordPress code, actions are defined as follows:
do_action( 'action_name', [optional_arguments] );

Here, the 'action_name' string represents the name of the action. If you would like to pass additional arguments to the callback function, you can specify the [optional_arguments] variable. When no value is specified, this field is empty by default.

  • Filter Hooks: Filters allow you to modify and return any data processed by WordPress. In the WordPress code, filters are defined as follows:
apply_filters( 'filter_name', 'value_to_be_filtered', [optional_arguments] );

Here, the ‘filter_name’ string represents the name of the filter. 'value_to_be_filtered' represents the value that has to be filtered and returned. In the same way as actions, the [optional_arguments] variable can pass additional arguments

12 : Explain shortcode in WordPress.

A shortcode is a small chunk of code, placed between brackets like [yourshortcode], that performs a specific function for your site. It can be placed anywhere to provide a specific feature to a page, post, or other content. WordPress shortcodes make it easy to add dynamic content to pages, posts, and sidebars. Several WordPress plugins and themes offer shortcodes for adding specialized content to websites, such as sliders, image galleries, contact forms, and more.

13 : What is a Child theme in WordPress?

A child theme inherits the functionality, features, and styling of the parent theme (WordPress theme). Child themes let you change small details of your site's appearance while still preserving the parent theme's appearance and functionality. The child theme is designed to inherit the features and appearance of the parent theme, while also enabling the user to make modifications/alterations to any part of the theme.

14. Explain WordPress Taxonomies.

WordPress Taxonomies group content or data together in WordPress. Shortly, it allows you to organize/group your posts based on what they have in common (characteristics). Several default taxonomies are available, and you can also design your own. WordPress's Categories and Tags are two of its most popular taxonomies.

15. Explain Category and Tags in WordPress. How to convert a category into a tag?

Categorization and tags play a much greater role in the content organization as well as SEO.

  • Categories: Essentially, categories are a general way to group content on a WordPress website. The idea of a category is to represent a topic or a group of topics that are somehow related to one another. A post can belong to several categories at once. Nevertheless, assigning more than two or three categories to one post is perhaps not the best idea.
  • Tags: Tags provide a good way to identify a piece of content by specific keywords. Just choose a few words that best describe a given post.

16. What are meta tags?

Meta tags are snippets of HTML code that describe the content of a website or a specific webpage. Search engines use these bits of data to determine what a page is about, and they provide the content that shows up in search results. Although meta tags still provide the description displayed in search engine results, they contribute greatly to improving CTRs (Click-Through Rates).

17 : What are disadvantages of WordPress?

  • Use of multiple plugins can make website heavy to load and slow
  • Only utilizes PHP
  • Sometimes updates can lead to loss of data, so you always need a backup copy
  • Modifying images and tables are difficult.
18 : What do you mean by the custom field 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.

19 : How to run database Query on WordPress?

WordPress’s query function allows you to execute any SQL query on the WordPress database. It is best used when there is a need for specific, custom, or otherwise complex SQL queries. For more basic queries, such as selecting information from a table, see the other wpdb functions above such as get_results, get_var, get_row or get_col.
Syntax

<?php $wpdb->query('query'); ?> 

Read more from https://codex.wordpress.org/Class_Reference/wpdb#Running_General_Queries

20 : List some action and filter hooks functions in WordPress?

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()
21 : unction to get website url in wordpress?
get_site_url(); function is used to get website url in wordpress. 
22 : 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.


23 : Which is the considerably best multilingual plugin in WordPress?
WPML is the best multilingual plugin for WordPress.

24 :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.

25 : What do you do when your WordPress website is hacked?

Below are the basic steps that can be taken when your WordPress website is hacked

  • Install security plugins like WP security
  • Re-install the latest version of WordPress
  • Change password and user-ids for all your users
  • Check if all your themes and plug-ins are up-to-date
  • Uninstall all plugins that are downloaded from untrusted places
26 : What are the hooks? Define different types of hooks in WordPress.

Action hooks: Action hooks facilitate you to insert an additional code from an outside resource.

Filter hooks: Filter hooks facilitate you to add content or text at the end of the post.


27 : In WordPress, objects are passed by value or by reference.
In WordPress, all objects are passed by value.

28 : How can you call a constructor for a parent class?
You can call a constructor for a parent class by this way:
Parents:: constructor($value)

29 : How many types of users WordPress have?

WordPress user role determines access permission to the users of a WordPress site.

  • Administrator: They have full rights over a site.
  • Editor: They deal with the content section of a website.
  • Author: They only deal with their posts. They can delete their post even after publishing.
  • Contributor: A contributor doesn't have the right to publish their post or page. They need to send it to the administrator for review.
  • Subscriber: Users who are subscribed to your site can log in and update their profile.
  • Follower: They don't have any right. They can only read and comment on your post.
  • Viewer: They can only read and comment on a post.
30 : How to optimize WordPress site performance?

Optimization increases the speed of your site and gives the best possible outcome. It improves the ranking of a website.

There are many tricks to optimize a WordPress site. Some of them are given below:

  • Use CDN
  • Use a caching plugin
  • Use a simple theme/framework
  • Keep site updated
  • Split long posts into smaller pages
31 : What are template tags in WordPress?
Template tags are used within themes to retrieve content from your database. It is a piece of code that tells WordPress to get something from the database.

32 : What are meta-tags in WordPress?
Meta-tags are keyword and description used to display website or page information.

33 : What are custom fields in WordPress?
Custom fields are also called post meta. It is a feature through which users can add arbitrary information when writing a post. These metadata can be displayed using template tags in WordPress themes.

After registering your menus, use wp_nav_menu() to inform your theme where to display your menus. You can add this code to a header.php file for displaying the registered header-menu:
wp_nav_menu( array( 'theme_location' => 'header-menu' ) );

Example

wp_nav_menu( array $args = array(
   'menu' => "",
   'menu_class' => "",
   'menu_id' => "",
   'container' => "",
   'container_class' => "",
   'container_id' => "",
   'fallback_cb' => "",
   'before' => "",
   'after' => "",
   'link_before' => "",
   'link_after' => "",
   'echo' => "",
   'depth' => "",
   'walker' => "",
   'theme_location' => "",
   'items_wrap' => "",
   'item_spacing' => "",
) );
// These are the parameters od this function.
35 : How to display custom Post in WordPress?

$args = array( 'post_type' => 'blog', 'posts_per_page' => 10 );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
   the_title();
   echo '<div class="entry-content">';
     the_content();
   echo '</div>';
endwhile;

36 : What is shortcode in WordPress? How we can use it?

Shortcodes in WordPress are specific codes that allow the developers to do various tasks with minimal effort. Shortcodes are used to simplify tasks such as creating objects or embedding files that require lots of coding in a single line. A Shortcode is synonymous to the shortcut. WordPress has five built-in shortcodes- audio, embed, caption, video, and gallery.

Example

1. You can call shortcode function like this :
echo do_shortcode('[display_related_posts]');
2. In your function.php you can make
function display_related_posts($array = array()) {
      return 'BestInterviewQuestion.com';
}
add_shortcode('display_related_posts', 'display_related_posts');
37 : How to add custom dynamic sidebars in WordPress?

A Sidebar in WordPress is an area where widgets can be used with a theme and this area is also termed as a Widgetized area. In WordPress, sidebar is created by using a function named as register_sidebar() function. The widget zed area should be registered in WordPress supported the theme as they are presentational. The sidebar function takes only one argument of an associative array of parameters that set the options for the widget area. Wrap $args array and register_sidebar() both are separate functions that can be called on ‘widgets_init’ hooks. In short, with the use of register_sidebar() method/ function users can add sidebars to WordPress. Syntax code for the creation of sidebar-

$args = array(
   'name' => __( 'Sidebar name', 'theme_text_domain' ),
   'id' => 'unique-sidebar-id',
   'description' => '',
   'class' => '', 'before_widget' => '<li id="%1$s" class="widget %2$s">',
   'after_widget' => '</li>',
   'before_title' => '<h2 class="widgettitle">',
   'after_title' => '</h2>'

);

38 : How does WordPress interact with databases?

There are many ways when WordPress can interact with databases like using functions like get_posts(), wp_get_post_terms(), wp_query(), and $wpdb(). From all these functions the most effective function to get a database table out of WordPress is the $wpdb() function. $wpdb is a global function and hence is declared with global keyword.

global $wpdb;

39 : How to Create a Widget in WordPress?

Widget is defined as a configurable code snippet which makes it possible to customize the functionality and appearance of a WordPress blog. Users can access the widgets page form the appearance menu effectively. Widgets in WordPress allow you to drag and drop the elements into the sidebar of your website. WordPress is featured with a standard set of widgets that you can use with any theme of WordPress. In order to create a customer widget in WordPress, it is important for the customers.

  • In order to create a site-specific plugin, paste the code functions .php file (theme’s file)
  • add your code to appearance >> widgets pages
  • register with ‘wpb_widget’ and load customer widgets
40 : What WordPress plugins do you use for SEO?

Here is a list of some best picks which we think will do the honors. Name any 3-4 and be sure to study about them in brief.

  • Yoast SEO
  • Google XML sitemaps
  • SEO framework
  • SEO squirrely
  • WP meta SEO
  • Rank Math
  • SEO press
  • AIOSEO (All In One SEO)
41 : What are the difference between action hook and filter in WordPress?
1. Actions Hook

Actions Hook are triggered by particular events that take place in WordPress such as changing themes, publishing a post, or displaying an administration screen. It is a custom PHP function defined in your plugin and hooked, i.e., set to respond, to some of these events.

Actions offently do one or more of the following things

  • Modify database data
  • Send an email message
  • Modify the generated administration screen or front end page sent to a user web browser.

Here are some Actions Functions listed

  • has_action()
  • do_action()
  • add_action()
  • remove_action() etc
2. Filters Hook

Filters Hook are functions that WordPress passes data through, at certain points in execution, just before taking some action with the data. It sits between the database and the browser and between the browser and the database; all most all input and output in WordPress pass through at least one filter hook.

The necessary steps to add your filters to WordPress are listed:

  • Create the PHP function that filters the data
  • Hook to the screen in WordPress, by calling add_filter()
  • Put your PHP function in a plugin file and activate it.

Here are some Filters Functions listed

  • has_filter()
  • doing_filter()
  • add_filter()
  • remove_filter() etc
42 : Make child theme:-

Step 1: Create a child theme folder.

Step 2: Create a stylesheet for your child theme.

Next, you’ll need to create a stylesheet file to contain all of the CSS rules and declarations for your child theme. To do so, create a new text file and name it “style.css.”

You’ll have to add a required header comment at the very top of the file in order for the stylesheet to actually work. This comment contains basic info about the child theme, including that it is a child theme with a particular parent theme.

You really only need to include two things: the theme name and template (ie. the parent theme’s name). You can include other information, including a description, author name, version, and tags. These additional details are important if you’re going to publish or sell your child theme.

Here’s an example of a complete header comment for a Twenty Twenty-One child theme:

 
/*

Theme Name: Twenty Twenty-One

Theme URI: https://example.com/twenty-twenty-one-child/

Description: Twenty Twenty-One Child Theme

Author: Anna Fitzgerald

Author URI: https://example.com

Template: twentytwentyone

Version: 1.0.0

License: GNU General Public License v2 or later

License URI: http://www.gnu.org/licenses/gpl-2.0.html

Tags: two-column, responsive-layout

Text Domain: twentytwentyonechild

*/

Notice the slashes and asterisks. These signify that this code will be “commented out” in CSS so WordPress doesn’t try to execute it.

You can add custom CSS later when you’re ready to begin customizing your child theme. For now, click Save so this stylesheet will be saved in your child theme’s directory.

Step 3: Enqueue the parent and child themes’ stylesheets.

Now it’s time to enqueue your parent and child themes’ stylesheets. This will ensure two things: First, that the child theme inherits its parent theme’s styling so when you activate your child theme, you’re not just looking at a bunch of unstyled text.

Second, that the child theme’s stylesheet is loaded before the parent theme’s — without overriding it. That way, once you add custom CSS and otherwise modify your child theme, these modifications will augment or replace certain styles and functions of the parent theme.

To do so, create another file in your child theme’s directory. Name it “functions.php” and add the following code:

 
<?php

add_action( 'wp_enqueue_scripts', 'enqueue_parent_styles' );

function enqueue_parent_styles() {

wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );

}

?>

Step 4: Install and activate your child theme.

You install a child theme the same way you install any theme. You have two options: you can copy the folder to the site using FTP, or create and upload a zip file of the child theme folder.

To upload the file, go to your WordPress dashboard and click on Appearance > Themes > Upload. Then, choose your child theme’s directory

Once it’s uploaded, click Activate.

Good news: your child theme is now live! The only problem is that it looks exactly like your parent theme. It’s time to customize.

Step 5: Customize your child theme.

To customize your child theme, you’ll likely start by adding CSS to the style.css file in your child theme’s directory. It’s one of the easiest ways to make changes to your theme. Whether you want to customize the color scheme, padding, typography, or other fundamental design elements of the parent theme, simply add code to your child theme’s stylesheet below the header comment. This code will override the code in your parent theme’s stylesheet.

To modify the functionality of the parent theme, on the other hand, you need to add functions to the functions.php file in your child theme’s directory.

For example, if you want to allow writers and other users to format their posts in different ways, then you can use the add_theme_support() function. To allow them to format posts as notes, links, a gallery of images, quotes, a single image, or video, then you’d add the following to your functions.php file in your child theme directory:

add_theme_support( 'post-formats', array ( 'aside', 'gallery', 'quote', 'image', 'video' ) );

You can put as many or as few functions as you want between the opening and closing PHP tags of your file.