6

I am using the Transients API to hold the output for a gallery plugin's shortcode I created. Due to the number of images used and different layouts needed, the transient name looks lik the following:

_transient_galleries_single_shortcode_5183five

So - the int portion is the post ID it is referencing and the five is this specific shortcode's column setting. Where I'm running into trouble is flushing these shortcodes on-save of the referenced galleries.

What I'd normally do is something like following:

function galleries_delete_transient($post_id) {
    switch (get_post_type()) {
        case 'galleries':
            delete_transient('galleries_single_shortcode_'.$post_id);
        break;
    }
}
add_action('save_post','galleries_delete_transient');

The problem being here is that the column setting can be different based on a certain page's needs - so I was wondering if there is any type of wildcard I can use here to make this a bit more dynamic (to just clear all of them - something like delete_transient('galleries_single_shortcode_'.$post_id%);). Any help would be greatly appreciated. Thanks!

Update

Complete shortcode function added for clarity how everything is constructed:

function galleries_single_shortcode($atts, $content = null) {
    extract(shortcode_atts(array(
            "id"     => '',
            "columns" => 'four'
    ), $atts));

    global $galleries_options;

    $galleries_single_shortcode_output = get_transient('galleries_single_shortcode_'.$id.$columns);
    if ($galleries_single_shortcode_output === false) {

        ob_start();

        // OUTPUT HERE

        $galleries_single_shortcode_output = ob_get_contents();

        ob_end_clean();

        set_transient('galleries_single_shortcode_'.$id.$columns, $galleries_single_shortcode_output, 60 * 60 * 24);

    }


    return $galleries_single_shortcode_output;
}
add_shortcode('galleries_single', 'galleries_single_shortcode');
6
  • How do you determine the number of columns? Asked another way: how do you determine/construct the correct transient name? Knowing that method would help answer the question. Commented Nov 19, 2012 at 13:57
  • Hi @ChipBennett - added the wrapper function around the shortcode for clarity - in essence, manually added by the user [galleries_single id=123 columns=four]. Thanks!
    – Zach
    Commented Nov 19, 2012 at 14:07
  • Why are you saving the column setting as part of the transient key instead of as part of the data?
    – s_ha_dum
    Commented Nov 19, 2012 at 14:43
  • @s_ha_dum So if the user calls the same gallery on two different pages, but needs different column settings there won't be any conflicts and they are kept unique
    – Zach
    Commented Nov 19, 2012 at 14:52
  • I don't think the transients function accepts wildcards. It would be something like delete_transient('galleries_single_shortcode_'.$post_id.'%%');) if it did, though. I assume you've tested that.
    – s_ha_dum
    Commented Nov 19, 2012 at 15:09

1 Answer 1

8

Forgot I could go this route:

$wpdb->query( "DELETE FROM `$wpdb->options` WHERE `option_name` LIKE ('_transient_galleries_%')" );
1
  • 7
    While this works for you it's important to note that this won't work for everybody. If you're using memcache (or similar), transients are not stored in the database. Commented Feb 20, 2013 at 23:11

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.