Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function pm_custom_uri_rest() {
- register_rest_field(
- 'product',
- 'custom_uri',
- array(
- 'get_callback' => 'rest_get_custom_uri',
- 'update_callback' => 'rest_update_custom_uri',
- 'schema' => array(
- 'description' => 'The value of "Current URI" field.',
- 'type' => 'string',
- 'context' => array('view', 'edit')
- )
- )
- );
- }
- add_action('rest_api_init', 'pm_custom_uri_rest');
- function rest_get_custom_uri($post, $field_name, $request) {
- return (!empty($post['id'])) ? Permalink_Manager_URI_Functions_Post::get_post_uri($post['id']) : '';
- }
- function rest_update_custom_uri($value, $post, $field_name) {
- // Perform Validation of input
- if (!$value || !is_string($value)) {
- return;
- }
- // Get product ID
- if(!empty($post->ID)) {
- $post_id = $post->ID;
- } elseif(method_exists($post, 'get_id')) {
- $post_id = $post->get_id();
- }
- if(!empty($post_id)) {
- Permalink_Manager_URI_Functions::save_single_uri($post_id, $value, false, true);
- }
- }
- function pm_rest_custom_uri_after_rest_insert($post, $request, $is_new_post) {
- if($is_new_post && class_exists('Permalink_Manager_URI_Functions_Post')) {
- // 1. Generate the URI
- $new_uri = Permalink_Manager_URI_Functions_Post::get_default_post_uri($post);
- // 2. Save the URI in DB
- Permalink_Manager_URI_Functions::save_single_uri($post->ID, $new_uri, false, true);
- }
- }
- add_action('rest_after_insert_product', 'pm_rest_custom_uri_after_rest_insert', 9, 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement