Make WordPress Core

Opened 10 years ago

Last modified 6 years ago

#19012 closed enhancement

get_ID and the_ID should accept $id as a parameter — at Version 3

Reported by: peterchester Owned by: ryan
Milestone: Priority: normal
Severity: normal Version: 3.3
Component: Posts, Post Types Keywords: needs-testing needs-codex has-patch
Focuses: Cc:

Description (last modified by markjaquith)

I see throughout the WordPress code as well as throughout many plugins and themes that get_ID() is often accompanied by a test to see if an ID had been passed. It would be a harmless but extremely useful update to simply allow get_ID() and the_ID() to accept an optional $id and pass it through if it's numerical.

I've already gone ahead and created the patch (attached)

Change History (3)

#1 @peterchester
10 years ago

It's pretty straight forward. Basically, I see a TON of repeated code all over the place that basically looks like this:

function myfunction ($post_id = null) {
	if ($post_id == null) {
		$post_id = get_ID();

I'm proposing that we abstract that effort to the get_ID function itself so that one could simply do this:

function myfunction ($post_id = null) {
	$post_id = get_ID( $post_id );

It should have no effect on existing code at all since it's an optional parameter, but moving forward it could be super helpful to everyone.

#2 @peterchester
10 years ago

  • Cc peter@… added

#3 @markjaquith
10 years ago

  • Description modified (diff)
  • Keywords needs-patch 2nd-opinion added; has-patch removed
  • Milestone changed from Awaiting Review to Future Release
  • Owner changed from peterchester to ryan
  • Status changed from new to reviewing

get_post() does something similar. It takes a post object, a post id, or nothing, and gives you a post object.

I wouldn't use is_numeric() though — too permissive.

Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus +0123.45e6 is a valid numeric value. Hexadecimal notation (0xFF) is allowed too but only without sign, decimal and exponential part.

Instead I'd say we cast to integer, and check for it being greater than zero. I'm also not sure it's useful for the_ID().

Note: See TracTickets for help on using tickets.