Make WordPress Core


Ignore:
Timestamp:
09/30/2024 03:17:31 AM (19 months ago)
Author:
peterwilsoncc
Message:

Media: Add short-circuit filter to attachment_url_to_postid().

Introduces the filter pre_attachment_url_to_postid to allow developers to short-circuit the function attachment_url_to_postid().

The return values are expected to be an attachment ID, zero (0) to indicate no attachment was found or null to indicate the function should proceed as usual.

The function performs an expensive database query so developers making use of the function frequently may wish to use a custom table with appropriate indexes to reduce the load on their database server.

Props antpb, apermo, audrasjb, joedolson.
Fixes #61383.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r59008 r59118  
    53845384    global $wpdb;
    53855385
     5386    /**
     5387     * Filters the attachment ID to allow short-circuit the function.
     5388     *
     5389     * Allows plugins to short-circuit attachment ID lookups. Plugins making
     5390     * use of this function should return:
     5391     *
     5392     * - 0 (integer) to indicate the attachment is not found,
     5393     * - attachment ID (integer) to indicate the attachment ID found,
     5394     * - null to indicate WordPress should proceed with the lookup.
     5395     *
     5396     * Warning: The post ID may be null or zero, both of which cast to a
     5397     * boolean false. For information about casting to booleans see the
     5398     * {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}.
     5399     * Use the === operator for testing the post ID when developing filters using
     5400     * this hook.
     5401     *
     5402     * @param int|null $post_id The result of the post ID lookup. Null to indicate
     5403     *                          no lookup has been attempted. Default null.
     5404     * @param string   $url     The URL being looked up.
     5405     */
     5406    $post_id = apply_filters( 'pre_attachment_url_to_postid', null, $url );
     5407    if ( null !== $post_id ) {
     5408        return (int) $post_id;
     5409    }
     5410
    53865411    $dir  = wp_get_upload_dir();
    53875412    $path = $url;
Note: See TracChangeset for help on using the changeset viewer.