Advertisement
mbis

Force auto-update

Apr 27th, 2021
1,195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.43 KB | None | 0 0
  1. function pm_update_product_uri($post) {
  2.     global $permalink_manager_uris, $permalink_manager_options;
  3.  
  4.     // Do not do anything if post is autosaved
  5.     if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post; }
  6.    
  7.     // Do not do anything if the field with URI or element ID are present
  8.     if(isset($_POST['custom_uri']) || !empty($_POST['permalink-manager-edit-uri-element-id'])) { return $post; }
  9.  
  10.     // Get the full post object
  11.     $post_object = (!empty($post->ID)) ? $post : get_post($post);
  12.  
  13.     // Check the post type
  14.     if(empty($post_object->post_type) || $post_object->post_type !== 'product') { return $post; };
  15.    
  16.     // Ignore auto-drafts, removed posts and posts without title
  17.     if(in_array($post_object->post_status, array('auto-draft', 'trash')) || empty($post_object->post_title)) { return $post_id; }
  18.    
  19.     // Get the post ID
  20.     $post_id = $post_object->ID;
  21.    
  22.     // Get auto-update URI setting (if empty use global setting)
  23.     $auto_update_uri = get_post_meta($post_id, "auto_update_uri", true);
  24.     $auto_update_uri = (!empty($auto_update_uri)) ? $auto_update_uri : $permalink_manager_options["general"]["auto_update_uris"];
  25.  
  26.     if($auto_update_uri == 1) {
  27.         // Get the default URI
  28.         $default_uri = Permalink_Manager_URI_Functions_Post::get_default_post_uri($post_id);
  29.  
  30.         // Save the default URI
  31.         Permalink_Manager_URI_Functions::save_single_uri($post_id, $default_uri, false, true);
  32.     }
  33. }
  34. add_action('save_post', 'pm_update_product_uri', 99, 1);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement