Just to clarify, I am asking what part of the plugin’s code is called when a shown shortcode becomes hidden. I’d like to call some PHP code when something becomes hidden.
Hi, justaguyyo.
Unfortunately, there are currently no hooks into the plugin where you could perform some action based on the state of the shortcode. Which shortcode are you trying to use? If you’re willing to be a guinea pig, I may be able to whip something up.
Thanks for the fast reply! I am not trying to use shortcode, I am trying to use PHP to update a variable. Specifically, update_option(CF7_COUNTER, 0);
Right, but you want to do it based on the status of one of the Timed Content shortcodes, is that correct?
Yeah. I’d like it so when it hides it updates the value. Is this a really complex thing I’ve asked?
It would be possible to add hooks into whether a particular shortcode is showing or hiding content, but it wouldn’t be realtime interaction (i.e., interaction would depend on a page visit/reload before, during, or after a specified time). Would something like that still be useful to you?
Yep! That’s exactly what I’d like to do!
Perfect. Like I said, if you’re willing to be a guinea pig and edit the plugin on your end, I think we can try something out. It sounds like you want to trigger this behaviour based on the [timed-content-server] shortcode, is that right?
I am actually using a [timed-content-rule] it is fine if this command is called over and over. I’d just like to add it to update the value when the plugin determines it is time to hide something.
Yes, I’d love to edit the plugin and be a guinea pig. Thank you a lot for your fast replies!
Great! Go to ~line 1080 in the rulesShowHTML function and replace the if/else block with this:
global $post;
if ( ( ( $rule_is_active == true ) && ( $action_is_show == true ) ) || ( ( $rule_is_active == false ) && ( $action_is_show == false ) ) ) {
do_action("timed_content_rule_show", $post->post_id, $id, $content);
return str_replace(']]>', ']]>', apply_filters($the_filter, $content));
} else {
do_action("timed_content_rule_hide", $post->post_id, $id, $content);
return "";
}
That gives you two action hooks: timed_content_rule_show and timed_content_rule_hide, which pass the current post ID, rule ID and the enclosed content to your hooked functions. From there, add something like this to your theme’s functions.php:
function my_updated_counter($post_id, $rule_id, $content) {
// Now, you could check $rule_id and only update the counter
// when running a specific rule, or $post_id to only update
// the counter when a specific page/post is loaded, or you could
// check $content to see if it contains your Contact Form 7 shortcode.
update_option(CF7_COUNTER, 0);
}
add_action( "timed_content_rule_hide", "my_updated_counter", 10, 3 );
I’ve been using and trying to mess with the code to see how it works right now. For some reason it is completely hit or miss. I am only using the code I linked in the start and the code you gave me. There are no other modifications on my end. I am monitoring the value in phpMyAdmin and double checking by using the counter.
Sometimes it works and resets the counter but most of the time it does not. I have tried using both the hide and show action, tried setting it in advance, then letting the action start and then letting the action end, still no change. I have also tried starting the action immediately and ending it later, which does work sometimes but doesn’t always. I am really confused as to why this is only working sometimes and not always. PHP feels like magic to me at this point.
Assuming you placed the tutorial code in functions.php, my my_updated_counter function and call to add_action should be placed after it since the hooked function relies on CF7_COUNTER being defined. Also, it’s going to be run every time a [timed-content-rule] shortcode is processed, so you may want to test for your specific rule, like this:
if ( $rule_id == 3 ) // for example, your rule ID is probably different
update_option(CF7_COUNTER, 0);
Finally, hooking into timed_content_rule_hide means my_updated_counter will be run whenever your content is not being displayed to site visitors.
One other thing to check for is if you are running any caching plugins; if so, you will want to make an exception for the page/post your shortcode is on.
AHHH!! Thank you so much for all of your help K. Tough! Your code works, and I finalled realized why it wasn’t working before. It wasn’t working before becuase.. DUH the code is only called when the page with the timed content shortcode! Thank you a bunch. You’ve been amazing, your plugin is amazing and you are incredibly helpful!!