💾 Import from Rank Math
Migrate all your Rank Math SEO data to AgencyBoost without losing a single page's optimisation.
'Unauthorised.' ) ); } $mode = isset( $_POST['mode'] ) ? sanitize_key( $_POST['mode'] ) : 'dry_run'; $overwrite = ! empty( $_POST['overwrite'] ); $post_types = isset( $_POST['post_types'] ) ? array_map( 'sanitize_key', (array) wp_unslash( $_POST['post_types'] ) ) : array( 'post', 'page' ); $map = array( 'rank_math_title' => 'seo_title', 'rank_math_description' => 'seo_desc', 'rank_math_focus_keyword' => 'focus_kw', 'rank_math_canonical_url' => 'canonical', 'rank_math_facebook_title' => 'og_title', 'rank_math_facebook_description' => 'og_desc', 'rank_math_facebook_image' => 'og_image', ); global $wpdb; $keys_in = "'" . implode( "','", array_keys( $map ) ) . "','rank_math_robots'"; // phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.InterpolatedNotPrepared $post_ids = $wpdb->get_col( "SELECT DISTINCT post_id FROM {$wpdb->postmeta} WHERE meta_key IN ({$keys_in})" ); $results = array(); $stats = array( 'total' => 0, 'migrated' => 0, 'skipped' => 0 ); foreach ( $post_ids as $pid ) { $pid = (int) $pid; $post = get_post( $pid ); if ( ! $post ) continue; if ( ! in_array( $post->post_type, $post_types, true ) ) continue; if ( ! in_array( $post->post_status, array( 'publish', 'draft' ), true ) ) continue; $stats['total']++; $row = array( 'id' => $pid, 'title' => wp_trim_words( $post->post_title, 7 ), 'type' => $post->post_type, 'changes' => array(), 'skipped' => array(), ); foreach ( $map as $rm_key => $ab_key ) { $rm_val = get_post_meta( $pid, $rm_key, true ); if ( empty( $rm_val ) ) continue; $existing = agenli_get_post_meta( $pid, $ab_key ); if ( $existing && ! $overwrite ) { $row['skipped'][] = $ab_key; continue; } if ( 'og_image' === $ab_key ) { if ( is_numeric( $rm_val ) ) { $url = wp_get_attachment_url( (int) $rm_val ); if ( $url ) $rm_val = $url; } if ( is_array( $rm_val ) && ! empty( $rm_val['url'] ) ) { $rm_val = $rm_val['url']; } } if ( 'live' === $mode ) { agenli_save_post_meta( $pid, $ab_key, $rm_val ); } $row['changes'][] = $ab_key . ': ' . substr( (string) $rm_val, 0, 50 ); } /* Robots */ $rm_robots = get_post_meta( $pid, 'rank_math_robots', true ); if ( ! empty( $rm_robots ) ) { if ( is_string( $rm_robots ) ) $rm_robots = maybe_unserialize( $rm_robots ); if ( is_array( $rm_robots ) ) { $ni = in_array( 'noindex', $rm_robots, true ) ? 'noindex' : 'index'; $nf = in_array( 'nofollow', $rm_robots, true ) ? 'nofollow' : 'follow'; $robots_final = $ni . ',' . $nf; } else { $robots_final = sanitize_text_field( (string) $rm_robots ); } $existing_robots = agenli_get_post_meta( $pid, 'seo_robots', '' ); if ( ! $existing_robots || $overwrite ) { if ( 'live' === $mode ) { agenli_save_post_meta( $pid, 'seo_robots', $robots_final ); } $row['changes'][] = 'seo_robots: ' . $robots_final; } else { $row['skipped'][] = 'seo_robots'; } } if ( ! empty( $row['changes'] ) ) { $stats['migrated']++; } else { $stats['skipped']++; } $results[] = $row; } wp_send_json_success( array( 'mode' => $mode, 'stats' => $stats, 'results' => $results, ) ); } function agenli_page_import() { $agenli_active_page = 'agenli-import'; /* ── Meta key mapping: Rank Math → AgencyBoost ── */ $agenli_rm_map = array( 'rank_math_title' => 'seo_title', 'rank_math_description' => 'seo_desc', 'rank_math_focus_keyword' => 'focus_kw', 'rank_math_canonical_url' => 'canonical', 'rank_math_facebook_title' => 'og_title', 'rank_math_facebook_description' => 'og_desc', 'rank_math_twitter_title' => 'og_title', /* fallback if no FB title */ 'rank_math_twitter_description' => 'og_desc', /* fallback if no FB desc */ ); /* ── Handle DRY RUN or LIVE migration ── */ $agenli_mode = ''; $agenli_results = array(); $agenli_stats = array( 'total' => 0, 'migrated' => 0, 'skipped' => 0, 'errors' => 0 ); if ( isset( $_POST['agenli_import_rankmath'] ) ) { check_admin_referer( 'agenli_import_rankmath' ); if ( ! current_user_can( 'manage_options' ) ) wp_die( 'Unauthorised' ); $agenli_mode = sanitize_key( $_POST['agenli_import_rankmath'] ); $agenli_overwrite = ! empty( $_POST['agenli_overwrite'] ); $agenli_post_types = isset( $_POST['agenli_post_types'] ) ? array_map( 'sanitize_key', (array) $_POST['agenli_post_types'] ) : array( 'post', 'page' ); global $wpdb; /* Get all posts that have ANY Rank Math data */ $agenli_rm_keys_sql = implode( "','", array_keys( $agenli_rm_map ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery $agenli_post_ids = $wpdb->get_col( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching "SELECT DISTINCT post_id FROM {$wpdb->postmeta} // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching WHERE meta_key IN ('{$agenli_rm_keys_sql}')" // phpcs:ignore ); /* Also include rank_math_robots separately (needs special handling) */ // phpcs:ignore WordPress.DB.DirectDatabaseQuery $agenli_robot_ids = $wpdb->get_col( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching "SELECT DISTINCT post_id FROM {$wpdb->postmeta} // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching WHERE meta_key = 'rank_math_robots'" ); $agenli_post_ids = array_unique( array_merge( $agenli_post_ids, $agenli_robot_ids ) ); foreach ( $agenli_post_ids as $agenli_pid ) { $agenli_pid = (int) $agenli_pid; $agenli_post = get_post( $agenli_pid ); if ( ! $agenli_post ) continue; /* Filter by selected post types */ if ( ! in_array( $agenli_post->post_type, $agenli_post_types, true ) ) continue; if ( 'publish' !== $agenli_post->post_status && 'draft' !== $agenli_post->post_status ) continue; $agenli_stats['total']++; $agenli_row = array( 'id' => $agenli_pid, 'title' => $agenli_post->post_title, 'type' => $agenli_post->post_type, 'url' => get_permalink( $agenli_pid ), 'changes' => array(), 'skipped' => array(), ); /* ── Migrate standard fields ── */ foreach ( $agenli_rm_map as $agenli_rm_key => $agenli_ab_key ) { $agenli_rm_val = get_post_meta( $agenli_pid, $agenli_rm_key, true ); if ( empty( $agenli_rm_val ) ) continue; /* Check if AgencyBoost already has data for this field */ $agenli_existing = agenli_get_post_meta( $agenli_pid, $agenli_ab_key ); if ( $agenli_existing && ! $agenli_overwrite ) { $agenli_row['skipped'][] = $agenli_ab_key . ' (already set)'; continue; } /* OG image: Rank Math stores as URL or attachment ID */ if ( 'og_image' === $agenli_ab_key ) { $agenli_image_val = $agenli_rm_val; /* If it's numeric, it's an attachment ID - get the URL */ if ( is_numeric( $agenli_rm_val ) ) { $agenli_image_url = wp_get_attachment_url( (int) $agenli_rm_val ); if ( $agenli_image_url ) $agenli_image_val = $agenli_image_url; } /* rank_math_facebook_image stores as array sometimes */ if ( is_array( $agenli_rm_val ) && ! empty( $agenli_rm_val['url'] ) ) { $agenli_image_val = $agenli_rm_val['url']; } if ( 'dry_run' !== $agenli_mode ) { agenli_save_post_meta( $agenli_pid, $agenli_ab_key, $agenli_image_val ); } $agenli_row['changes'][] = 'og_image ← ' . esc_html( substr( $agenli_image_val, 0, 50 ) ) . '...'; continue; } if ( 'dry_run' !== $agenli_mode ) { agenli_save_post_meta( $agenli_pid, $agenli_ab_key, $agenli_rm_val ); } $agenli_row['changes'][] = $agenli_ab_key . ' ← "' . esc_html( substr( $agenli_rm_val, 0, 60 ) ) . '"'; } /* ── Robots: Rank Math stores as serialized array ── */ $agenli_rm_robots = get_post_meta( $agenli_pid, 'rank_math_robots', true ); if ( ! empty( $agenli_rm_robots ) ) { /* Rank Math robots is array: ['noindex','nofollow'] or ['index','follow'] */ if ( is_string( $agenli_rm_robots ) ) { $agenli_rm_robots = maybe_unserialize( $agenli_rm_robots ); } if ( is_array( $agenli_rm_robots ) ) { $agenli_robots_str = implode( ',', $agenli_rm_robots ); } else { $agenli_robots_str = (string) $agenli_rm_robots; } /* Normalize: ensure it has both index and follow parts */ $agenli_has_index = ( strpos( $agenli_robots_str, 'noindex' ) !== false ) ? 'noindex' : 'index'; $agenli_has_follow = ( strpos( $agenli_robots_str, 'nofollow' ) !== false ) ? 'nofollow' : 'follow'; $agenli_robots_final = $agenli_has_index . ',' . $agenli_has_follow; $agenli_existing_robots = agenli_get_post_meta( $agenli_pid, 'seo_robots', '' ); if ( ! $agenli_existing_robots || $agenli_overwrite ) { if ( 'dry_run' !== $agenli_mode ) { agenli_save_post_meta( $agenli_pid, 'seo_robots', $agenli_robots_final ); } $agenli_row['changes'][] = 'seo_robots ← "' . esc_html( $agenli_robots_final ) . '"'; } else { $agenli_row['skipped'][] = 'seo_robots (already set)'; } } /* ── Focus keywords: Rank Math allows comma-separated ── */ $agenli_rm_kws = get_post_meta( $agenli_pid, 'rank_math_focus_keyword', true ); if ( ! empty( $agenli_rm_kws ) ) { /* Take first keyword as primary focus kw */ $agenli_kw_arr = explode( ',', $agenli_rm_kws ); $agenli_focus_kw = trim( $agenli_kw_arr[0] ); $agenli_extra_kws = array_slice( $agenli_kw_arr, 1 ); if ( ! agenli_get_post_meta( $agenli_pid, 'focus_kw' ) || $agenli_overwrite ) { if ( 'dry_run' !== $agenli_mode ) { agenli_save_post_meta( $agenli_pid, 'focus_kw', $agenli_focus_kw ); if ( ! empty( $agenli_extra_kws ) ) { agenli_save_post_meta( $agenli_pid, 'seo_keywords', implode( ', ', $agenli_extra_kws ) ); } } } } if ( ! empty( $agenli_row['changes'] ) ) { $agenli_stats['migrated']++; } else { $agenli_stats['skipped']++; } $agenli_results[] = $agenli_row; } } /* ── Check if Rank Math is installed ── */ $agenli_rm_active = class_exists( 'RankMath' ) || defined( 'RANK_MATH_VERSION' ); $agenli_rm_data = false; if ( ! $agenli_rm_active ) { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery $agenli_rm_count = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching "SELECT COUNT(*) FROM {$wpdb->postmeta} WHERE meta_key = 'rank_math_title'" // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching ); $agenli_rm_data = ( $agenli_rm_count > 0 ); } $agenli_all_types = get_post_types( array( 'public' => true ), 'objects' ); ?>
Migrate all your Rank Math SEO data to AgencyBoost without losing a single page's optimisation.