Home / Archive / WPEX Tetris REST Meta Integration
Duplicate Snippet

Embed Snippet on Your Site

WPEX Tetris REST Meta Integration

This code snippet exposes the following meta fields to the REST API:
- universal_local_image_attachment_ids
- universal_local_audio_attachment_ids
- universal_local_video_attachment_ids

Note: Fetching the actual media assets using these attachment IDs requires querying the WordPress media REST endpoint.

Supported Post Types:
- Any public post type that supports post-formats

theme requild
https://github.com/WP-Developer-Hub/wpex-tetris

Code Preview
php
<?php
if(!function_exists('wpex_register_universal_media_meta')){
    function wpex_register_universal_media_meta(){
        $media_meta_fields=array(
            'universal_local_image_attachment_ids',
            'universal_local_audio_attachment_ids',
            'universal_local_video_attachment_ids',
        );
        $post_types=get_post_types(array('public'=>true));
        foreach($post_types as $post_type){
            if(post_type_supports($post_type,'post-formats')){
                foreach($media_meta_fields as $meta_key){
                    if(!registered_meta_key_exists('post',$meta_key,$post_type)){
                        register_post_meta($post_type,$meta_key,array(
                            'type'=>'array',
                            'single'=>true,
                            'default'=>array(),
                            'show_in_rest'=>array(
                                'schema'=>array(
                                    'type'=>'array',
                                    'items'=>array('type'=>'integer'),
                                ),
                            ),
                            'sanitize_callback'=>function($value){
                                return is_array($value)?array_map('absint',$value):array();
                            },
                            'auth_callback'=>function($allowed,$meta_key,$post_id,$user_id,$cap,$caps){
                                return current_user_can('edit_post',$post_id);
                            },
                        ));
                    }
                }
            }
        }
    }
    add_action('rest_api_init','wpex_register_universal_media_meta');
}

Comments

Add a Comment