Understanding the core architecture is what separates a "site builder" from a WordPress Engineer. These questions cover the internal mechanisms of WordPress.
1. Explain the WordPress Request Lifecycle (Loading Sequence).
Answer: A high-level overview of what happens when a request hits WordPress:
- index.php: Entry point.
- wp-blog-header.php: Loads the environment.
- wp-load.php: Bootstraps WordPress (config, db).
- wp-settings.php: Loads core libraries, plugins, and the active theme's `functions.php`.
- init hook: Core is fully loaded.
- wp() (Main Query): Parses the URL and runs the main database query to determine what content to show.
- template_redirect: Decides which template file to load.
- template-loader.php: Loads the template (e.g., `single.php`).
- Shutdown: PHP execution ends.
2. What is the difference between `actions` and `filters`?
Answer: Both are "Hooks", but they serve different purposes:
- Actions (`do_action` / `add_action`): Events where you can "do" something at a specific point (e.g., echo HTML, send an email, update DB). They do not return a value.
- Filters (`apply_filters` / `add_filter`): Events where you modify data and must return it. (e.g., changing the post title, modifying a query).
// Action: Do something
add_action('wp_footer', function() {
echo '<!-- Analytics Code -->';
});
// Filter: Modify data
add_filter('the_title', function($title) {
return 'Prefix: ' . $title;
});
3. Describe the WordPress Database Schema.
Answer: WordPress uses a relational database (MySQL/MariaDB/PostgreSQL) with 12 core tables (by default). The most important ones are:
wp_posts: Stores posts, pages, attachments, nav menu items, and revisions.wp_postmeta: Stores metadata for posts (EAV model).wp_users&wp_usermeta: User data.wp_options: Global site settings.wp_terms,wp_term_taxonomy,wp_term_relationships: Categories and tags.
Key Concept: The EAV (Entity-Attribute-Value) model in `wp_postmeta` allows for flexibility but can be a performance bottleneck if not queried correctly.
4. What is the Template Hierarchy?
Answer: It is the logic WordPress uses to decide which theme file to use to render a page. It searches from specific to generic.
Example for a Single Post:
single-{post_type}-{slug}.phpsingle-{post_type}.phpsingle.phpsingular.phpindex.php(Fallback)
5. What are Transients?
Answer: Transients are a way to store cached data in the database (wp_options) with an expiration time. They are similar to Options but are temporary.
Usage: Storing the result of an expensive API call or complex query.
if ( false === ( $data = get_transient( 'my_api_data' ) ) ) {
$data = fetch_api_data();
set_transient( 'my_api_data', $data, 12 * HOUR_IN_SECONDS );
}
6. Compare `WP_Query` vs `get_posts()` vs `query_posts()`.
Answer:
- `WP_Query`: The class behind all queries. It is the most powerful and customizable way to query the database. It is safe to use anywhere.
- `get_posts()`: A wrapper around `WP_Query` that returns an array of post objects (not a loop). It suppresses filters by default and is slightly faster for simple retrievals.
- `query_posts()`: NEVER USE THIS. It overrides the main query loop and causes significant issues with pagination and page state. It is deprecated in practice.
7. When should you use a Custom Post Type vs. a Custom Taxonomy?
Answer: This is an architecture question.
- Custom Post Type (CPT): Use this for Entities or "Nouns" that have their own content and lifecycle. Examples: Movies, Products, Events, Team Members.
- Custom Taxonomy: Use this for Grouping or "Adjectives" that describe those entities. Examples: Genre (for Movies), Color (for Products), Department (for Team Members).
Rule of Thumb: If you need to write a paragraph about it, it's a Post Type. If it's just a label to sort by, it's a Taxonomy.
8. What is the Singleton Pattern and where is it used in WordPress?
Answer: The Singleton pattern ensures a class has only one instance and provides a global point of access to it.
WordPress Examples:
$wpdb: The global database connection object.$wp_rewrite: Manages rewrite rules.- Many plugins use a `getInstance()` method to prevent multiple initializations of their main class, which avoids hook duplication.
9. What is the Rewrite API?
Answer: The Rewrite API allows you to create custom URL structures (permalinks) that map to specific WordPress queries.
Key Functions:
add_rewrite_rule(): Adds a new rule to the top of the rewrite list.add_rewrite_tag(): Registers a query variable so WordPress knows to look for it.flush_rewrite_rules(): Regenerates the `.htaccess` file or database rules. Warning: Only run this on plugin activation/deactivation, never on every page load (performance killer).
10. Explain the WordPress Cron system.
Answer: WP-Cron is a pseudo-cron system. It is not a real system cron job. It runs when a visitor loads a page.
Limitation: If no one visits the site, scheduled tasks (like publishing scheduled posts) won't run.
Best Practice: Disable the default WP-Cron (`DISABLE_WP_CRON`) and set up a real system cron job to call `wp-cron.php` every minute.
11. What is the REST API and how do you extend it?
Answer: The REST API provides a JSON interface to WordPress content. It allows you to build decoupled applications (Headless WP).
Extending: Use `register_rest_route()` to create custom endpoints.
add_action( 'rest_api_init', function () {
register_rest_route( 'myplugin/v1', '/data', array(
'methods' => 'GET',
'callback' => 'my_custom_callback',
'permission_callback' => function () { return current_user_can( 'edit_posts' ); }
) );
} );
12. What are Nonces?
Answer: "Number used ONCE". They are security tokens used to protect URLs and forms from misuse (CSRF attacks).
Usage:
- Create: `wp_create_nonce('action_name')`
- Verify: `wp_verify_nonce($_REQUEST['_wpnonce'], 'action_name')`
Note: In WordPress, nonces are valid for 24 hours by default, so they are not strictly "once", but they are user-specific.
13. How does the `WP_Query` class work?
Answer: `WP_Query` is the class behind all post queries. The main loop uses a global instance of it.
Key Methods:
- `have_posts()`: Checks if there are posts to loop through.
- `the_post()`: Sets up the global `$post` object and advances the counter.
- `wp_reset_postdata()`: Restores the global `$post` to the main query after a secondary loop.