WordPress.org

Make WordPress Core

Opened 7 years ago

Closed 6 years ago

#5155 closed defect (bug) (fixed)

wp_tag_cloud('format=array') does not return array

Reported by: andrewflanagan Owned by: ryan
Milestone: 2.5 Priority: low
Severity: minor Version: 2.3
Component: Template Keywords: tags, tag cloud, array, has-patch
Focuses: Cc:

Description

wp_tag_cloud('format=array'); will always return "Array" since the array is not returned but it merely attempts to "echo" the array (which in PHP will just output "Array"). I don't think that this is the intended purpose...

I had thought that the purpose of the parameter "format=array" was to get an actual PHP array of all tags. I can get this tag array by using get_terms('post_tag', $args);

I tried to slog through the code a little bit (wp-includes/category-template.php line 310 and on) but it didn't seem to handle the special case of not "echo"ing a result, but merely returning an array object.

Attachments (2)

5155.diff (485 bytes) - added by Otto42 7 years ago.
5155.improved.diff (696 bytes) - added by westi 7 years ago.
Improved patch

Download all attachments as: .zip

Change History (11)

comment:1 andrewflanagan7 years ago

  • Summary changed from wp_get_tags to wp_tag_cloud('format=array') does not return array

comment:2 westi7 years ago

  • Owner changed from anonymous to ryan

I think wp_tag_cloud is only designed to print out the tag cloud either as a flat list or with UL's. The array format that wp_generate_tag_cloud supports should only be used in calls to wp_generate_tag_cloud which does return the array.

This is an array of html links for the tags.

I think if anything you are calling wp_tag_cloud incorrectly.

Assigning to ryan for more input on this.

comment:3 follow-up: Otto427 years ago

No, I gotta agree with the ticket. wp_generate_tag_cloud requires an input of tags from get_tags, which wp_tag_cloud does. Forcing them to use it directly adds more potential for error and doesn't necessarily allow for the result to always be the same data that wp_tag_cloud will create with the same parameters. So allowing format to be 'array' should return the array.

Anyway, it's an easy patch. Attached.

Otto427 years ago

comment:4 in reply to: ↑ 3 westi7 years ago

  • Milestone changed from 2.3.1 to 2.4

Replying to Otto42:

No, I gotta agree with the ticket. wp_generate_tag_cloud requires an input of tags from get_tags, which wp_tag_cloud does. Forcing them to use it directly adds more potential for error and doesn't necessarily allow for the result to always be the same data that wp_tag_cloud will create with the same parameters. So allowing format to be 'array' should return the array.

Anyway, it's an easy patch. Attached.

Thanks for that.

I guess I should have read the code a bit closer last night :-)

However that patch isn't quite correct I don't think.

Attaching a new one for testing.

westi7 years ago

Improved patch

comment:5 follow-up: Otto427 years ago

Why would you want to apply the wp_tag_cloud filters to an array?

Just wondering. Seems unlikely to me that any filters will be written to handle arrays properly there.

comment:6 in reply to: ↑ 5 westi7 years ago

Replying to Otto42:

Why would you want to apply the wp_tag_cloud filters to an array?

Just wondering. Seems unlikely to me that any filters will be written to handle arrays properly there.

I think we should always give the chance for a plugin to filter the output.

The plugin gets the $args string as a second argument to the filter call so it should be checking what it is getting before filtering anyway - otherwise it could break the output already if it applies filtering for flat displays to lists.

comment:7 santosj6 years ago

  • Component changed from General to Template

comment:8 hansengel6 years ago

  • Keywords has-patch added

comment:9 ryan6 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [6616]) Return, don't echo, array. Props Otto42. fixes #5155

Note: See TracTickets for help on using tickets.