Day 34: an overview of Transients API, HTTP API and Rewrite API

Transient API is similar to options API. The only added benefit is adding the ‘expiration time’ to easily handle the cache.

It uses wp_options table to temporarily store the cached information.

“Transients are inherently sped up by caching plugins, where normal Options are not”.

Transient expiration times are the maximum time. There is no minimum age. Transients might disappear one second after you set them, or 24 hours, but they will never be around after the expiration time.

Function of Transient

Please note: Transient name. Expected to not be SQL-escaped.
Must be 172 characters or fewer in length.

Set/Get Transient

1. get_transient(): Sets/updates the value of a transient.

set_transient( string $transient, mixed $value, int $expiration ): bool

2. set_transient(): Retrieves the value of a transient.

get_transient( string $transient ): mixed

3. set_site_transient(): Sets/updates the value of a site transient. It will automatically serialize the value if needed before saving it.

set_site_transient( string $transient, mixed $value, int $expiration ): bool

4. get_site_transient(): Retrieves the value of a site transient. It will return the boolean false if the value or key does not exist or expires.

get_site_transient( string $transient ): mixed
Delete Transient

1. delete_transient(): Deletes a transient.

delete_transient( string $transient ): bool

2. delete_site_transient(): Deletes a site transient.

delete_site_transient( string $transient ): bool

Common Headers in an HTTP

  • x-ratelimit-limit – Number of requests allowed in a time period
  • x-ratelimit-remaining – Number of remaining available requests in time period
  • content-length – How large the content is in bytes. Can be useful to warn the user if the content is fairly large
  • last-modified – When the resource was last modified. Highly useful to caching tools
  • cache-control – How should the client handle caching

Rewrite API

The Rewrite API serves as the backbone of this functionality, offering developers a robust framework to manipulate and organize URL structures.

Storage Mechanism:

The storage mechanism of rewrite rules in WordPress revolves around a set of regular expressions that map URLs to their corresponding query parameters. These rules are stored in the rewrite_rules option in the WordPress database, which serves as a repository for all custom and default rewrite rules defined by themes, plugins, and WordPress itself.

Properties of Rewrite Rules:

  1. Pattern:
  • Defines the URL pattern to match against incoming requests.
  • Example: /category/(.+)/?$
  1. Query Parameters:
  • Specifies the corresponding query parameters to be passed to WordPress.
  • Example: index.php?category_name=$matches[1]
  1. Rewrite Tags:
  • Acts as placeholders in rewrite rules to capture dynamic parts of URLs.
  • Example: $matches[1]

Methods and Functions Used in Rewrite API:

  1. add_rewrite_rule() Function:
  • Purpose: Registers a new rewrite rule with WordPress.
  • Example:
    php add_rewrite_rule('^category/([^/]+)/?$', 'index.php?category_name=$matches[1]', 'top');
  1. flush_rewrite_rules() Function:
  • Purpose: Flushes and regenerates the rewrite rules to ensure they are up to date.
  • Example:
    php flush_rewrite_rules();
  1. WP_Rewrite Class:
  • Purpose: Represents the main class for managing rewrite rules in WordPress.
  • Example:
    php global $wp_rewrite;
  1. add_rewrite_tag() Function:
  • Purpose: Adds a new rewrite tag to the list of recognized tags.
  • Example:
    php add_rewrite_tag('%category_name%', '([^/]+)');
  1. generate_rewrite_rules() Method:
  • Purpose: Generates rewrite rules based on registered permalink structures and query variables.
  • Example:
    php $rules = $wp_rewrite->generate_rewrite_rules('/%category%/%postname%/');
  1. rewrite_rules_array Filter Hook:
  • Purpose: Allows modification of the array of rewrite rules before they are processed by WordPress.
  • Example:
    php add_filter('rewrite_rules_array', 'custom_rewrite_rules'); function custom_rewrite_rules($rules) { // Modify $rules array return $rules; }

Advantages of Using Rewrite API:

  1. Flexibility: Provides developers with the flexibility to define custom URL structures and rewrite rules tailored to their specific needs.
  2. SEO-Friendly URLs: Enables the creation of SEO-friendly permalinks that enhance the discoverability and ranking of WordPress sites on search engines.
  3. Cleaner Code: Organizes URL management logic within WordPress, leading to cleaner and more maintainable codebases.

Disadvantages of Using Rewrite API:

  1. Complexity: Manipulating rewrite rules and understanding their interactions can be complex, especially for novice developers.
  2. Potential for Errors: Incorrectly defined rewrite rules or conflicting rules may lead to unexpected behavior and errors on the site.

Leave a Reply

Your email address will not be published. Required fields are marked *