WordPress Developer 5 — Questions and Answers
Question 1: What is the correct way to localize a JavaScript variable for use with a WordPress-enqueued script?
- wp_localize_script() (Correct answer)
- wp_add_inline_script()
- wp_register_script()
- wp_script_add_data()
Correct answer: wp_localize_script()
wp_localize_script() outputs a JavaScript object before the enqueued script, making PHP data available in JavaScript.
Question 2: Which WordPress filter allows you to modify the SQL query generated by WP_Query before it runs?
- posts_request (Correct answer)
- pre_get_posts
- the_posts
- found_posts
Correct answer: posts_request
The posts_request filter receives the complete SQL query string just before execution, allowing raw SQL modification.
Question 3: In WordPress theme development, what is the purpose of get_template_part()?
- To include a reusable template file while supporting child theme overrides (Correct answer)
- To register a partial widget area in the theme
- To retrieve a specific part of the current page template
- To load a PHP file from the plugin directory
Correct answer: To include a reusable template file while supporting child theme overrides
get_template_part() loads template fragments and automatically checks child themes first, supporting theme inheritance.
Question 4: What WordPress database table stores taxonomy term relationships to posts?
- wp_term_relationships (Correct answer)
- wp_term_taxonomy
- wp_terms
- wp_postmeta
Correct answer: wp_term_relationships
wp_term_relationships links object IDs (posts, etc.) to term_taxonomy_id entries, creating the many-to-many relationship.
Question 5: Which WordPress function is used to redirect a user to another URL and exit script execution?
- wp_redirect() followed by exit() (Correct answer)
- wp_safe_redirect()
- header('Location: ...')
- wp_redirect() alone
Correct answer: wp_redirect() followed by exit()
wp_redirect() sends the Location header but requires an explicit exit() or die() call to stop further script execution.
Question 6: What is the main advantage of using add_action() over directly calling a function in WordPress?
- It allows code to execute at a specific point in the WordPress lifecycle without modifying core files (Correct answer)
- It automatically handles errors and retries if the function fails
- It caches the function result for better performance
- It restricts the function to admin users only
Correct answer: It allows code to execute at a specific point in the WordPress lifecycle without modifying core files
Hooks let you attach custom code to specific events in WordPress's execution flow, enabling extensibility without core file edits.
Question 7: Which method in the WordPress HTTP API is used to make a GET request to an external URL?
- wp_remote_get() (Correct answer)
- wp_http_get()
- wp_fetch_url()
- wp_get_request()
Correct answer: wp_remote_get()
wp_remote_get() performs an HTTP GET request and returns an array with response code, headers, and body.
What is the correct way to localize a JavaScript variable for use with a WordPress-enqueued script?