Home › Forums › Pro Support › Hook and condition
- This topic has 6 replies, 2 voices, and was last updated 2 years, 4 months ago by
elvin.
-
AuthorPosts
-
May 19, 2021 at 4:14 am #30382
Martin
ParticipantHello,
I have used this hook with condition for certain wp-shop-post in snippets-plugin and it works:
add_action( 'wpsp_inside_image_container', function( $settings ) { if ( 4118 === $settings['list_id'] ) { //do something } });
Tried to use the same condition in GP Elements, with hook wpsp_before_title .
<?php if ( $settings['list_id'] === 4118 ) { // do something } ?>
The condition don’t really work in Elements. If “if”-statement and condition removed it works. If used in Elements should the condition look different?
May 20, 2021 at 4:31 am #30409elvin
ModeratorHi there,
It won’t work because the
$settings
variable is only passed to WPSP filters or action hooks.Your second code didn’t specify the hook so the settings variable value coming from the plugin doesn’t exist in it.
May 20, 2021 at 4:48 am #30413Martin
ParticipantBut I did specify a hook in the element, in the settings for the hooks below the editor. I guess it is not enough … Thank you.
May 20, 2021 at 6:34 am #30416Martin
ParticipantBut is this not the point with Elements, that the code should be
if ( 4118 === $settings['list_id'] ) { //do something }
when “wpsp_inside_image_container” is set in the settings of the Element and not
add_action( 'wpsp_inside_image_container', function( $settings ) { if ( 4118 === $settings['list_id'] ) { //do something } });
??
May 20, 2021 at 4:51 pm #30422elvin
ModeratorThe problem lies with passing of variables.
While GPP’s Hook element lets you use
wpsp_inside_image_container
, the issue here is that, the$settings
variable isn’t passed when you use it because we can’t specify what values can be passed on the Elements when we use the hooks.When you did this: https://ibb.co/mcPrJqz and wrote this code:
if ( 4118 === $settings['list_id'] ) { //do something }
The literal code translation is –
add_action( 'wpsp_inside_image_container', function( ){ if ( 4118 === $settings['list_id'] ) { //do something } } );
Notice that there’s no
$settings
variable passed within thefunction()
. That’s why when you did a echo out the$settings['list_id']
, no value was shown.This is different from writing the code manually on code snippets like this –
add_action( 'wpsp_inside_image_container', function( $settings ){ if ( 4118 === $settings['list_id'] ) { //do something } } );
..because code snippets actually allow us to specify which variable to pass to the function. (in this case, its
$settings
)May 20, 2021 at 11:22 pm #30433Martin
ParticipantGot it. Thank you.
May 20, 2021 at 11:40 pm #30440elvin
ModeratorNo problem. 😀
-
AuthorPosts
- You must be logged in to reply to this topic.