Custom Fields
Re-Envisioned
Support Meta Box Builder Hyperlink url field outputs strange code

This topic contains 2 replies, has 2 voices, and was last updated by  Hein 8 months, 3 weeks ago.

  • Creator
    Topic
  • #13471
    Resolved
    Hein
    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

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #13472

    Anh 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

    Hein
    Participant

    Thanks Anh,

    That solved it.

    You can close this topic now.

Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.