Filter hook 'pre_attachment_url_to_postid'

in WP Core File wp-includes/media.php at line 5514

View Source

pre_attachment_url_to_postid

Filter Hook
Description
Filters the attachment ID to allow short-circuit the function. Allows plugins to short-circuit attachment ID lookups. Plugins making use of this function should return: - 0 (integer) to indicate the attachment is not found, - attachment ID (integer) to indicate the attachment ID found, - null to indicate WordPress should proceed with the lookup. Warning: The post ID may be null or zero, both of which cast to a boolean false. For information about casting to booleans see the {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}. Use the === operator for testing the post ID when developing filters using this hook.

Hook Information

File Location wp-includes/media.php View on GitHub
Hook Type Filter
Line Number 5514

Hook Parameters

Type Name Description
int|null $post_id The result of the post ID lookup. Null to indicate no lookup has been attempted. Default null.
string $url The URL being looked up.

Usage Examples

Basic Usage
<?php
// Hook into pre_attachment_url_to_postid
add_filter('pre_attachment_url_to_postid', 'my_custom_filter', 10, 2);

function my_custom_filter($post_id, $url) {
    // Your custom filtering logic here
    return $post_id;
}

Source Code Context

wp-includes/media.php:5514 - How this hook is used in WordPress core
<?php
5509  	 *
5510  	 * @param int|null $post_id The result of the post ID lookup. Null to indicate
5511  	 *                          no lookup has been attempted. Default null.
5512  	 * @param string   $url     The URL being looked up.
5513  	 */
5514  	$post_id = apply_filters( 'pre_attachment_url_to_postid', null, $url );
5515  	if ( null !== $post_id ) {
5516  		return (int) $post_id;
5517  	}
5518  
5519  	$dir  = wp_get_upload_dir();

PHP Documentation

<?php
/**
	 * Filters the attachment ID to allow short-circuit the function.
	 *
	 * Allows plugins to short-circuit attachment ID lookups. Plugins making
	 * use of this function should return:
	 *
	 * - 0 (integer) to indicate the attachment is not found,
	 * - attachment ID (integer) to indicate the attachment ID found,
	 * - null to indicate WordPress should proceed with the lookup.
	 *
	 * Warning: The post ID may be null or zero, both of which cast to a
	 * boolean false. For information about casting to booleans see the
	 * {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}.
	 * Use the === operator for testing the post ID when developing filters using
	 * this hook.
	 *
	 * @since 6.7.0
	 *
	 * @param int|null $post_id The result of the post ID lookup. Null to indicate
	 *                          no lookup has been attempted. Default null.
	 * @param string   $url     The URL being looked up.
	 */
Quick Info
  • Hook Type: Filter
  • Parameters: 2
  • File: wp-includes/media.php
Related Hooks

Related hooks will be displayed here in future updates.