Wordpress Interview Questions and Answers With Examples
1. What are WordPress plugins?
WordPress plugins are group of functions, which add new features in your wordpress website or blog. There are more than 50,000 plugins in wordpress directory. Lets assume you have a website and you want to include a form in your website, where visitor can send feedback. For this you can use a wordpress plugin like contact form 7, form builder or ninja form, which saves your time and money to develop or hire a developer.
Some popular wordpress plugins
- Contact Form 7
- Woocommerce
- SEO Yoast
- Jetpack
- Wordfence Security
- All in One SEO
- Google Analytics for Wordpress
2. Users in wordpress
In wordpress every user have a role, which have some collection of capabilities. Default their are 5 roles in wordpress.
Wordpress roles
- Subscriber: It is a default role of user when user registered. User with subsriber role have only access to update his/her own profile and read website or blog posts.
- Contributor: User with contributor role can create posts in wordpress which will stored in drafts.
- Author: User with author role can create, edit, delete their own posts.
- Editor: User with editor role can create, edit, delete their all posts. Editor have access to modify whole website content.
- Administrator: Administrator have all the access likes managing posts, users, plugins, website setting etc.
3. Tables in wordpress database
There are default 12 tables in database, when we installed wordpress. These tables are used to stored website info, content. Following are the descriptions of the database tables created during wordpress installation.
4. Check wheather current page is home or not
In wordpress their are 2 functions which are used to check wheather a page is front page or not. Those functions are: is_home(), is_front_page(). We will use these functions using && operator.
<?php
if(is_home() && is_front_page()){
echo 'This is website home page';
}
else{
echo 'not a home page';
}
?>
5. Function to get featured image in wordpress
In wordpress we can add featured image to posts and page. We can set one featured image of every post and page. To get that featured image of any post or page the_post_thumbnail() function is used. We can get featured images in different sizes using function: page the_post_thumbnail().
<?php
the_post_thumbnail('small');
the_post_thumbnail('thumbnail');
the_post_thumbnail('medium');
the_post_thumbnail('large');
?>
In wordpress we can also create custom sizes of images in wordpress. add_image_size() function is used to add custom featured image size in wordpress. Use below code to add custom featured image size in functions.php.
Add custom image size
<?php
add_image_size( 'customsize', 220, 220, true );
?>
Return image of custom size
<?php
the_post_thumbnail('customsize');
?>
6. Get logged in user details in wordpress
Their is an global variable $current_user and function wp_get_current_user() which are used in wordpress to get user details. But first we have to check wheather a user is logged in or not. Wordpress use own default function is_user_logged_in() to check wheather a user is logged in or not.
<?php
global $current_user;
wp_get_current_user();
if(is_user_logged_in()){
echo 'Name: '.$current_user->display_name;
print_r($current_user);
}
?>
7. Hooks in wordpress
Hooks are very useful in wordpress, which are used to add addtional information or update any default function behaviour without changing orignal files. Their are two types of hooks in wordpress:
- Action hooks: It will triggered at a specific time, when wordpress is running. It allows you to modify in a theme or plugin without changing any orignal file.
- Filter hooks: It is used to add content or text at the end of post or page.
Some filter hooks functions
- add_filter()
- apply_filters()
- remove_filter()
- current_filter()
- has_filter()
Some action hooks functions
- add_action()
- do_action()
- remove_action()
- doing_action()
- has_action()
8. Default prefix in wordpress
Default prefix is `wp_` wordpress database tables. Database tables prefix also defined in wp-config.php.
9. How to create ecommerce website with wordpress
No need to create any functionlity for it, just install a wordpress plugin `woocomerce`. After installed create products in it and add a payment gateway for recieve payments.
10. How to add css and js file in wordpress?
In Wordpress theme wp_enqueue_style() function is used to link a stylesheet and wp_enqueue_script() function is used to link a javascript file. These functions will call in add_action hook. Add below code in functions.php to link css file `custom.css` and js file custom.js to your activated theme.
In parent theme
<?php
function custom_func_to_add_script_and_style(){
wp_enqueue_style('custom-css', get_template_directory_uri( 'css/custom.css'));
wp_enqueue_script('custom-js', get_template_directory_uri( 'js/custom.js'));
}
add_action('wp_enqueue_scripts', 'custom_func_to_add_script_and_style');
?>
In child theme
<?php
function custom_func_to_add_script_and_style(){
wp_enqueue_style('custom-css', get_stylesheet_directory_uri('css/custom.css'));
wp_enqueue_script('custom-js', get_stylesheet_directory_uri('js/custom.js'));
}
add_action('wp_enqueue_scripts', 'custom_func_to_add_script_and_style');
?>
11. Create child theme in wordpress
Child theme is an very important feature of wordpress. We will lost our customization in parent theme after updating. With the help of child theme we can update functionality of parent theme without changing any file in parent theme and changes will not be deleted if our parent theme gets updated.
Steps to create child theme
- Step 1: Create a folder in themes directory. (If you theme name is `PhpEsperto` then your folder name must be `PhpEsperto-child` for better understanding in future)
- Step 2: Create a new file style.css and paste below code in it
/* Theme Name: PhpEsperto child Template: PhpEsperto */
- Step 3: Create a new file functions.php and paste below code in it
<?php add_action( 'wp_enqueue_scripts', 'enqueue_child_theme_styles', PHP_INT_MAX); function enqueue_child_theme_styles() { wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' ); } ?>
- Step 4: After that go to themes in admin dashboard and activate your child theme.
12. Widgets in wordpress
Widget is a block in wordpress which we can call anywhere on website or blog using function dynamic_sidebar(). In wordpress admin dashboard widgets are situated in dashboard -> appreance -> widgets. Every widget have a unique id. We can get widget using name of the widget used as a peremetre in dynamic_sidebar() function. There are many default widgets in wordpess, which we can simply drag them in right side widget blocks. Assume our widget name is `custom sidebar`, we will use below code to get `custom sidebar` widget block by name:
<?php
dynamic_sidebar('custom sidebar');
?>
13. Create custom template for custom post type
In wordpress, blog page used single.php template to display content. We can create different single blog templates for different custom post types. Use following steps to create template for custom post type, post type name is `gaming`.
- Step 1: Create a copy of template single.php and rename it to single-gaming.php (i.e. single-{post_type}.php)
- Step 2: Update wordpress permalinks, dashboard->settings->permalinks->save
- Step 3: After that template will auto assign to gaming post types
14. Create custom post type in wordpress
There are default 5 custom post types in wordpress: post, page, attachment, revision, nav_menu_item. Custom post types data stored in `wp_posts` and `wp_postsmeta` tables. There are many wordpress plugin which create and modify custom post types, but plugins make our website slow to load. So we can create our custom post type using add_action hook, in whichh we can pass post type aruments in register_post_type() function.
You can also create custom post type using our post type generator.
Add below code in functions.php file to create custom post type in your wordpress website or blog. Here i am going to create custom post types for `gaming`.
<?php
/*---------------------------Games post type here--------------*/
add_action('init', 'games');
function games() {
$labels = array(
'name' => _x( 'Games', 'post type general name' ),
'singular_name' => _x( 'Games', 'post type singular name' ),
'add_new' => _x( 'Add New', 'post' ),
'add_new_item' => __( 'Add New Games' ),
'edit_item' => __( 'Edit Games' ),
'new_item' => __( 'New Games' ),
'all_items' => __( 'All Games' ),
'view_item' => __( 'View Games' ),
'search_items' => __( 'Search Games' ),
'not_found' => __( 'No Games found' ),
'not_found_in_trash' => __( 'No Games found' ),
'parent_item_colon' => '',
'menu_name' => 'Games'
);
$args = array(
'labels' => $labels,
'description' => 'Games posts',
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'publicly_queryable' => true,
'menu_position' => 5,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true, 'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments' ),
'has_archive' => true,
'menu_icon' => ' dashicons-format-quote',
);
register_post_type( 'games', $args );
register_taxonomy("tax_games", 'games', array("hierarchical" => true, "label" => "Games Category",'show_admin_column' => true, "singular_label" => "Games_page Category","rewrite" => true) );
}
?>
15. Customise excerpt length for wordpress post
You can also create custom post type using our post type generator.
Exceprt is an highlighted part of post content. Function get_the_excerpt() is used to get the excerpt of the post and default words limit of excerpt is 55 words.
Add below code in functions.php file. I've changed excerpt limit to 55 words in below code, you can change it according to your requirements.
<?php
function custom_excerpt_length($length){
return 55;
}
add_filter('excerpt_length', 'custom_excerpt_length');
?>
16. Get posts of custom post type
In wordpress we can get posts using new WP_Query() function. Use below code to get posts from custom posts types.
<?php
$args = array(
'post_type'=> 'games',
'post_status' => 'publish',
'posts_per_page' => -1
);
$result = new WP_Query( $args );
if ( $result-> have_posts() ) : while ( $result->have_posts() ) : $result->the_post();
the_title();
the_excerpt();
echo "<a href='".get_the_permalink()."'>Read More</a>";
endwhile;
?>
17. Get lists of category
In wordpress we can get all categories using function get_categories(). Use below code to get all categories in wordpress.
<?php
$categories = get_categories();
foreach($categories as $category) {
echo '<li><a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a></li>';
}
?>
18. Create shortcode in wordpress
In wordpress we can get all categories using function get_categories(). Use below code to get all categories in wordpress.
Create shorcode
<?php
function myCustomFunction(){
return "xyz";
}
add_shortcode( 'my_shortcode', 'myCustomFunction' );
?>
Call shortcode
<?php
echo do_shortcode('[my_shortcode]');
?>
19. Send email in wordpress
In wordpress wp_mail() function is used to send an email with peremetres $to, $subject, strip_tags($message), $headers in wordpress.
<?php
wp_mail($to, $subject, $message, $headers);
?>
20. Get menu by name in wordpress
wp_nav_menu() function is used to get menu with argument `menu`. Use below code to get menu by name.
Create shorcode
<?php
wp_nav_menu('menu'=>'MENU NAME HERE');
?>
Latest Comments
Sana
29 Oct 2021