0

I want to replace my custom gallery shortcode in my post content with the default Wordpress shortcode (in pre get posts). I will be doing this in only a certain instance where it will need to be formatted in the default gallery shortcode format (in order to work with exporting to Apple News).

How can I search through the content for my custom gallery shortcode:

[showgallery id="1234"]

And then replace it with the default one:

[gallery ids="1234,1235,1236,1237,1238"]

I can handle the changing it from one id to the several ids by just creating a custom function that will just grab the one id, fetch that post, grab the array of images within that post (I use ACF galleries), and then return the list of ids.

So I just need to know how to search through the content, find each showGallery shortcode, and then for each of those shortcodes grab the id, send it to my custom function, remove the old shortcode, and then replace it with the new one that would be returned by my function.

I found this which seems close, but still can't figure out fully how to remove the old one, etc.

Extract attribute values from every shortcode in post

1 Answer 1

0

Either make a regex filter as a the_content filter,

add_filter('the_content', 'my_regex_filter', 1); // Execute as early as possible

function my_regex_filter($content) {
    $regex = get_shortcode_regex(array('showgallery'));
    $content = preg_replace('/'.$regex.'/s', '[$1gallery$3$4$5$6]', $content );
    return $content;
}

or rewrite your add_shortcode function.

remove_shortcode('showgallery');
add_shortcode('showgallery', 'my_showgallery');

function my_showgallery($args) {
    return gallery_shortcode($args);
}

I would recommend the second, because it is cleaner and faster.

Edit:

Rewriting the shortcode for external plugins.

In your functions.php define the plugin version of your shortcode:

remove_shortcode('showgallery');
add_shortcode('showgallery', 'my_showgallery');

function my_showgallery($args) {
    return gallery_shortcode($args);
}

function my_template_gallery($args) {
    // Here put your custom gallery code…
}

Add to your header.php the following two lines:

remove_shortcode('showgallery');
add_shortcode('showgallery', 'my_template_gallery');

Now you got the rendered WP shortcode everywhere except on your templates.

9
  • I was thinking of something like your first answer, but the actual TODO: part is what I would need input/help with figuring out.
    – KKranzo
    Mar 18, 2016 at 15:32
  • updated @KKranzo ;) edit i'm not sure about the regex flags...
    – iantsch
    Mar 18, 2016 at 15:33
  • And I can't really rewrite the add_shortcode because this would need to run only when the content was being pulled by a certain plugin (the export to Apple News plugin).
    – KKranzo
    Mar 18, 2016 at 15:34
  • this is also possible... remove the old shortcode > add the new > filter content > remove the new > add the old
    – iantsch
    Mar 18, 2016 at 15:35
  • It looks like your updated first answer is close, but I would need to locate each shortcode, run a function using the id attribute from the shortcode, then replace the shortcode with the return/output of the function. So it seems like there needs to be a step in between the $regex line and $content line in your example, correct?
    – KKranzo
    Mar 18, 2016 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.