Advertisement
Guest User

Untitled

a guest
Oct 10th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.56 KB | None | 0 0
  1. function pm_replace_custom_field_tags($default_uri, $native_slug, $element, $slug, $native_uri) {
  2.     // 1. Insert %new_dynamic_tag% in permastructure settings (Tools -> Permalink Manager -> Permsatructures)
  3.     $tag = '%new_dynamic_tag%';
  4.  
  5.     // 2A. Check if value set with ACF plugin can be parsed
  6.     if(function_exists('get_field')) {
  7.         $acf_element_id = (!empty($element->ID)) ? $element->ID : "{$element->taxonomy}_{$element->term_id}";
  8.         $field_object = get_field_object($custom_field, $acf_element_id);
  9.  
  10.         // Extra function to format and get the value (use $custom_field_value variable)
  11.         // ...
  12.         // ...
  13.         // $custom_field_value = ...;
  14.        
  15.     }
  16.  
  17.     // 2B. Use native functions to get custom field value (if it cannot be parsed from ACF)
  18.     if(empty($custom_field_value)) {
  19.         if(!empty($element->ID)) {
  20.             $custom_field_value = get_post_meta($element->ID, $custom_field, true);
  21.         } else if(!empty($element->term_id)) {
  22.             $custom_field_value = get_term_meta($element->term_id, $custom_field, true);
  23.         } else {
  24.             $custom_field_value = "";
  25.         }
  26.     }
  27.  
  28.     // Make sure that custom field is a string and replace %new_dynamic_tag% with custom field value (if it is set)
  29.     if(!empty($custom_field_value) && is_string($custom_field_value)) {
  30.         $default_uri = str_replace(tag, Permalink_Manager_Helper_Functions::sanitize_title($custom_field_value), $default_uri);
  31.     }
  32.  
  33.     return $default_uri;
  34. }
  35. add_filter( 'permalink_manager_filter_default_post_uri', 'pm_replace_custom_field_tags', 9, 5 );
  36. add_filter( 'permalink_manager_filter_default_term_uri', 'pm_replace_custom_field_tags', 9, 5 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement