| Snippet Status | Done |
| Snippet Description | - v1.2 - 18 nov
- v1.0 - 17 nov 2025
|
| Snippet Type Code | PHP |
| Snippet URL | |
| Snippet Content | * @version 1.2
* Created: AI (Gemini / GPT-5 Mini) and modified by Toine Branbergen
*/
function bulk_insert_safe_redirects($redirects) {
if ( ! function_exists( 'wp_insert_post' ) || ! function_exists( 'update_post_meta' ) ) {
error_log("Fout: WordPress functies zijn niet beschikbaar.");
return 0;
} $count = 0; foreach ( $redirects as $old_url => $new_url ) {
$post_id = wp_insert_post( [
'post_title' => sanitize_text_field( $old_url ),
'post_content' => esc_url_raw( $new_url ),
'post_status' => 'publish',
'post_type' => 'redirect_rule',
] ); if ( is_wp_error( $post_id ) ) {
error_log( "Fout bij aanmaken redirect post: " . $post_id->get_error_message() );
continue;
} // Save the actual meta keys found in the database
update_post_meta( $post_id, '_redirect_rule_from', esc_url_raw( $old_url ) );
update_post_meta( $post_id, '_redirect_rule_to', esc_url_raw( $new_url ) );
update_post_meta( $post_id, '_redirect_rule_status_code', 301 ); // Optional: keep compatibility with other keys/UI (if needed)
update_post_meta( $post_id, 'redirect_from', esc_url_raw( $old_url ) );
update_post_meta( $post_id, 'redirect_to', esc_url_raw( $new_url ) ); // Verify saved values and log if something went wrong
$saved_from = get_post_meta( $post_id, '_redirect_rule_from', true );
$saved_to = get_post_meta( $post_id, '_redirect_rule_to', true );
$saved_status = get_post_meta( $post_id, '_redirect_rule_status_code', true ); if ( empty( $saved_from ) || empty( $saved_to ) || empty( $saved_status ) ) {
error_log( "Redirect meta missing for post {$post_id}. from=" . var_export( $saved_from, true ) . " to=" . var_export( $saved_to, true ) . " status=" . var_export( $saved_status, true ) );
} $count++;
} return $count;
}
$redirect_list = [
'/product/kruidenmix-voor-lihanboutje-750g/' => 'https://www.horecakruiden.nl/product/kruidenmix-voor-lihanboutje/',
];
$inserted_count = bulk_insert_safe_redirects($redirect_list); |
| Snippet Content CSS | |
| Snippet Content HTML | |
| Snippet Content JS | |
| Snippet Content PHP | * @version 1.2
* Created: AI (Gemini / GPT-5 Mini) and modified by Toine Branbergen
*/
function bulk_insert_safe_redirects($redirects) {
if ( ! function_exists( 'wp_insert_post' ) || ! function_exists( 'update_post_meta' ) ) {
error_log("Fout: WordPress functies zijn niet beschikbaar.");
return 0;
} $count = 0; foreach ( $redirects as $old_url => $new_url ) {
$post_id = wp_insert_post( [
'post_title' => sanitize_text_field( $old_url ),
'post_content' => esc_url_raw( $new_url ),
'post_status' => 'publish',
'post_type' => 'redirect_rule',
] ); if ( is_wp_error( $post_id ) ) {
error_log( "Fout bij aanmaken redirect post: " . $post_id->get_error_message() );
continue;
} // Save the actual meta keys found in the database
update_post_meta( $post_id, '_redirect_rule_from', esc_url_raw( $old_url ) );
update_post_meta( $post_id, '_redirect_rule_to', esc_url_raw( $new_url ) );
update_post_meta( $post_id, '_redirect_rule_status_code', 301 ); // Optional: keep compatibility with other keys/UI (if needed)
update_post_meta( $post_id, 'redirect_from', esc_url_raw( $old_url ) );
update_post_meta( $post_id, 'redirect_to', esc_url_raw( $new_url ) ); // Verify saved values and log if something went wrong
$saved_from = get_post_meta( $post_id, '_redirect_rule_from', true );
$saved_to = get_post_meta( $post_id, '_redirect_rule_to', true );
$saved_status = get_post_meta( $post_id, '_redirect_rule_status_code', true ); if ( empty( $saved_from ) || empty( $saved_to ) || empty( $saved_status ) ) {
error_log( "Redirect meta missing for post {$post_id}. from=" . var_export( $saved_from, true ) . " to=" . var_export( $saved_to, true ) . " status=" . var_export( $saved_status, true ) );
} $count++;
} return $count;
}
$redirect_list = [
'/product/kruidenmix-voor-lihanboutje-750g/' => 'https://www.horecakruiden.nl/product/kruidenmix-voor-lihanboutje/',
];
$inserted_count = bulk_insert_safe_redirects($redirect_list); |
| Particle Snippet in Software | 1 |
| Particle Snippet in Boilerplate | 1 |