WordPress Developer 2 — Questions and Answers
Question 1: Which WordPress function retrieves a post's meta value for a given key?
- get_post_meta() (Correct answer)
- get_option()
- get_user_meta()
- get_term_meta()
Correct answer: get_post_meta()
get_post_meta($post_id, $key, $single) retrieves post meta values stored in the wp_postmeta table.
Question 2: What is the correct way to enqueue a script that depends on jQuery in WordPress?
- wp_enqueue_script('my-script', get_template_directory_uri().'/js/script.js', array('jquery'), '1.0', true) (Correct answer)
- wp_register_script('my-script', '/js/script.js')
- add_action('wp_head', 'my_script_function')
- wp_add_script('my-script', array('jquery'))
Correct answer: wp_enqueue_script('my-script', get_template_directory_uri().'/js/script.js', array('jquery'), '1.0', true)
wp_enqueue_script() with 'jquery' in the dependencies array ensures jQuery loads before your script.
Question 3: In the WordPress Loop, which function outputs the current post's title as HTML?
- the_title() (Correct answer)
- get_the_title()
- post_title()
- echo the_title()
Correct answer: the_title()
the_title() directly echoes the post title, while get_the_title() returns it without outputting.
Question 4: Which WordPress hook fires after all plugins have been loaded but before theme setup?
- plugins_loaded (Correct answer)
- init
- after_setup_theme
- wp_loaded
Correct answer: plugins_loaded
plugins_loaded fires after all active plugins are loaded, giving access to all plugin functions before init.
Question 5: What does the $wpdb->prepare() method primarily protect against?
- SQL injection attacks (Correct answer)
- Cross-site scripting (XSS)
- CSRF attacks
- Brute force login attempts
Correct answer: SQL injection attacks
$wpdb->prepare() sanitizes values in SQL queries using sprintf-style placeholders to prevent SQL injection.
Question 6: Which template hierarchy file takes precedence for a single post of custom post type 'product'?
- single-product.php (Correct answer)
- single.php
- singular.php
- post-product.php
Correct answer: single-product.php
WordPress looks for single-{post_type}.php first, so single-product.php takes precedence over single.php.
Question 7: What is the purpose of the nonce in WordPress form handling?
- To verify that a request was intentionally made by a user from your site (Correct answer)
- To hash the user's password before submission
- To encrypt POST data in transit
- To generate a unique session ID for the user
Correct answer: To verify that a request was intentionally made by a user from your site
WordPress nonces are security tokens that verify the source and intent of requests, protecting against CSRF attacks.
Which WordPress function retrieves a post's meta value for a given key?