APIs and Advanced Development Flashcards
Q: What is an API?
A: An Application Programming Interface (API) allows applications to communicate with each other by defining methods and data structures.
Q: What is the WordPress REST API?
A: The WordPress REST API provides endpoints for developers to interact with WordPress sites, enabling data to be accessed and manipulated remotely.
Q: How do you enable the WordPress REST API?
A: The REST API is enabled by default in WordPress; you can access it via /wp-json/ on your site.
Q: What is an endpoint in an API?
A: An endpoint is a specific URL where an API interacts with data (e.g., /wp-json/wp/v2/posts for retrieving posts).
Q: What authentication methods are commonly used with the WordPress REST API?
A: Basic Authentication, OAuth, Application Passwords, and JWT (JSON Web Tokens).
Q: How does Basic Authentication work with the REST API?
A: It requires a username and password sent in the request header for API access.
Q: What are Application Passwords in WordPress?
A: Application Passwords allow secure API authentication for individual users without sharing actual passwords.
Q: Why should HTTPS be used when authenticating API requests?
A: To ensure secure transmission of credentials and protect against data interception.
Q: What are some default endpoints provided by the WordPress REST API?
- /wp/v2/posts: For retrieving or managing posts.
- /wp/v2/users: For managing users.
- /wp/v2/taxonomies: For working with taxonomies.
- /wp/v2/media: For managing media uploads.
Q: How do you fetch all posts using the REST API?
A: Send a GET request to /wp-json/wp/v2/posts.
Q: How do you create a new post using the REST API?
A: Send a POST request to /wp-json/wp/v2/posts with the required data (e.g., title, content) and authentication.
Q: What is the difference between GET, POST, PUT, and DELETE in API requests?
- GET: Retrieve data.
- POST: Create new data.
- PUT: Update existing data.
- DELETE: Remove data.
Q: How do you create a custom REST API endpoint?
A: Use the register_rest_route() function in your plugin or theme.
Q: What parameters are required in register_rest_route()?
A: The namespace, route, and a callback function to handle the request.
Q: How do you add parameters to a custom REST API endpoint?
A: Define args in the route options, specifying parameter types and validation rules.
Q: How do you restrict access to a custom REST API endpoint?
A: Use permission callback functions in register_rest_route() to check user capabilities.