public function initApi() { register_rest_route('app/v1', '/coupons', array('methods' => 'GET', 'callback' => array($this, 'getItems'), 'permission_callback' => function () { return current_user_can('level_0'); })); register_api_field('user', 'coupons', array('get_callback' => array($this, 'getCouponCount'), 'update_callback' => null, 'schema' => null)); }
public function register_fields() { $callbacks = array('get_callback' => [$this, 'get_meta'], 'update_callback' => [$this, 'set_meta'], 'schema' => null); register_api_field('comment', 'linkoscope_likes', $callbacks); $raw = array('schema' => array('type' => 'string', 'raw' => array('type' => 'string', 'description' => 'Title for the object, as it exists in the database.', 'context' => array('view', 'edit')))); register_api_field('comment', 'content', $raw); $int = array('schema' => array('description' => 'Votes on a comment.', 'type' => 'integer')); register_api_field('comment', 'karma', $int); }
function wp_listings_register_listing_meta() { $allowed_meta_keys = allowed_meta_keys(); foreach ($allowed_meta_keys as $listing_meta_key) { register_api_field('listing', $listing_meta_key, array('get_callback' => 'wp_listings_get_listing_meta', 'update_callback' => 'wp_listings_update_listing_meta', 'schema' => null)); } }
function moreposts_register_fields() { // add author name register_api_field('post', 'author_name', array('get_callback' => 'moreposts_get_author_name', 'update_callback' => null, 'schema' => null)); // add featured image register_api_field('post', 'featured_image_src', array('get_callback' => 'moreposts_get_image_src', 'update_callback' => null, 'schema' => null)); }
function register_place_meta_fields() { register_api_field($this->type, 'tourist_office_group', array('get_callback' => array($this, 'get_place_meta_field'), 'update_callback' => array($this, 'update_place_meta_field'), 'schema' => null)); register_api_field($this->type, 'contact_info', array('get_callback' => array($this, 'get_place_meta_field'), 'update_callback' => array($this, 'update_place_meta_field'), 'schema' => null)); register_api_field($this->type, 'social_media_info', array('get_callback' => array($this, 'get_place_meta_field'), 'update_callback' => array($this, 'update_place_meta_field'), 'schema' => null)); register_api_field($this->type, 'geographic_coordinates', array('get_callback' => array($this, 'get_place_meta_field'), 'update_callback' => array($this, 'update_place_meta_field'), 'schema' => null)); register_api_field($this->type, 'geographic_coordinates', array('get_callback' => array($this, 'get_place_meta_field'), 'update_callback' => array($this, 'update_place_meta_field'), 'schema' => null)); }
public function initApi() { register_api_field('user', 'signed', array('get_callback' => array($this, 'getSignedCount'), 'update_callback' => null, 'schema' => null)); register_api_field('user', 'canSign', array('get_callback' => array($this, 'getCanSign'), 'update_callback' => null, 'schema' => null)); register_rest_route('app/v1', '/sign', array('methods' => 'POST', 'callback' => array($this, 'doSign'), 'permission_callback' => function () { return current_user_can('level_0'); })); }
function related_content_register_fields() { // Add Author Name register_api_field('post', 'author_name', array('get_callback' => 'related_content_author_name', 'update_callback' => null, 'schema' => null)); // Add Featured Image register_api_field('post', 'featured_image_src', array('get_callback' => 'related_content_get_image_src', 'update_callback' => null, 'schema' => null)); // Add Related Links register_api_field('post', 'related_links', array('get_callback' => 'related_content_related_links', 'update_callback' => null, 'schema' => null)); }
function post_add_tax_register() { $post_types = get_post_types(array('public' => true, 'exclude_from_search' => false), 'names'); foreach ($post_types as $cpt) { if ($cpt === 'attachment') { continue; } register_api_field($cpt, 'post_taxonomies', array('update_callback' => array($this, 'post_add_tax'), 'schema' => null)); } }
public function bq_wp_rest_ap_extend() { $types = get_post_types(array('public' => true, '_builtin' => false)); register_api_field($types, 'featured_image_url', array('get_callback' => array($this, 'featured_image_url'), 'update_callback' => null, 'schema' => null)); register_api_field($types, 'custom_post_title', array('get_callback' => array($this, 'post_title'), 'update_callback' => null, 'schema' => null)); register_api_field($types, 'prev_post_url', array('get_callback' => array($this, 'prev_post_url'), 'update_callback' => null, 'schema' => null)); register_api_field($types, 'prev_post_id', array('get_callback' => array($this, 'prev_post_id'), 'update_callback' => null, 'schema' => null)); register_api_field($types, 'next_post_url', array('get_callback' => array($this, 'next_post_url'), 'update_callback' => null, 'schema' => null)); register_api_field($types, 'next_post_id', array('get_callback' => array($this, 'next_post_id'), 'update_callback' => null, 'schema' => null)); register_api_field('post', 'featured_image_url', array('get_callback' => array($this, 'featured_image_url'), 'update_callback' => null, 'schema' => null)); }
public function register_api_fields() { $schema = array('description' => 'Modular page builder data.', 'type' => 'array', 'context' => array('view')); register_api_field($this->get_supported_post_types(), $this->args['api_prop'], array('schema' => $schema, 'get_callback' => function ($object, $field_name, $request) { if (!is_null($request->get_param('ignore_page_builder'))) { return array(); } $data = $this->get_rendered_data($object['id'], $this->id . '-data'); return !empty($data) ? $data : array(); })); }
public function test_get_additional_field_registration() { $schema = array('type' => 'integer', 'description' => 'Some integer of mine', 'enum' => array(1, 2, 3, 4), 'context' => array('view', 'edit')); register_api_field('type', 'my_custom_int', array('schema' => $schema, 'get_callback' => array($this, 'additional_field_get_callback'), 'update_callback' => array($this, 'additional_field_update_callback'))); $request = new WP_REST_Request('OPTIONS', '/wp/v2/types/schema'); $response = $this->server->dispatch($request); $data = $response->get_data(); $this->assertArrayHasKey('my_custom_int', $data['schema']['properties']); $this->assertEquals($schema, $data['schema']['properties']['my_custom_int']); $request = new WP_REST_Request('GET', '/wp/v2/types/post'); $response = $this->server->dispatch($request); $this->assertArrayHasKey('my_custom_int', $response->data); global $wp_rest_additional_fields; $wp_rest_additional_fields = array(); }
public function register_fields() { $meta_callbacks = array('get_callback' => [$this, 'get_meta'], 'update_callback' => [$this, 'set_meta'], 'schema' => null); register_api_field('linkoscope_link', 'linkoscope_score', $meta_callbacks); register_api_field('linkoscope_link', 'linkoscope_likes', $meta_callbacks); $author_name_callbacks = array('get_callback' => [$this, 'get_author_name'], 'update_callback' => [$this, 'no_op'], 'schema' => null); register_api_field('linkoscope_link', 'author_name', $author_name_callbacks); $comment_count_callbacks = array('get_callback' => [$this, 'get_comment_count'], 'update_callback' => [$this, 'no_op'], 'schema' => null); register_api_field('linkoscope_link', 'comment_count', $comment_count_callbacks); $tags_callbacks = array('get_callback' => [$this, 'get_tags'], 'update_callback' => [$this, 'set_tags'], 'schema' => null); register_api_field('linkoscope_link', 'tags', $tags_callbacks); $raw = array('schema' => array('type' => 'string', 'raw' => array('type' => 'string', 'description' => 'Title for the object, as it exists in the database.', 'context' => array('view', 'edit')))); register_api_field('linkoscope_link', 'title', $raw); register_api_field('linkoscope_link', 'content', $raw); }
function wp_rest_api_add_taxonomies() { $post_types = get_post_types(array('public' => true), 'objects'); foreach ($post_types as $post_type) { $show_in_rest = isset($post_type->show_in_rest) && $post_type->show_in_rest ? true : false; if ($show_in_rest) { $taxonomies = get_taxonomies(array(), "objects"); foreach ($taxonomies as $taxonomy) { if (is_object_in_taxonomy($post_type->name, $taxonomy->name)) { register_api_field($post_type->name, $taxonomy->name, array('get_callback' => array($this, 'wp_rest_api_register_taxonomy'), 'schema' => null)); } } } } }
/** * Register our enhanced better_featured_image field to all public post types * that support post thumbnails. * * @since 1.0.0 */ function better_rest_api_featured_images_init() { $post_types = get_post_types(array('public' => true), 'objects'); foreach ($post_types as $post_type) { $post_type_name = $post_type->name; $show_in_rest = isset($post_type->show_in_rest) && $post_type->show_in_rest ? true : false; $supports_thumbnail = post_type_supports($post_type_name, 'thumbnail'); // Only proceed if the post type is set to be accessible over the REST API // and supports featured images. if ($show_in_rest && $supports_thumbnail) { // Compatibility with the REST API v2 beta 9+ if (function_exists('register_rest_field')) { register_rest_field($post_type_name, 'better_featured_image', array('get_callback' => 'better_rest_api_featured_images_get_field', 'schema' => null)); } elseif (function_exists('register_api_field')) { register_api_field($post_type_name, 'better_featured_image', array('get_callback' => 'better_rest_api_featured_images_get_field', 'schema' => null)); } } } }
function slug_register_api() { register_api_field('portfolio_project', 'gallery_set', array('get_callback' => 'slug_get_api_field', 'update_callback' => null, 'schema' => null)); register_api_field('portfolio_project', 'project_short_description', array('get_callback' => 'slug_get_api_field', 'update_callback' => null, 'schema' => null)); register_api_field('portfolio_project', 'font_color', array('get_callback' => 'slug_get_api_field', 'update_callback' => null, 'schema' => null)); }
public function test_get_additional_field_registration() { $schema = array('type' => 'integer', 'description' => 'Some integer of mine', 'enum' => array(1, 2, 3, 4), 'context' => array('view', 'edit')); register_api_field('user', 'my_custom_int', array('schema' => $schema, 'get_callback' => array($this, 'additional_field_get_callback'), 'update_callback' => array($this, 'additional_field_update_callback'))); $request = new WP_REST_Request('OPTIONS', '/wp/v2/users'); $response = $this->server->dispatch($request); $data = $response->get_data(); $this->assertArrayHasKey('my_custom_int', $data['schema']['properties']); $this->assertEquals($schema, $data['schema']['properties']['my_custom_int']); wp_set_current_user(1); if (is_multisite()) { $current_user = wp_get_current_user(1); update_site_option('site_admins', array($current_user->user_login)); } $request = new WP_REST_Request('GET', '/wp/v2/users/1'); $response = $this->server->dispatch($request); $this->assertArrayHasKey('my_custom_int', $response->data); $request = new WP_REST_Request('POST', '/wp/v2/users/1'); $request->set_body_params(array('my_custom_int' => 123)); $response = $this->server->dispatch($request); $this->assertEquals(123, get_user_meta(1, 'my_custom_int', true)); $request = new WP_REST_Request('POST', '/wp/v2/users'); $request->set_body_params(array('my_custom_int' => 123, 'email' => '*****@*****.**', 'username' => 'abc123', 'password' => 'hello')); $response = $this->server->dispatch($request); $this->assertEquals(123, $response->data['my_custom_int']); global $wp_rest_additional_fields; $wp_rest_additional_fields = array(); }
public function test_get_additional_field_registration() { $schema = array('type' => 'integer', 'description' => 'Some integer of mine', 'enum' => array(1, 2, 3, 4), 'context' => array('view', 'edit')); register_api_field('attachment', 'my_custom_int', array('schema' => $schema, 'get_callback' => array($this, 'additional_field_get_callback'))); $request = new WP_REST_Request('OPTIONS', '/wp/v2/media'); $response = $this->server->dispatch($request); $data = $response->get_data(); $this->assertArrayHasKey('my_custom_int', $data['schema']['properties']); $this->assertEquals($schema, $data['schema']['properties']['my_custom_int']); $attachment_id = $this->factory->attachment->create_object($this->test_file, 0, array('post_mime_type' => 'image/jpeg', 'post_excerpt' => 'A sample caption')); $request = new WP_REST_Request('GET', '/wp/v2/media/' . $attachment_id); $response = $this->server->dispatch($request); $this->assertArrayHasKey('my_custom_int', $response->data); global $wp_rest_additional_fields; $wp_rest_additional_fields = array(); }
/** * Registers the `acf` field against comments * * @return void * * @since 1.3.0 */ function addACFDataCommentV2() { register_api_field('comment', 'acf', array('get_callback' => array($this, 'addACFDataCommentV2cb'), 'update_callback' => null, 'schema' => null)); }
public function test_get_additional_field_registration() { $schema = array('type' => 'integer', 'description' => 'Some integer of mine', 'enum' => array(1, 2, 3, 4), 'context' => array('view', 'edit')); register_api_field('comment', 'my_custom_int', array('schema' => $schema, 'get_callback' => array($this, 'additional_field_get_callback'), 'update_callback' => array($this, 'additional_field_update_callback'))); $request = new WP_REST_Request('OPTIONS', '/wp/v2/comments'); $response = $this->server->dispatch($request); $data = $response->get_data(); $this->assertArrayHasKey('my_custom_int', $data['schema']['properties']); $this->assertEquals($schema, $data['schema']['properties']['my_custom_int']); $request = new WP_REST_Request('GET', '/wp/v2/comments/' . $this->approved_id); $response = $this->server->dispatch($request); $this->assertArrayHasKey('my_custom_int', $response->data); $request = new WP_REST_Request('POST', '/wp/v2/comments/' . $this->approved_id); $request->set_body_params(array('my_custom_int' => 123, 'content' => 'abc')); wp_set_current_user(1); $response = $this->server->dispatch($request); $this->assertEquals(123, get_comment_meta($this->approved_id, 'my_custom_int', true)); $request = new WP_REST_Request('POST', '/wp/v2/comments'); $request->set_body_params(array('my_custom_int' => 123, 'title' => 'hello', 'post' => $this->post_id)); $response = $this->server->dispatch($request); $this->assertEquals(123, $response->data['my_custom_int']); global $wp_rest_additional_fields; $wp_rest_additional_fields = array(); }
function register_new_field() { register_api_field('post', 'my_awesome_field', array('get_callback' => array($this, 'awesome_field'))); }
/** * Register the fields * * @since 0.1.0 * * @access protected */ protected function register_fields() { register_api_field($this->post_types, $this->title_field, array('get_callback' => array($this, 'get_post_meta_cb'), 'update_callback' => array($this, 'update_post_meta_cb'), 'schema' => null)); register_api_field($this->post_types, $this->description_field, array('get_callback' => array($this, 'get_post_meta_cb'), 'update_callback' => array($this, 'update_post_meta_cb'), 'schema' => null)); }
function dec_register_deal_terms() { register_api_field('eat', 'rw_deal_terms', array('get_callback' => 'dec_get_deal_terms', 'update_callback' => 'dec_update_deal_terms', 'schema' => null)); }
public function agency_rest_support() { register_api_field('agency', 'meta', array('get_callback' => 'agency_rest_metadata', 'update_callback' => null, 'schema' => null)); }
public function initApi() { register_api_field('user', 'invited', array('get_callback' => array($this, 'getInvitedCount'), 'update_callback' => null, 'schema' => null)); register_api_field('user', 'inviteCode', array('get_callback' => array($this, 'getInviteCode'), 'update_callback' => null, 'schema' => null)); }
function add_fields_to_rest() { register_api_field('post', 'author_name', array('get_callback' => 'tr_get_author_name', 'update_callback' => null, 'scheme' => null)); register_api_field('post', 'featured_image_src', array('get_callback' => 'get_featured_image_src', 'update_callback' => null, 'scheme' => null)); }
function add_comments() { register_api_field('post', 'comments', array('get_callback' => array($this, 'get_comments'), 'update_callback' => null, 'schema' => null)); }
public function document_rest_support() { register_api_field('document', 'meta', array('get_callback' => array($this, 'document_rest_metadata'), 'update_callback' => null, 'schema' => null)); }
function taskvideo_save($post_id) { // Checks save status $is_autosave = wp_is_post_autosave($post_id); $is_revision = wp_is_post_revision($post_id); $is_valid_nonce = isset($_POST['vid_nonce']) && wp_verify_nonce($_POST['vid_nonce'], basename(__FILE__)) ? 'true' : 'false'; // Exits script depending on save status if ($is_autosave || $is_revision || !$is_valid_nonce) { return; } // Checks for input and sanitizes/saves if needed if (isset($_POST['video-url'])) { $vid = $_POST['video-url']; $dir = get_site_url() . '/' . $_POST['dir']; $vid1 = $dir . '/' . $vid; update_post_meta($post_id, 'video-url', sanitize_text_field($vid)); update_post_meta($post_id, 'vid', sanitize_text_field($vid1)); } } add_action('save_post', 'taskvideo_save'); function slug_get_post_meta_cb($object, $field_name, $request) { return get_post_meta($object['id'], $field_name); } function slug_update_post_meta_cb($value, $object, $field_name) { return update_post_meta($object['id'], $field_name, $value); } add_action('rest_api_init', function () { register_api_field('tasks', 'vid', array('get_callback' => 'slug_get_post_meta_cb', 'update_callback' => 'slug_update_post_meta_cb', 'schema' => null)); });
/** * Register the API fields * * Register the plugin data fields with the REST API `posts` endpoint. * * @since 0.8.3 */ public function register_fields() { // Featured image caption data register_api_field('post', CCFIC_KEY, array('get_callback' => array($this, 'rest_caption'), 'update_callback' => null, 'schema' => null)); }
<?php /** * Customize the response from WP API. * * Endpoint: http://HOME_URL.COM/wp/v2/posts?filter[orderby]=rand&filter[posts_per_page]=1 * * @link http://v2.wp-api.org/ */ /** * Add post status, source, and source URL fields to API request */ add_action('rest_api_init', function () { register_api_field('post', 'post_status', array('get_callback' => 'qod_get_post_status', 'update_callback' => 'qod_update_post_status', 'schema' => null)); register_api_field('post', '_qod_quote_source', array('get_callback' => 'qod_get_quote_meta_fields', 'update_callback' => 'qod_update_quote_meta_fields', 'schema' => null)); register_api_field('post', '_qod_quote_source_url', array('get_callback' => 'qod_get_quote_meta_fields', 'update_callback' => 'qod_update_quote_meta_fields', 'schema' => null)); }); /** * Handler for fetching the post status. * * @param array $object Details of current post. * @param string $field_name Name of field to add to response. * @param WP_REST_Request $request Current request. * * @return mixed */ function qod_get_post_status($object, $field_name, $request) { $post = get_post($object['id']); return $post->post_status; }