Interview FAQ WP Interview FAQ

This article will cover all the relevant WordPress questions with answers that are frequently asked in an interview for plugin development and customization

What is a plugin?

Plugin is a collection of function that enhance the functionality of WordPress. For Example contact form 7,gravity forms plugin can be used to add form to WordPress. Woo-Commerce for selling products.

How to create a plugin?

To start a plugin create a folder in /wp-content/plugin/unique-folder-name

in this folder create unique-plugin-name.php file

<?php
/**
 * Plugin Name: Unique Plugin Name
 * Plugin URI: https://curiouswebs.in/unique-plugin-name
 * Description: This is basic plugin
 * Version: 1.0
 * Author: curious webs
 * Author URI: https://curiouswebs.in/
 */

After that according to plugin requirement

  • add css/js files
  • create option page
  • write action,filters
How to run SQL query in WordPress?

using global object $wpdb;

  • $wpdb->insert
  • $wpdb->replace
  • $wpdb->update
  • $wpdb->delete
  • $wpdb->query
  • $wpdb->prepare
What is mu-plugins?
  • They are must use plugins and are located in /wp-content/plugins/mu-plugins folder.
  • They are activated by default. User do not need to activated them
  • They cannot be disabled directly from the admin panel except plugin file need to be removed from the directory.
  • They are automatically enabled on all sites of multisites.
  • Example : Create showGreetings.php in /wp-content/plugins/mu-plugins/showGreetings.php
  • in showGreetings.php write
add_filter( 'the_content', function( $content ) {
    return 'Greetings!!'.$content;
} );
How to override plugin file in theme?

To override plugin file in theme simply copy that file in your theme with the same structure as that of the plugin folder . For eg. wp-content/plugins/myplugin/custom_file.php then in theme create a folder myplugin/custom_file.php

Name WordPress global variable?

$post is mostly used global variable in WordPress theme. It holds the information of the post name,date,author,description etc.

<?php
global  $post;
print_r ($post); 

$wpdb is a global object and it is instantiated to talk to the wordpress database

<?php
global $wpdb;
$results = $wpdb->get_results( "SELECT * FROM  {$wpdb->prefix}_postmeta", OBJECT ); 
How to use Ajax in plugin?

To use Ajax in plugin or theme

  1. include admin_ajax file in theme/plugin
  2. Using wp_ajax action work with ajax
  3. Send data to the wordpress action
  4. get the result
 <script>var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";</script> 
<script> 
var str = '&str=some_data'&action=my_ajax_action';
        $.ajax({
        data: str,
                dataType: "html", // html,xml,json whatever
                type: 'post', // get or post
                url: ajaxurl,
                beforeSend: function () {
                // what will it do before loading the content..
                },
                success: function (data) {
                   console.log(data); // do something with data
            }
        }); 
</script>
<?php
 function  my_ajax_action() { 
$str = $_POST['str'];
// do something with data

die; // otherwise will return  in the end
}
add_action('wp_ajax_my_ajax_action ', 'my_ajax_action ');
add_action('wp_ajax_my_ajax_action ', 'my_ajax_action '); 
Explain how the data is stored in the database.

Here is how WordPress store its data in SQL tables

wp_optionsBasic details like site_url,home_url, blogname,blogdescription,admin_email,mail_server information etc.
wp_usersuser name, id,display name, email,status etc.
wp_usermetaall other field information of the user eg first name , last name, description etc.
wp_poststore the information of post id,title ,post_type,revisions,attachement etc.
wp_postmetainformation of the post meta , meta box’s post field and post value
wp_termscategories and tags for pages or posts
wp_term_relationshipsconnetion between posts and categories tags etc.
wp_termmetametadata or meta fields values of taxonomy
wp_term_taxonomyterms related to the wp_terms and their parent information
wp_commentscomment id, author,date,content,agent parent etc.
wp_commmentmetacomment meta fileds value
wp_linkslink url,name,target,description etc.
How to include css/js in plugin?

using wp_enqueue_style()

<?php 
wp_enqueue_style( 'navigation', get_template_directory_uri() . '/css/navigation.css','1.1'); 

using wp_enqueue_script()

<?php 
wp_enqueue_script( 'slider', get_template_directory_uri() . '/js/slider.js','1.1');  
How to include a file in plugin?
<?php  include( plugin_dir_path( __FILE__ ) . 'payment/stripe.php');
What is the plugin structure?

Here is the basic structure of plugin

/wp-content
    /wp-plugins
       /plugin-name
          plugin-name.php      
          uninstall.php 
          license.php
       /languages      
       /includes 
         shortcodes.php
         post-types.php
         widgets.php
         sidebar.php
         /classes
       /admin           
         /js           
         /css           
         /images  
          admin_headers.php
          admin_blocks.php
          admin_options.php    
      /public           
         /js           
         /css           
         /images 
         /templates
           
Name some plugins?
  • woocomerce
  • contact form 7
  • ninja forms
  • custom post type UI
  • ACF Pro
  • Akismet
  • toolset
  • Duplicator
  • Quiz maker
  • Wp forms lite
  • BB Press
What is metabox?

WordPress meta box allows to add extra fields to a post or page. Here is the basic example how to create meta box, meta fields in it, save theme and get meta field value.

<?php
// adding meta box
add_action('admin_init', 'meta_box_for_service'); 
function meta_box_for_service() {   
  add_meta_box(          
                  'cs_service',     // unique id    
                  'Service Details', // meta box title       
                  'display_service_fields',   // content callback      
                  'service'     //post type   
                          
  ); 
} 


// display content to the meta box
function  display_service_fields(){
<label for="service_name">Service Name</label>
<select name="service_name" id="service_name">
<option value="">Digital Marketing</option>
<option value="something">Web Development</option>
<option value="else">Others</option>
</select> 

}

// Note : no submit button : because on click of update button it will save the values.

// saving values

function save_service_postmeta($post_id)
{
   if (array_key_exists('service_name', $_POST)) {
         update_post_meta(
              $post_id,
              '_service_name_meta_key',
              $_POST['service_name']
         );
   }
}
add_action('save_post', 'save_service_postmeta '); 

// getting value

 $service_name  = get_post_meta($post->ID, ' _service_name_meta_key', true); 
How to increase upload size of media?

via php.ini file

upload_max_filesize = 12M
post_max_size = 13M
memory_limit = 15M

via .htaccess file

php_value upload_max_filesize 12M
php_value post_max_size 13M
php_value memory_limit 15M

via wp-config.php file

@ini_set( 'upload_max_size' , '12M' );
@ini_set( 'post_max_size', '13M');
@ini_set( 'memory_limit', '15M' );

via upload_size_limit() filter

How to add menu in admin sidebar?

To create a custom menu page

 <?php 

/**
* Register a custom menu page.
*/
function register_my_custom_menu_page() {
      add_menu_page(
                   __( 'Custom Menu Title', 'textdomain' ),
                   'custom menu',
                   'manage_options',
                   'my_custom_menu_page',
                   '',
                   plugins_url( 'myplugin/images/icon.png' ),
                   6
     );
}
add_action( 'admin_menu', 'register_my_custom_menu_page' ); 

/**
* Display a custom menu page
*/
function my_custom_menu_page(){
       esc_html_e( 'Admin Page Test', 'textdomain' );  
} 

These are some of the questions related to plugin that can be asked in an interview. If you find this article useful please leave a feedback. Also you can tell what other questions you have faced in an interview?

Leave a comment