Hyperlink url field outputs strange code

Support MB Builder Hyperlink url field outputs strange codeResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #13471
    HeinHein
    Participant

    Hey Guys,

    I am working on a thing which seemed simple but I am struggling with something and I need a few fresh eyes. Is it because of me or what?

    The thing I'm making is a few source link/text inside posts. So the authors can have enter the sources of some stuff which has been written inside the article.

    No I've added this code inside functions.php (don't mind the default values, it's just for testing purposes)

    add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' );
    function your_prefix_register_meta_boxes( $meta_boxes ) {
        $meta_boxes[] = array (
            'title' => 'Micro Trends Sources',
            'id' => 'micro-trends-sources',
            'post_types' => array(
                0 => 'post',
                1 => 'page',
            ),
            'context' => 'advanced',
            'priority' => 'high',
            'fields' => array(
                array (
                    'id' => 'text_1',
                    'type' => 'text',
                    'name' => 'Source Title',
                    'size' => 50,
                    'clone' => 1,
                    'sort_clone' => 1,
                ),
                array (
                    'id' => 'url_1',
                    'type' => 'url',
                    'name' => 'Source URL',
                    'size' => 50,
                    'clone' => 1,
                    'sort_clone' => 1,
                ),
            ),
        );
        return $meta_boxes;
    }

    Looks like this in the post area

    Post area

    I need to set this up with a shortcode so I've used this.

    <a href="[rwmb_meta meta_key='url_1']">[rwmb_meta meta_key='text_1']</a>

    And it looks like this inside the post frontend area

    post front area

    Now this is all looking good and works fine. Only when I click on a link it returns a 404 error. After some head smashing to the wall, I've found that there's some extra code behind the url.

    strange code

    It trows in a closing li and ul at the end of the url. Now I think it's because of the brackets inside the shortcode or because the shortcode is wrapped inside a href tag.

    Hope this all makes some sense. I would appreciate any ideas on how to solve this. Probably something simple.

    Regards,
    Hein

    #13472
    Anh TranAnh Tran
    Keymaster

    Hi Hein,

    You have 2 cloneable fields, so when you use the shortcode to output, each of them will output in a unordered list.

    To fix this, please use this PHP code instead of shortcode:

    $texts = rwmb_meta( 'text_1' );
    $urls = rwmb_meta( 'url_1' );
    if ( ! empty( $texts ) ) {
        echo '<ul>';
        foreach ( $texts as $k => $text ) {
            echo "<li><a href='{$urls[$k]}'>{$text}</a></li>";
        }
        echo '</ul>';
    }
    #13480
    HeinHein
    Participant

    Thanks Anh,

    That solved it.

    You can close this topic now.

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