Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author donmik

    (@atallos)

    If you need to convert field values, you can use the filter of my plugin. In the faq, you have an example:

    add_filter( 'bxcft_show_field_value', 'my_show_field', 15, 4);
    function my_show_field($value_to_return, $type, $id, $value) {
        if ($type == 'birthdate') {
            $value = str_replace("<p>", "", $value);
            $value = str_replace("</p>", "", $value);
            $field = new BP_XProfile_Field($id);
            // Get children.
            $childs = $field->get_children();
            $show_age = false;
            if (isset($childs) && $childs && count($childs) > 0) {
                // Get the name of custom post type.
                if ($childs[0]->name == 'show_age')
                    $show_age = true;
            }
            if ($show_age) {
                return '<p>'.floor((time() - strtotime($value))/31556926).'</p>';
            }
            return '<p>'.date_i18n( 'F j' , strtotime($value) ).'</p>';
        }
        return $value_to_return;
    }

    In your case, you need to check the name of your field maybe, and return what you need to embed media.

    Thread Starter crypt0rchid

    (@crypt0rchid)

    Thanks for the reply. I’m really only starting to scratch the surface with coding so I can just about understand what that is meant to do by looking at it but I have no idea how to change it to get it to do what I want to do.
    The field I want to allow embedding media is called ‘primary media’ what do I need to change in your example to get it to allow embedding?
    Thanks!

    Plugin Author donmik

    (@atallos)

    Hi,

    For youtube this works:

    add_filter( 'bxcft_show_field_value', 'my_show_field', 15, 4);
    function my_show_field($value_to_return, $type, $id, $value) {
        if ($type == 'web') {
            $value = str_replace("<p>", "", $value);
            $value = str_replace("</p>", "", $value);
            $field = new BP_XProfile_Field($id);
            if ($field->name == 'Link test youtube') {
                $link_src = strip_tags(trim($value));
                $link_src = str_replace('youtu.be/', 'youtube.com/embed/', $link_src);
                return '<iframe width="560" height="315" src="'.$link_src.'" frameborder="0" allowfullscreen></iframe>';
            }
        }
        return $value_to_return;
    }

    First, I check if the type of the field is the type I used. In my case I use website field.

    if ($type == 'web') {

    The next 3 lines are from my plugin:

    $value = str_replace("<p>", "", $value);
            $value = str_replace("</p>", "", $value);
            $field = new BP_XProfile_Field($id);

    Buddypress returns the value of your field surrounded by a paragraph <p> so I remove it to take the value of field only. The third line I create $field so I can now access the name of the field.

    I check now if it’s the field with the name I want. In my case I call my field “Link test youtube”.

    if ($field->name == 'Link prueba') {

    When I edit my profile I use the share link of youtube: https://youtu.be/axQhG6-qNhA, so I need to transform this into: <iframe width=”560″ height=”315″ src=”https://www.youtube.com/embed/axQhG6-qNhA&#8221; frameborder=”0″ allowfullscreen></iframe>

    First, I remove the tags my field has:

    $link_src = strip_tags(trim($value));

    The strip_tags remove the tag . This tag is added automatically by a filter in buddypress. The trim function remove any extra whitespace at the beginning or end of value. After applying this two functions in $link_src I have only the link https://youtu.be/axQhG6-qNhA. Now I need to transform this into the embed link.

    $link_src = str_replace('youtu.be/', 'youtube.com/embed/', $link_src);

    I replace youtu.be with youtube.com/embed/.

    return '<iframe width="560" height="315" src="'.$link_src.'" frameborder="0" allowfullscreen></iframe>';

    This line return the iframe.

    The last line is necessary to display the other fields you don’t want to modify. With soundcloud you need to do something similar.

    Great code donmik, it worked for me.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Embedding media’ is closed to new replies.