Archived topic
Hook in Elements fire several times
8 replies · Started by KAMADEVA AB on May 20, 2021
Hello,
I wanted to ask regarding hooks in Element.
I have a hook like this:
<?php
add_action( 'wpsp_inside_image_container', function( $settings ) {
if ( 4118 === $settings['list_id'] ) {
$wpsp_category = get_the_category();
if($wpsp_category){echo '<span class="cat_links"><span class="'.$wpsp_category[0]->slug.'"><a href="'.get_category_link( $wpsp_category[0]->term_id ).'">'.$wpsp_category[0]->name.'</a></span></span>';}
}
});
?>
When used in elements with custom hook in settings "wpsp_inside_image_container" it fires 5-6 (!?) times for each post that is shown.
Same code used in Snippets plugin only runs 1 time every post.
Any reason why it if fired multiple times in Element?
I guess it has to do with using
add_action( 'wpsp_inside_image_container', function( $settings ) {
But if I don't use "add_action" here I can't check " $settings['list_id']" from WPSP in Elements ....
Hi there,
yeah you shouldn't use add_action inside a Hook Element. Try hooking in the code:
<?php
if ( 4118 === $settings['list_id'] ) {
$wpsp_category = get_the_category();
if($wpsp_category){echo '<span class="cat_links"><span class="'.$wpsp_category[0]->slug.'"><a href="'.get_category_link( $wpsp_category[0]->term_id ).'">'.$wpsp_category[0]->name.'</a></span></span>';}
}
?>
And set the Hook to Custom Hook and enter in the field provided: wpsp_inside_image_container
Hi David,
I did. If I do, the if statement is not working anymore and it don't work inside Element. T ried to echo out the "$settings['list_id']" but it has no value.
Hi Martin,
Can you explain a bit more about what you're trying to do? Perhaps we can offer a more appropriate solution.
Note: We don't recommend using add_action() codes within the Hook Element. It wasn't made for that. If you wish to hook in a specific value within WPSP posts, do it with a Child Theme's functions.php or a Code Snippets plugin.
Hi Elvin,
So if I want to use Element and use some criteria/value from WPSP, then ELement is not good?
Basically I wanted to use Element to run most of the snippets and have them in one place, as long they are specific to one location and not the whole site.
So if I want to use Element and use some criteria/value from WPSP, then ELement is not good?
For this kind of thing, it isn't.
This is actually quite a good example of why some PHP snippets shouldn't be run in Hook Element.
I believe this thread here is yours as well? I've explained why Hook element isn't the best thing to use here.
https://wpshowposts.com/support/topic/hook-and-condition/#post-30422
In this case, it's basically because we can't control the variable gets passed to the hook used on the Hook Element.
Now I understand that you lose the display rule if you do this in the code snippets plugin. Yes that's an issue but we can fix that by adding if conditions within the add_action() hooks.
Now I understand that you lose the display rule if you do this in the code snippets plugin. Yes that’s an issue but we can fix that by adding if conditions within the add_action() hooks.
Ahhh, will look into that myself, it is actually easy when I think about it. The theme is so blazing fast, that it don't matter really so much in the end, but seems it is sometimes not necessary to run the PHP snippet in front in general, if it is only used in one case (this case Front page). But I learned more about Elements :)
Thank you.
Glad you learned something from it. Let us know if you need further help. :)