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
<?phpglobal
$wpdb;
$results
= $wpdb->get_results( "SELECT * FROM
{$wpdb->prefix}_postmeta", OBJECT );
How to use Ajax in plugin?
To use Ajax in plugin or theme
- include admin_ajax file in theme/plugin
- Using wp_ajax action work with ajax
- Send data to the wordpress action
- 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_options | Basic details like site_url,home_url, blogname,blogdescription,admin_email,mail_server information etc. |
wp_users | user name, id,display name, email,status etc. |
wp_usermeta | all other field information of the user eg first name , last name, description etc. |
wp_post | store the information of post id,title ,post_type,revisions,attachement etc. |
wp_postmeta | information of the post meta , meta box’s post field and post value |
wp_terms | categories and tags for pages or posts |
wp_term_relationships | connetion between posts and categories tags etc. |
wp_termmeta | metadata or meta fields values of taxonomy |
wp_term_taxonomy | terms related to the wp_terms and their parent information |
wp_comments | comment id, author,date,content,agent parent etc. |
wp_commmentmeta | comment meta fileds value |
wp_links | link 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 boxadd_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 functiondisplay_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 valuesfunction
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?