// ============================================================ // AAROHI INSURANCE CUSTOM FUNCTIONS // Paste this block at the very END of the existing functions.php // in Appearance -> Theme File Editor. Do NOT replace the file — // append this below all the existing theme code. // ============================================================ // ===== FORCE ALL POST LINKS TO STAY ON OUR OWN SITE ===== function aarohi_force_real_permalink( $permalink, $post ) { if ( ! is_object( $post ) || empty( $post->ID ) ) return $permalink; if ( get_post_type( $post ) !== 'post' ) return $permalink; $real_url = home_url( '/?p=' . $post->ID ); if ( get_option( 'permalink_structure' ) ) { $real_url = trailingslashit( home_url( '/' . $post->post_name . '/' ) ); } return $real_url; } add_filter( 'post_link', 'aarohi_force_real_permalink', 9999, 2 ); add_filter( 'the_permalink', 'aarohi_force_real_permalink', 9999, 2 ); add_filter( 'post_type_link', 'aarohi_force_real_permalink', 9999, 2 ); // Shortcode to display post excerpt function aarohi_post_excerpt_shortcode() { global $post; $excerpt = $post->post_excerpt; if ( ! empty( $excerpt ) ) { return '
' . esc_html( $excerpt ) . '
'; } $content = wp_strip_all_tags( $post->post_content ); $short = wp_trim_words( $content, 55, '...' ); return '' . esc_html( $short ) . '
'; } add_shortcode( 'post_excerpt', 'aarohi_post_excerpt_shortcode' ); // Shortcode to display the post's already-assigned featured image // (no title — the theme's native post title handles that) function aarohi_post_title_image_shortcode() { global $post; if ( ! $post ) return ''; $title = get_the_title( $post->ID ); $output = ''; if ( has_post_thumbnail( $post->ID ) ) { $image_url = get_the_post_thumbnail_url( $post->ID, 'large' ); $output .= '\s*<\/p>/i', '', $content ); } return $content; } add_filter( 'the_content', 'aarohi_strip_content_images', 5 ); // Smart duplicate check for WPeMatico add_filter( 'wpematico_check_duplicate', 'aarohi_smart_duplicate_check', 10, 3 ); function aarohi_smart_duplicate_check( $is_duplicate, $title, $url ) { global $wpdb; $existing = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->posts} p INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id WHERE pm.meta_key = '_wpematico_item_url' AND pm.meta_value = %s AND p.post_status NOT IN ('trash', 'auto-draft')", $url ) ); return $existing > 0; } add_filter( 'wpematico_is_perma_item', 'aarohi_allow_deleted_refetch', 10, 2 ); function aarohi_allow_deleted_refetch( $is_permanent, $url ) { global $wpdb; $exists = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->posts} p INNER JOIN {$wpdb->postmeta} pm ON p.ID = pm.post_id WHERE pm.meta_key = '_wpematico_item_url' AND pm.meta_value = %s AND p.post_status NOT IN ('trash', 'auto-draft')", $url ) ); return $exists > 0; } // Auto-assign featured image from Aarohi's own uploaded image library // Uses attachment_url_to_postid() first to avoid self-request blocking function aarohi_auto_featured_image( $post_id ) { $force = isset( $_GET['force'] ) && $_GET['force'] == '1'; if ( has_post_thumbnail( $post_id ) && ! $force ) return; // IMAGE ARRAY — 45 images hosted on aarohiinsurance.com $all_images = [ 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Document1-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Document2-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Document3-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Document4-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Document5-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Document6-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Document7-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Document8-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Document9-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Document10-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Family1-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Family2-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Family3-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Family4-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Family5-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Family6-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Family7-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Family8-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Family9-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Family10-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Nature-Disaster1-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Nature-Disaster2-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Nature-Disaster3-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Nature-Disaster4-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Nature-Disaster5-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Nature-Disaster6-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Nature-Disaster7-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Nature-Disaster8-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Nature-Disaster9-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Nature-Disaster10-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Retirement1-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Retirement2-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Retirement3-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Retirement4-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Retirement5-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Retirement6-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Retirement7-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Retirement8-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Retirement9-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/Retirement10-scaled.jpg', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/1.png', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/2.png', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/3.png', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/4.png', 'https://aarohiinsurance.com/wp-content/uploads/2026/07/5.png', ]; $total_images = count( $all_images ); $image_index = $post_id % $total_images; $image_url = $all_images[ $image_index ]; if ( $force ) { delete_post_thumbnail( $post_id ); } // Try looking up existing attachment first $attachment_id = attachment_url_to_postid( $image_url ); if ( ! $attachment_id ) { $unscaled_url = preg_replace( '/-scaled(\.[a-z]+)$/i', '$1', $image_url ); $attachment_id = attachment_url_to_postid( $unscaled_url ); } if ( $attachment_id ) { set_post_thumbnail( $post_id, $attachment_id ); return; } // Fallback: sideload require_once( ABSPATH . 'wp-admin/includes/media.php' ); require_once( ABSPATH . 'wp-admin/includes/file.php' ); require_once( ABSPATH . 'wp-admin/includes/image.php' ); $upload = media_sideload_image( $image_url, $post_id, '', 'id' ); if ( ! is_wp_error( $upload ) ) { set_post_thumbnail( $post_id, $upload ); } } add_action( 'save_post', 'aarohi_auto_featured_image' ); add_action( 'wpematico_post_saved', 'aarohi_auto_featured_image' ); // Auto-categorization — assigns ONE most relevant category based on keywords function aarohi_auto_categorize( $post_id ) { $post = get_post( $post_id ); if ( ! $post || $post->post_type !== 'post' ) return; if ( in_array( $post->post_status, [ 'auto-draft', 'trash' ] ) ) return; // Only auto-categorize if post has no category yet, or is still in Uncategorized // This allows manual category changes to stick without being overwritten $current_categories = wp_get_post_categories( $post_id ); $uncategorized = get_category_by_slug( 'uncategorized' ); $uncategorized_id = $uncategorized ? $uncategorized->term_id : 1; if ( ! empty( $current_categories ) && ! in_array( $uncategorized_id, $current_categories ) ) { return; // Post already has a real category set manually — don't overwrite it } $title = strtolower( $post->post_title ); $content = strtolower( strip_tags( $post->post_content ) ); $text = $title . ' ' . $content; $rules = [ 'annuity' => [ 'annuity', 'annuities', 'fixed annuity', 'variable annuity', 'indexed annuity', 'fixed indexed annuity', 'fia', 'myga', 'immediate annuity', 'deferred annuity', 'income annuity', 'annuitization', 'annuity rate', 'annuity contract', 'annuity product', 'income rider', 'surrender charge', ], 'final-expense' => [ 'final expense', 'funeral cost', 'burial insurance', 'funeral insurance', 'end of life expense', 'burial cost', 'funeral planning', 'final expense insurance', ], 'life-insurance' => [ 'life insurance', 'term life', 'whole life', 'universal life', 'policy', 'premium', 'death benefit', 'beneficiary', 'coverage', 'underwriting', 'life cover', 'policyholder', 'sum assured', 'lic', 'insurer', 'insurance plan', ], 'medicare-insurance' => [ 'medicare', 'medicare advantage', 'medicare supplement', 'medigap', 'part a', 'part b', 'part d', 'medicare enrollment', 'medicare plan', ], 'mortgage-protection' => [ 'mortgage', 'home loan', 'mortgage protection', 'mortgage insurance', 'home loan cover', 'property loan', 'loan protection', 'housing loan', ], 'guide' => [ 'how to', 'guide', 'tips', 'checklist', 'step by step', 'explained', 'what is', 'beginner', 'basics', 'understanding', ], ]; $scores = []; foreach ( $rules as $slug => $keywords ) { $scores[ $slug ] = 0; foreach ( $keywords as $keyword ) { if ( strpos( $text, $keyword ) !== false ) { $scores[ $slug ] += 2; } } } arsort( $scores ); $winner_slug = array_key_first( $scores ); $winner_score = $scores[ $winner_slug ]; if ( $winner_score === 0 ) { $winner_slug = 'industry-news'; } $category = get_category_by_slug( $winner_slug ); if ( $category ) { wp_set_post_categories( $post_id, [ $category->term_id ] ); } else { $fallback = get_category_by_slug( 'industry-news' ); if ( $fallback ) { wp_set_post_categories( $post_id, [ $fallback->term_id ] ); } } } add_action( 'wpematico_post_saved', 'aarohi_auto_categorize', 20 ); add_action( 'save_post', 'aarohi_auto_categorize', 20 ); // ===== TRUE PRE-IMPORT FRESHNESS FILTER (LAST 7 DAYS) ===== add_filter( 'wpematico_pre_insert_post', 'aarohi_block_old_pre_insert', 10, 2 ); function aarohi_block_old_pre_insert( $post_data, $item ) { $check_date = null; if ( is_array( $post_data ) && ! empty( $post_data['post_date'] ) ) { $check_date = $post_data['post_date']; } elseif ( is_object( $post_data ) && ! empty( $post_data->post_date ) ) { $check_date = $post_data->post_date; } if ( ! $check_date && is_object( $item ) && method_exists( $item, 'get_date' ) ) { $check_date = $item->get_date( 'Y-m-d H:i:s' ); } if ( ! $check_date ) return $post_data; $post_timestamp = strtotime( $check_date ); if ( ! $post_timestamp ) return $post_data; $window_start = strtotime( '-6 days midnight' ); $window_end = strtotime( 'tomorrow midnight' ) - 1; if ( $post_timestamp < $window_start || $post_timestamp > $window_end ) { return false; } return $post_data; } // ===== POST-SAVE SAFETY NET (LAST 7 DAYS) ===== function aarohi_reject_old_posts( $post_id ) { $post = get_post( $post_id ); if ( ! $post || $post->post_type !== 'post' ) return; if ( in_array( $post->post_status, [ 'trash', 'auto-draft' ] ) ) return; $source_date = get_post_meta( $post_id, '_wpematico_item_date', true ); $check_date = $source_date ? $source_date : $post->post_date; $post_timestamp = strtotime( $check_date ); if ( ! $post_timestamp ) return; $window_start = strtotime( '-6 days midnight' ); $window_end = strtotime( 'tomorrow midnight' ) - 1; if ( $post_timestamp < $window_start || $post_timestamp > $window_end ) { wp_trash_post( $post_id ); } } add_action( 'wpematico_post_saved', 'aarohi_reject_old_posts', 5 ); add_action( 'save_post', 'aarohi_reject_old_posts', 5 ); // Sort posts newest first on front end function aarohi_newest_first_query( $query ) { if ( ! is_admin() && $query->is_main_query() && ( is_home() || is_category() ) ) { $query->set( 'orderby', 'date' ); $query->set( 'order', 'DESC' ); } } add_action( 'pre_get_posts', 'aarohi_newest_first_query' ); // ===== ENFORCE MAXIMUM 80 PUBLISHED POSTS ===== define( 'AAROHI_MAX_PUBLISHED_POSTS', 80 ); function aarohi_enforce_post_limit( $post_id ) { $post = get_post( $post_id ); if ( ! $post || $post->post_type !== 'post' ) return; if ( $post->post_status !== 'publish' ) return; if ( get_transient( 'aarohi_post_limit_running' ) ) return; set_transient( 'aarohi_post_limit_running', 1, 10 ); $total_published = wp_count_posts( 'post' )->publish; if ( $total_published > AAROHI_MAX_PUBLISHED_POSTS ) { $excess = $total_published - AAROHI_MAX_PUBLISHED_POSTS; $oldest_posts = get_posts( [ 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => $excess, 'orderby' => 'date', 'order' => 'ASC', ] ); foreach ( $oldest_posts as $old_post ) { wp_trash_post( $old_post->ID ); } } delete_transient( 'aarohi_post_limit_running' ); } add_action( 'save_post', 'aarohi_enforce_post_limit', 99 ); add_action( 'wpematico_post_saved', 'aarohi_enforce_post_limit', 99 ); // ===== ADMIN POST HANDLERS ===== add_action( 'admin_post_aarohi_fix_categories', 'aarohi_run_fix_categories' ); function aarohi_run_fix_categories() { if ( ! current_user_can( 'manage_options' ) ) wp_die( 'Unauthorized' ); check_admin_referer( 'aarohi_tools' ); $posts = get_posts( [ 'post_type' => 'post', 'posts_per_page' => -1, 'post_status' => 'publish' ] ); $count = 0; foreach ( $posts as $post ) { aarohi_auto_categorize( $post->ID ); $count++; } wp_redirect( admin_url( 'tools.php?page=aarohi-tools&done=categories&count=' . $count ) ); exit; } add_action( 'admin_post_aarohi_fix_images', 'aarohi_run_fix_images' ); function aarohi_run_fix_images() { if ( ! current_user_can( 'manage_options' ) ) wp_die( 'Unauthorized' ); check_admin_referer( 'aarohi_tools' ); $posts = get_posts( [ 'post_type' => 'post', 'posts_per_page' => -1, 'meta_query' => [ [ 'key' => '_thumbnail_id', 'compare' => 'NOT EXISTS' ] ] ] ); $count = 0; foreach ( $posts as $post ) { aarohi_auto_featured_image( $post->ID ); $count++; } wp_redirect( admin_url( 'tools.php?page=aarohi-tools&done=images&count=' . $count ) ); exit; } add_action( 'admin_post_aarohi_force_images', 'aarohi_run_force_images' ); function aarohi_run_force_images() { if ( ! current_user_can( 'manage_options' ) ) wp_die( 'Unauthorized' ); check_admin_referer( 'aarohi_tools' ); $posts = get_posts( [ 'post_type' => 'post', 'posts_per_page' => -1 ] ); $count = 0; foreach ( $posts as $post ) { delete_post_thumbnail( $post->ID ); aarohi_auto_featured_image( $post->ID ); $count++; } wp_redirect( admin_url( 'tools.php?page=aarohi-tools&done=force_images&count=' . $count ) ); exit; } add_action( 'admin_post_aarohi_delete_old_posts', 'aarohi_run_delete_old_posts' ); function aarohi_run_delete_old_posts() { if ( ! current_user_can( 'manage_options' ) ) wp_die( 'Unauthorized' ); check_admin_referer( 'aarohi_tools' ); $two_months_ago = strtotime( '-2 months' ); $posts = get_posts( [ 'post_type' => 'post', 'posts_per_page' => -1, 'post_status' => [ 'publish', 'draft', 'pending', 'trash' ] ] ); $count = 0; foreach ( $posts as $post ) { $source_date = get_post_meta( $post->ID, '_wpematico_item_date', true ); $check_date = $source_date ? $source_date : $post->post_date; $post_timestamp = strtotime( $check_date ); if ( $post_timestamp && $post_timestamp < $two_months_ago ) { wp_delete_post( $post->ID, true ); $count++; } } wp_redirect( admin_url( 'tools.php?page=aarohi-tools&done=deleted_old&count=' . $count ) ); exit; } add_action( 'admin_post_aarohi_trash_non_fresh', 'aarohi_run_trash_non_fresh' ); function aarohi_run_trash_non_fresh() { if ( ! current_user_can( 'manage_options' ) ) wp_die( 'Unauthorized' ); check_admin_referer( 'aarohi_tools' ); $window_start = strtotime( '-6 days midnight' ); $window_end = strtotime( 'tomorrow midnight' ) - 1; $posts = get_posts( [ 'post_type' => 'post', 'posts_per_page' => -1, 'post_status' => 'publish' ] ); $count = 0; foreach ( $posts as $post ) { $source_date = get_post_meta( $post->ID, '_wpematico_item_date', true ); $check_date = $source_date ? $source_date : $post->post_date; $post_timestamp = strtotime( $check_date ); if ( $post_timestamp && ( $post_timestamp < $window_start || $post_timestamp > $window_end ) ) { wp_trash_post( $post->ID ); $count++; } } wp_redirect( admin_url( 'tools.php?page=aarohi-tools&done=trashed_non_fresh&count=' . $count ) ); exit; } add_action( 'admin_post_aarohi_enforce_post_limit_now', 'aarohi_run_enforce_post_limit_now' ); function aarohi_run_enforce_post_limit_now() { if ( ! current_user_can( 'manage_options' ) ) wp_die( 'Unauthorized' ); check_admin_referer( 'aarohi_tools' ); $total_published = wp_count_posts( 'post' )->publish; $count = 0; if ( $total_published > AAROHI_MAX_PUBLISHED_POSTS ) { $excess = $total_published - AAROHI_MAX_PUBLISHED_POSTS; $oldest_posts = get_posts( [ 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => $excess, 'orderby' => 'date', 'order' => 'ASC' ] ); foreach ( $oldest_posts as $old_post ) { wp_trash_post( $old_post->ID ); $count++; } } wp_redirect( admin_url( 'tools.php?page=aarohi-tools&done=enforced_limit&count=' . $count ) ); exit; } // ===== ADMIN TOOLS PAGE ===== add_action( 'admin_menu', function() { add_management_page( 'Aarohi Insurance Tools', 'Aarohi Insurance Tools', 'manage_options', 'aarohi-tools', 'aarohi_tools_page' ); } ); function aarohi_tools_page() { $done = isset( $_GET['done'] ) ? sanitize_text_field( $_GET['done'] ) : ''; $count = isset( $_GET['count'] ) ? intval( $_GET['count'] ) : 0; $messages = [ 'categories' => 'Done! Auto-categorized ' . $count . ' posts successfully.', 'images' => 'Done! Assigned featured images to ' . $count . ' posts.', 'force_images' => 'Done! Reset images on ' . $count . ' posts.', 'deleted_old' => 'Done! Permanently deleted ' . $count . ' posts older than 2 months.', 'trashed_non_fresh' => 'Done! Trashed ' . $count . ' posts not published in the last 7 days.', 'enforced_limit' => 'Done! Trashed ' . $count . ' oldest posts — total back down to ' . AAROHI_MAX_PUBLISHED_POSTS . '.', ]; echo '
' . esc_html( $messages[ $done ] ) . '
| '; echo ' |