If you a code focused WordPress developer then you must know about WordPress Reserved Terms, Query Vars and Global Variables. Sometimes without knowing these terms and their actual safe use you may face wrong results than your required output and may kill your valuable time why something is not working.
If you are developing a WordPress theme or plugin, you may have encountered some terms that are reserved by WordPress and should not be used in certain contexts. These terms are called reserved terms, and they can cause conflicts with core functionality if used incorrectly. In this blog post, we will explain what reserved terms are, how to avoid them, and how to use query vars and global variables instead.
What are reserved terms?
Reserved terms are words or phrases that WordPress uses internally for various purposes, such as passing parameters through $_GET or $_POST arrays, registering taxonomies or post types, or handling query variables. For example, some of the reserved terms are:
<code class="language-php"> - action - attachment - author - category - post - tag - term </code>
You can find a complete list of reserved terms in the WordPress Codex.
Why should you avoid reserved terms?
Using reserved terms in your theme or plugin can cause unexpected behavior or errors, because WordPress may interpret them differently than you intended. For example, if you register a custom post type with the slug ‘post’, WordPress will confuse it with the built-in post type and may not display your custom posts correctly. Similarly, if you pass a parameter named ‘category’ through a $_GET or $_POST array, WordPress may think you are trying to filter posts by category and may ignore your parameter.
How to avoid reserved terms?
The best way to avoid reserved terms is to use unique and descriptive names for your theme or plugin features. For example, instead of using ‘post’ as a custom post type slug, you could use something like ‘my_post’ or ‘custom_post’. Instead of using ‘category’ as a parameter name, you could use something like ‘my_category’ or ‘custom_category’.
Another way to avoid reserved terms is to use prefixes or namespaces for your theme or plugin features. For example, if your theme or plugin name is ‘My Theme’, you could prefix all your features with ‘my_theme_’ or use a namespace like ‘My_Theme’. This way, you can avoid conflicts with WordPress core features or other themes or plugins.
A changelog is a document that records the changes made to a product over time. It typically includes information such as the date of the change, the version number, the author, and a brief description of what was added, modified, or removed. A changelog helps users and developers keep track of the product’s evolution and identify any issues or bugs that may arise from the updates. A changelog also serves as a communication tool between the product team and the stakeholders, as it showcases the progress and achievements of the project.
Query Vars & Global Variables
What are query vars and global variables?
Query vars and global variables are two ways of accessing data that WordPress generates or stores. Query vars are variables that WordPress uses to parse and build URLs, such as ‘p’ for post ID, ‘s’ for search term, or ‘cat’ for category ID. Global variables are variables that WordPress sets in the global scope and can be accessed from anywhere in your code, such as $wp_query for the main query object, $post for the current post object, or $wpdb for the database object.
How to use query vars and global variables?
To use query vars and global variables in your theme or plugin, you first need to declare them as global in your code. For example:
<code class="language-php"> global $wp_query; global $post; </code>
Then, you can access their properties and methods as usual. For example:
<code class="language-php"> echo $wp_query->found_posts; // prints the number of posts found by the main query echo $post->post_title; // prints the title of the current post </code></pre You can also set or modify query vars and global variables in your code, but you should be careful not to break core functionality or other themes or plugins. For example: <pre><code class="language-php"> $wp_query->set( 'posts_per_page', 5 ); // sets the number of posts per page for the main query $post->post_content = 'Hello World'; // changes the content of the current post </code>
You should always use the appropriate API functions when available, instead of modifying query vars and global variables directly. For example:
<code class="language-php"> query_posts( array( 'posts_per_page' => 5 ) ); // alters the main query using query_posts() the_content( 'Hello World' ); // filters and displays the content of the current post using the_content() </code>
In this blog post, we have learned what reserved terms are, how to avoid them, and how to use query vars and global variables instead. We hope this will help you develop better themes and plugins for WordPress.
We developed lots of WordPress plugins and sometimes we needed to declare our own query var. If you work with custom permalink you may need to declare query vars.