We're merging with GenerateBlocks! Learn more here.

[Support request] Hook and condition

Please login to receive premium support.

Support for the free plugin can be found here.

Home Forums Pro Support Hook and condition

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #30382
    Martin
    Participant

    Hello,

    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?

    #30409
    elvin
    Moderator

    Hi 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.

    #30413
    Martin
    Participant

    But 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.

    #30416
    Martin
    Participant

    But 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
    	}
    });
    

    ??

    #30422
    elvin
    Moderator

    The 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 the function(). 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)

    #30433
    Martin
    Participant

    Got it. Thank you.

    #30440
    elvin
    Moderator

    No problem. 😀

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.