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.

Show element based on URL

  • Hello, just trying out elements for the first time, moving a few JavaScript snippets that we previously added via another plugin.

    One snippet I want to show if part of the URL contains one of a number of words/part-of-words (thank, danke etc). This doesn’t seem possible in the admin settings, but I was trying this code https://generatepress.com/forums/topic/elements-block-hook-location-for-specific-url/#post-1669018, however I can’t get it to work. As ID I use the ID from the element page (../wp-admin/post.php?post=7765) and as word I use ‘danke’. However, when I visit the website https://mysite.com/danke/ the script doesn’t show.

    Not sure if this function has changed, or what else can be wrong. Location rules (or any other display rules) are empty. Another standard element with location rule set to all pages shows fine.

    Any ideas on what could be wrong?

    Also, how could I adapt the script (when it works), so that it works with WPML?

    – I won’t translate the element into different languages, so it will be using the ID from the base language.
    – I want to check for different words in the URL string, not just one.

    Thanks!

  • Hi there,

    Can you show me your code?

  • Hi Ying, this is what I’m using:

    add_filter( 'generate_block_element_display', function( $display, $element_id ) {
        if ( 7765 === $element_id && strpos($_SERVER['REQUEST_URI'], 'danke') !== false ) {
            $display = true;
        }
        return $display;
    }, 10, 2 );
  • Are you using a hook element? If so, use generate_element_display filter instead of generate_block_element_display.

    add_filter( 'generate_element_display', function( $display, $element_id ) {
        if ( 7765 === $element_id && strpos( strtolower($_SERVER['REQUEST_URI']), 'danke' ) !== false ) {
            return true;
        }
        return $display;
    }, 10, 2 );
  • Hi Ying, excellent, many thanks! Yes I was using the wp_footer hook, when I changed it to generate_element_display, it started showing.

    Thanks!

  • Glad to hear that 🙂

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