Site logo

Archived topic

Pulling an array's index-key at the time of using dynamic content

11 replies · Started by Hardik on December 12, 2021

Viewing posts 1–12 of 12

Hello,

I'm designing the author box's social media profile as per the instructions by Leo. I use The SEO Framework, which stores Facebook and Twitter user-meta as an array. According to the author, I need to pull the array’s index-key in order to access the field contents.

How do I achieve this using Generateblock's Dynamic Content?

Thank you!

Hi there,

Tricky one - the code itself isn't set up to deal with arrays.

Can you share more about the block you're trying to add dynamic data to? Is it a Button or Headline? Is it for a link or for the text?

We may be able to work something out with a filter.

Hello,

I'm trying to add the dynamic link type as "Author Meta" for the button. The button is the current post's author's social media profile.

Regards.

We could try this:

add_filter( 'generate_dynamic_element_url', function( $url, $link_type, $source, $block ) {
    if ( 'user-meta' === $link_type ) {
        global $post;
        $author_id = $post->post_author;

        $meta_name = $block['attrs']['gpDynamicLinkCustomField'];
        $seo_framework_data = get_user_meta( $author_id, 'autodescription-user-settings' );

        if ( $seo_framework_data && $meta_name && isset( $seo_framework_data[ $meta_name ] ) ) {
            $url = $seo_framework_data[ $meta_name ];
        }
    }

    return $url;
}, 10, 4 );

So you'd add the twitter_page name as the Custom Field name in the options, and this should (in theory) look it up in the array.

Let me know :)

I corrected a syntax error on line 9 and added it using the Code Snippets plugin, but the button still won't appear. What could be the issue here? This is how the Twitter button should appear, but frontend blog-posts show a blank area.

We'll need to debug a little.

After this line: $seo_framework_data = get_user_meta( $author_id, 'autodescription-user-settings' );

Add this: var_dump($seo_framework_data);

Does this output anything on the frontend?

Yup, I see something.

And this is how it is for the button element

Hi there,

Can you try doing a var_dump of $seo_framework_data[0][$meta_name] if it displays the url.

If it does, you can use that for $url value.

Example:

add_filter( 'generate_dynamic_element_url', function( $url, $link_type, $source, $block ) {
    if ( 'user-meta' === $link_type ) {
        global $post;
        $author_id = $post->post_author;

        $meta_name = $block['attrs']['gpDynamicLinkCustomField'];
        $seo_framework_data = get_user_meta( $author_id, 'autodescription-user-settings' );

        if ( $seo_framework_data && $meta_name && isset( $seo_framework_data[ $meta_name ] ) ) {
            $url = $seo_framework_data[0][$meta_name];
        }
    }

    return $url;
}, 10, 4 );

Hello,

I did var_dump($seo_framework_data[0][$meta_name]); and it did display the URL.

However, the button remains invisible at the frontend, as if it didn't get the value.

This should be the magic code that fixes it:

add_filter( 'generate_dynamic_element_url', function( $url, $link_type, $source, $block ) {
    if ( 'user-meta' === $link_type ) {
        global $post;
        $author_id = $post->post_author;

        $meta_name = $block['attrs']['gpDynamicLinkCustomField'];
        $seo_framework_data = get_user_meta( $author_id, 'autodescription-user-settings' );

        if ( is_array( $seo_framework_data ) && $meta_name && isset( $seo_framework_data[ 0 ][ $meta_name ] ) ) {
            $url = $seo_framework_data[ 0 ][ $meta_name ];
        }
    }

    return $url;
}, 10, 4 );

Let us know :)

Thank you so much for going over and above in helping out! Your code fixed it.

Awesome, glad we could help! :)

This archived topic is closed to new replies.