WordPress.org

Make WordPress Core

Opened 7 weeks ago

Last modified 4 weeks ago

#43949 new enhancement

Need filter in get_metadata

Reported by: fitehal Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Plugins Keywords: has-patch reporter-feedback
Focuses: Cc:

Description

When accessing get_post_meta('ID_HERE')

Need to filter the output array.

Existing get_{$meta_type}_metadata filter works before fetching data.

The filter required before returning or Processing value of $meta_cache

Attachments (1)

43949.diff (1.5 KB) - added by samirmalpande 7 weeks ago.

Download all attachments as: .zip

Change History (5)

@samirmalpande
7 weeks ago

#1 @samirmalpande
7 weeks ago

  • Keywords has-patch added; needs-patch removed

Added filter to get_metadata function called get_{$meta_type}_meta_cache

#2 @johnbillion
7 weeks ago

  • Component changed from General to Plugins
  • Keywords reporter-feedback added
  • Version trunk deleted

@samirmalpande Thanks for the patch!

@fitehal Can I ask what your use case is for this filter please?

#3 @fitehal
7 weeks ago

Hi @johnbillion,

Currently, there is no way to manipulate the metadata dynamically. If we are creating a theme or plugin it is not required. We can manipulate the data after I have fetched it from the database. But when we are extending or building upon some existing plugin which stores data in meta table not being able to add or remove some fields based on some login is not possible.

Existing get_{$meta_type}_metadata filter works before fetching data. If we want to do some data manipulation based on the value stored in the database it is not possible or it is difficult. There is no simple way or what we can call WordPress way to achieve it.

Another issue is that get_{$meta_type}_metadata filter will work only if the key is specified. If the user calls get_post_meta($post_id) without passing the key, the above filter is not called and is not of any use.

We were trying to extend a plugin which stores data in wp_postmeta table. We have added about 30 custom fields through the plugin on 1 page. We wanted those fields to with some default values set to get loaded on all pages. Doing it manually would take a lot of time and if we add fields directly to the database they will be available only to existing posts and not any new post we create.

While trying to solve this problem first thing that comes to our mind was there must be some kind of hook on get_post_meta function and we can use it to push our custom fields to an array which is returned from the database that way we can have the fields with default value without having to insert them in database. Those fields will be saved in the database automatically if the user saves the post.

Looking forward to knowing your thoughts on it.

@samirmalpande Thanks for the Patch!

#4 @samirmalpande
4 weeks ago

Hi @johnbillion,

There is another use case where this is required. We use WP Bakery (Visual Composer) and Offload S3 in almost all the websites. Offload S3 pushes all images to Amazon S3 and updates the url in pages.

Offload S3 does not update the image urls in database or content of the page so if we disable the plugin the original file urls work without problem. Offload S3 update the image urls on the fly while the page is being generated. This works very well in most of the cases.

In WP Bakery if we add a background image to any row or column, it stores the generated css in postmeta table. While page is being generated this css is added to html head tag using wp_head action. Offload S3 replaces all the urls in content but is not able to replaces urls which are added to head.

We work on environment where we use multiple servers. The file is not available on all the servers. We get 404 errors while loading background image. The background image doesn't show if image is not found.

If we could add filter on meta we would be able to update the urls stored in postmeta.

Thanks, Samir

Note: See TracTickets for help on using tickets.