Advertisement
festinger

Untitled

May 21st, 2025
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.30 KB | None | 0 0
  1. <?php
  2. // Auto-assign category and add post to menu on publish
  3. add_action('transition_post_status', 'auto_categorize_and_add_to_menu', 10, 3);
  4.  
  5. function auto_categorize_and_add_to_menu($new_status, $old_status, $post) {
  6.     // Only run when a post transitions from draft to publish
  7.     if ($new_status === 'publish' && $old_status === 'draft' && $post->post_type === 'post') {
  8.         // Step 1: Auto-assign category based on keywords
  9.         $title = strtolower($post->post_title);
  10.         $content = strtolower($post->post_content);
  11.        
  12.         // Define keyword-to-category mapping (customize this)
  13.         $category_mapping = [
  14.             'technology' => 'Technology', // Keyword => Category Name
  15.             'health' => 'Health',
  16.             'finance' => 'Finance',
  17.             // Add more keywords and categories as needed
  18.         ];
  19.  
  20.         $assigned_category = false;
  21.         foreach ($category_mapping as $keyword => $category_name) {
  22.             if (strpos($title, $keyword) !== false || strpos($content, $keyword) !== false) {
  23.                 // Get or create the category
  24.                 $category = get_term_by('name', $category_name, 'category');
  25.                 if (!$category) {
  26.                     $category_id = wp_insert_category([
  27.                         'cat_name' => $category_name,
  28.                         'category_description' => 'Auto-created category for ' . $category_name,
  29.                         'category_nicename' => sanitize_title($category_name),
  30.                     ]);
  31.                     $category = get_term_by('id', $category_id, 'category');
  32.                 }
  33.  
  34.                 // Assign the category to the post
  35.                 wp_set_post_categories($post->ID, [$category->term_id], true);
  36.                 $assigned_category = $category->term_id;
  37.                 break; // Assign only one category (remove to allow multiple)
  38.             }
  39.         }
  40.  
  41.         // Fallback: Assign a default category if no keywords match
  42.         if (!$assigned_category) {
  43.             $default_category = get_term_by('name', 'Uncategorized', 'category');
  44.             if ($default_category) {
  45.                 wp_set_post_categories($post->ID, [$default_category->term_id], true);
  46.                 $assigned_category = $default_category->term_id;
  47.             }
  48.         }
  49.  
  50.         // Step 2: Add post to the navigation menu
  51.         if ($assigned_category) {
  52.             // Define the menu to update (replace 'primary-menu' with your menu's slug or ID)
  53.             $menu_name = 'primary-menu';
  54.             $menu = wp_get_nav_menu_object($menu_name);
  55.  
  56.             if ($menu) {
  57.                 // Get the category term
  58.                 $category = get_term($assigned_category, 'category');
  59.  
  60.                 // Check if the category is already a menu item
  61.                 $menu_items = wp_get_nav_menu_items($menu->term_id);
  62.                 $parent_menu_item_id = 0;
  63.  
  64.                 foreach ($menu_items as $item) {
  65.                     if ($item->object === 'category' && $item->object_id == $category->term_id) {
  66.                         $parent_menu_item_id = $item->ID;
  67.                         break;
  68.                     }
  69.                 }
  70.  
  71.                 // If category isn't in the menu, add it
  72.                 if ($parent_menu_item_id === 0) {
  73.                     $parent_menu_item_id = wp_update_nav_menu_item($menu->term_id, 0, [
  74.                         'menu-item-title' => $category->name,
  75.                         'menu-item-object' => 'category',
  76.                         'menu-item-object-id' => $category->term_id,
  77.                         'menu-item-type' => 'taxonomy',
  78.                         'menu-item-status' => 'publish',
  79.                     ]);
  80.                 }
  81.  
  82.                 // Add the post as a sub-menu item under the category
  83.                 wp_update_nav_menu_item($menu->term_id, 0, [
  84.                     'menu-item-title' => $post->post_title,
  85.                     'menu-item-object' => 'post',
  86.                     'menu-item-object-id' => $post->ID,
  87.                     'menu-item-type' => 'post_type',
  88.                     'menu-item-status' => 'publish',
  89.                     'menu-item-parent-id' => $parent_menu_item_id,
  90.                 ]);
  91.             }
  92.         }
  93.     }
  94. }
  95.  
  96. // Optional: Limit menu size to prevent bloat (e.g., keep only the latest 10 posts per category)
  97. add_action('wp_update_nav_menu', 'limit_menu_items_per_category');
  98.  
  99. function limit_menu_items_per_category($menu_id) {
  100.     $menu = wp_get_nav_menu_object($menu_id);
  101.     if (!$menu) {
  102.         return;
  103.     }
  104.  
  105.     $menu_items = wp_get_nav_menu_items($menu_id);
  106.     $category_items = [];
  107.  
  108.     // Group menu items by parent category
  109.     foreach ($menu_items as $item) {
  110.         if ($item->menu_item_parent != 0 && $item->type === 'post_type') {
  111.             $category_items[$item->menu_item_parent][] = $item;
  112.         }
  113.     }
  114.  
  115.     // Keep only the latest 10 posts per category
  116.     foreach ($category_items as $parent_id => $items) {
  117.         if (count($items) > 10) {
  118.             usort($items, function ($a, $b) {
  119.                 return get_post_time('U', true, $b->object_id) - get_post_time('U', true, $a->object_id);
  120.             });
  121.  
  122.             for ($i = 10; $i < count($items); $i++) {
  123.                 wp_delete_nav_menu_item($menu_id, $items[$i]->ID);
  124.             }
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement