Duplicate Event – _oembed_ Postmeta Causing Database Error
-
I’m getting an error when using the duplicate function to duplicate an event that contains a YouTube video block.
Problem Seems to Be
The
_oembed_...meta key contains unescaped HTML with apostrophes and quotes, and the SQL statement being constructed likely does something like:INSERT INTO wp_postmeta (post_id, meta_key, meta_value) VALUES (123, '_oembed_...', 'Video can't be...')That
'incan'tisn’t escaped, so it breaks the query.Error Log
WordPress database error You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 't be loaded because JavaScript is disabled:
22448, '_oembed_674b1ab4ab02f7731dd6c3290b28c534', '<div class="container-lazyload preview-lazyload container-youtube js-lazyload--not-loaded"><a href="https://www.youtube.com/watch?v=QOgzEHWwR6I" class="lazy-load-youtube preview-lazyload preview-youtube" data-video-title="War Journalism from Vietnam to Gaza – How War Coverage Impacts U.S. Foreign Policy" title="Play video "War Journalism from Vietnam to Gaza – How War Coverage Impacts U.S. Foreign Policy"">https://www.youtube.com/watch?v=QOgzEHWwR6I</a><noscript>Video can't be loaded because JavaScript is disabled: <a href="https://www.youtube.com/watch?v=QOgzEHWwR6I" title="War Journalism from Vietnam to Gaza – How War Coverage Impacts U.S. Foreign Policy">War Journalism from Vietnam to Gaza – How War Coverage Impacts U.S. Foreign Policy (https://www.youtube.com/watch?v=QOgzEHWwR6I)</a></noscript></div>'), (22448, '_oembed_time_674b1ab4ab02f7731dd6c3290b28c534', '1746134347') made by require_once('wp-admin/admin.php'), require_once('wp-load.php'), require_once('wp-config.php'), require_once('wp-settings.php'), do_action('wp_loaded'), WP_Hook->do_action, WP_Hook->apply_filters, em_init_actions_start, EM_Event->duplicateTemporary Fix
I added a filter to prevent duplicating
_oembed_postmeta fields (they aren’t needed anyway), which seems to be working./**
* Remove oembed postmeta
* Don't duplicate postmeta with key _oembed_ when duplicating an event
*
* @param \EM_Event $original
* @param \EM_Event $duplicate
*
* @return void
*/
function remove_oembed_meta( \EM_Event $original, \EM_Event $duplicate ): void {
$meta_keys = get_post_custom_keys( $duplicate->post_id );
if ( empty( $meta_keys ) ) {
return;
}
foreach ( $meta_keys as $meta_key ) {
if ( strpos( $meta_key, '_oembed_' ) === 0 ) {
delete_post_meta( $duplicate->post_id, $meta_key );
}
}
}
\add_action( 'em_event_duplicate_pre', __NAMESPACE__ . '\remove_oembed_meta', 10, 2 );
The topic ‘Duplicate Event – _oembed_ Postmeta Causing Database Error’ is closed to new replies.