Documentation

Graphic_Data_Media_Manager
in package

Manages custom attachment fields in the WordPress Media Library.

Adds an Instance association dropdown to the media editor and persists the selected value as graphic_data_instance_id post meta on the attachment.

Table of Contents

Methods

add_instance_field_to_media()  : array<string|int, mixed>
Add Instance dropdown to Media Library attachment fields.
create_instance_list()  : array<string|int, mixed>
Query published Instance posts visible to the current user.
enqueue_instance_filter_script()  : void
Enqueue the media grid filter script and pass instance data to it.
filter_ajax_media_by_instance()  : array<string|int, mixed>
Apply the Instance filter to the media grid view (AJAX) query.
filter_media_by_instance()  : void
Apply the Instance filter to the media list view query.
render_instance_filter_dropdown()  : void
Render the Instance filter dropdown in the media list view toolbar.
save_instance_field_to_media()  : array<string|int, mixed>
Save the Instance dropdown value when an attachment is updated.

Methods

add_instance_field_to_media()

Add Instance dropdown to Media Library attachment fields.

public add_instance_field_to_media(array<string|int, mixed> $form_fields, WP_Post $post) : array<string|int, mixed>

Hooked to attachment_fields_to_edit. Builds a <select> populated with all published Instance posts and pre-selects the value stored in the graphic_data_instance_id post meta for the current attachment.

Parameters
$form_fields : array<string|int, mixed>

Existing attachment form fields.

$post : WP_Post

The attachment post object being edited.

Return values
array<string|int, mixed>

The form fields array with the Instance dropdown appended.

create_instance_list()

Query published Instance posts visible to the current user.

public create_instance_list() : array<string|int, mixed>

Authors receive only the instances listed in their assigned_instances user meta. All other roles receive every published Instance post. Returns an empty array when an author has no assigned instances.

Tags
global

wpdb $wpdb WordPress database abstraction object.

Return values
array<string|int, mixed>

Array of objects with ID and post_title properties, ordered by post title ascending.

enqueue_instance_filter_script()

Enqueue the media grid filter script and pass instance data to it.

public enqueue_instance_filter_script() : void

Hooked to admin_enqueue_scripts. Only runs on the upload.php screen. Depends on media-views so it loads after the WP media backbone stack.

filter_ajax_media_by_instance()

Apply the Instance filter to the media grid view (AJAX) query.

public filter_ajax_media_by_instance(array<string|int, mixed> $query) : array<string|int, mixed>

Hooked to ajax_query_attachments_args. WordPress strips unknown keys from the query via array_intersect_key() before this filter runs, so graphic_data_instance_id must be read directly from $_REQUEST['query'].

Sentinel values:

  • 'all' : media in user-accessible instances OR with no instance (union)
  • 'available' : media tagged with a user-accessible instance only
  • 'none' : media with no graphic_data_instance_id meta at all
  • numeric : media tagged with that specific instance ID
Parameters
$query : array<string|int, mixed>

Existing query args for WP_Query.

Return values
array<string|int, mixed>

Modified query args.

filter_media_by_instance()

Apply the Instance filter to the media list view query.

public filter_media_by_instance(WP_Query $query) : void

Hooked to parse_query. Adds a meta_query clause when graphic_data_instance_id is present in the request.

Parameters
$query : WP_Query

The current query object.

render_instance_filter_dropdown()

Render the Instance filter dropdown in the media list view toolbar.

public render_instance_filter_dropdown(string $post_type) : void

Hooked to restrict_manage_posts. Only outputs on the attachment post type screen.

Parameters
$post_type : string

The current post type.

save_instance_field_to_media()

Save the Instance dropdown value when an attachment is updated.

public save_instance_field_to_media(array<string|int, mixed> $post, array<string|int, mixed> $attachment) : array<string|int, mixed>

Hooked to attachment_fields_to_save. WordPress passes the post as an array (ARRAY_A) via get_post( $id, ARRAY_A ) before applying this filter, so $post is an array, not a WP_Post object. Reads instance_id from the submitted attachment fields and either updates or removes the graphic_data_instance_id post meta on the attachment accordingly.

Parameters
$post : array<string|int, mixed>

The attachment post data array being saved.

$attachment : array<string|int, mixed>

The attachment fields submitted from the media editor.

Return values
array<string|int, mixed>

The unmodified post array (required by the filter).

On this page

Search results