I hope somebody can help me. I need to delete a transient for all shortcodes located on a post/page when that page is updated. So, the page update should delete the transient just for the shortcodes located on that page. If that isn't possible it would help if all shortcode transients are deleted on any page update.

I tried using the save_post action but nothing happens, so i must be doing something wrong.

Also, the problem is that I'm generating a shortcode ID, based on post ID and incrementation of a static variable.

So, only when I call the method to delete a transient in shortcode-template.php i have access to the current's page ID and then I can delete a transient related to page ID/Shortcode ID combination. But then the transient is deleted every time the shortcode is loaded. And if i don't call the delete_transient function, it seems like it is not hooked to save_post action, because nothing happens.

I guess that is because the current page ID can't be fetched in a Class but only when used somewhere on the page. But I have no idea how to go around that.

Here is my Class:

  class MainClass {

            private static $shortcode_increment = 0;

            private function __construct() {
                add_action( 'save_post', array( $this, 'update_page_delete_transient') );

            public static function increment_id() {
                global $post;


                return 'shortcode_' . $post->ID . "_" . self::$shortcode_increment;

            // delete the shortcode cache on page update
            public function update_page_delete_transient() {

            delete_transient( 'weather_'.self::increment_id() );

Any help is appreciated. Thanks.


I'm not sure, how actions should work, as i understand it, the function added to the action should be activated when that action occurs, so there is no need to call that function? Or is there?

Will the function added to the action be activated automatically after the Class is instantiated in shortcode-template.php for example, since the action itself is in the construct? Or do I need to call the delete_transient function in shortcode-template.php in order for it to work? But when I do that, it just runs every time widget is loaded, and not on save post/page.


So, if i remove the post ID from the increment ID function, I can badly resolve the issue from update 1 above. delete_transient function is activated when i create an instance of an object and it is called on page/post save.

Now the problem that remains is how do i pass the unique Shortcode ID to delete_transient function, if the ID is increased only in the shortcode-template.php when a new shortcode is created. Or it would be even better if i could somehow pass the Page ID and Shortcode ID somehow to the delete_transient function in the Main Class, so I could delete a specific transient for a specific shortcode on update.

Is there a way to pass the data from the shortcode-template.php back to the Object method?

1 Answer 1


If you will save all data of shortcodes in one variable, you can reset it easy.

Example somewhere in shortcode(pseudocode):

$page_cache = get_transient("page_{$page_id}");
if ( empty($page_cache ) ){
  global $shortcode_index;
  $page_cache[$shortcode_index] = **some data that cached**;
  set_transient( "page_{$page_id}", $page_cache, **some time** );

And then you can delete it just by page_id on post save

 delete_transient( "page_{page_id}" );

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.