Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Dynamic Element

  • I have created two dynamic elements.

    If the subscriber number is >100 then the newsletter subscription will appear.
    If the subscriber number is <100 then the subscription form will appear for users to sign up.

    This is my code line:

    add_filter( ‘generate_element_display’, function( $display, $element_id ) {
    if (function_exists(‘rcp_count_memberships’)) {
    $total_suscriptores = rcp_count_memberships(array(‘status’ => ‘active’));
    $total_suscriptores = 101; //101
    if($total_suscriptores <= 100 && 3474 === $element_id){
    $display=false;
    }elseif( $total_suscriptores > 100 && 3492 === $element_id) {
    $display=false;
    }//fin_else
    }
    return $display;
    }, 10, 2 );

    Then when I change the $total_subscribers to “101” or “0”, no change is made, I don’t know why. So I have to open the two blocks, Element Id 3474 and Element Id 3492, I hit save on both blocks without making any changes, and this moment it dynamically appears the block that has to appear,

    Why does this happen and does not make the changes according to the parameter total_subscribers dynamically?

  • Hi there,

    what are the elements you’re using e.g a Block element ?

  • The type Element is a Hook.

    You can take a look in this link

    https://share.zight.com/Bluv9EDR#discussion

  • And if you remove the PHP, do BOTH the elements display ?

  • No, only one of them is displayed

  • Do they both have the display rules set to the same location ?
    AS that would be required.

  • Exactly.

    I use three browsers Safary, Chrome and Firefox to test the functionality. I have managed to display the two elements in Safary.

    Here are the credentials in case you need to take a look at them.

    Some questions what I have:
    Could it be a caching problem? How can a block change dynamically if the content is cached? How can the block be made non-cached? I use the plugin LiteSpeed Cache

  • I did not see either of the elements showing on the front page.

    How did you define the custom hook?

  • Hi, the name is “my_custom_hook”

    You can also take a look, here
    https://share.zight.com/Bluv9EDR#discussion

  • Hi, the name is “my_custom_hook”

    I know, I mean how did you define the hook? Who told you to use the “my_custom_hook” hook? As GP does not have this hook name. So there must be custom PHP code to create this custom hook. let me know!

    No, only one of them is displayed

    I did not see any of them displaying on the front page.

  • Hi Ying, This is the code (but the following snippet is deactivated now):

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        if (function_exists('rcp_count_memberships')) {
            $total_suscriptores = rcp_count_memberships(array('status' => 'active'));
             $total_suscriptores = 101
            if($total_suscriptores <= 100 && 3454 === $element_id){ // Oculta Newsletter Block si los suscriptores <=100
    	  $display=false;
    	}elseif( $total_suscriptores > 100 && 3462 === $element_id) { // Oculta Subscriber Block si los suscriptores <=100
             $display=false;
     }//fin_else   
        }
        return $display;
    }, 10, 2 );

    If I activate this snippet, the Hook Element (Newsletter) should appear.

    “my_custom_hook” hook is the custom name of the Hook element I use for both elements Should I other custom name?

    You can take a look on my website, you have the credentials in the private information.

  • The code is for showing/hiding specific elements on condition, however, you also need custom code to create the custom hook.

    Can you activate the code so I can see if the custom hook is working?

  • I have just activated the code

  • “my_custom_hook” hook is the custom name of the Hook element I use for both elements Should I other custom name?

    The my_custom_hook does NOT exist, unless you use code to add it your site.

    Instead use the same Hook name as your other element.

  • I don’t understand what you means. I don’t know if we are speaking about the same thing.

    If you take a look at this screenshot.
    https://share.zight.com/Bluv9EDR#discussion

    Currently, I have in both, in the field customize hook name “my_custom_hook” (how is currently on the setup). Have I to change it to other name? for example:

    -In the setup of the Hook Element (Newsletter block) , in the field customize hook name “elemento-Dnmc”
    -In the setup of the Hook Element (Subscriber block) , in the field customize hook name “”elemento-Dnmc”

    is this correct? If this is not correct, can you explain it step by step, what you want means, please?

    let’s not forget that what I want to achieve is that, depending on a parameter, it will show a newsletter block or a subscriber block appears dynamically.

  • In the elements setting sidebar, there is list of Hooks.

    https://docs.generatepress.com/article/block-element-hook/

    The List contains Hooks that GeneratePress makes available to you.
    As those Hooks are hard coded into the Theme.

    And here we provide a quick visual guide for where Hooks are located.

    https://docs.generatepress.com/article/hooks-visual-guide/

    So using that visual guide, where on the page do you want the Block Elements to appear ?

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