Created
July 18, 2018 09:46
-
-
Save marko-stimac/c3a1736dc899e7d8a5f49dd82b6e3180 to your computer and use it in GitHub Desktop.
Wordpress - insert shortcode for gallery when importing images into posts with WP All Import Pro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('pmxi_saved_post', 'update_gallery_ids', 10, 3); | |
function update_gallery_ids($id) { | |
$attached_images = get_attached_media( 'image', $id ); | |
// Prepare images into array | |
$arr = array(); | |
foreach($attached_images as $image) | |
array_push($arr, $image->ID); | |
// Prepare shortcode | |
$string = '<p>[gallery ids="'. implode(", ",$arr) . '"]</p>'; | |
// Prepare post for updating | |
$content_post = get_post($id); | |
$content = $content_post->post_content; | |
$my_post = array(); | |
$my_post['ID'] = $id; | |
$my_post['post_content'] = $content . $string; | |
// Save post | |
wp_update_post( $my_post ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment