Support » Plugin: Media Library Assistant » Adding MLA custom fields to theme php

  • Resolved Kassandra_P


    Hi David,

    Before I get to the technical nitty gritty, I have to say I am blown away by the thoroughness of this plugin, and by the clearly written documentation that goes along with it. Reading the documentation has taught me a lot, not only about the plugin, but about WordPress in general. And the plugin itself is a lifesaver for my current project!

    So, on to my question. I am pretty much at sea when it come to php, so this may be a silly one.

    I would like to add the content of the custom fields I have (successfully!) created using MLA to my theme’s image attachment page. It’s a pretty simple page, and I’ve worked out what all the elements do and where to put the code. But what should the code be?

    I suspect from reading the support that I’ll have to do something like: <?php do_shortcode(“[mla_gallery attachment_tag='{$tag}]'”); ?>, only is there something like custom_tag… or? Also some of the content of my fields will be urls. Do they need to be written a special way to work as links?

    In more detail, I’m hoping to get the output to be like this:
    <div class=”name”>[Custom Field ‘Name’ Content]</div>
    <div class=”city”>[Custom Field ‘City’ Content]</div>
    <div class=”social”>[Custom Field ‘Twitter’ “URL/link”].’ | ‘.[Custom Field ‘LinkedIn’ “URL/link”]</div>

    (That is the right way to add the text separator, right?)

    Thank you in advance!! I’m afraid I may have a couple more questions coming up since I also have to make my own markup for the gallery and I haven’t managed to get the pagination to work yet (if my theme’s posts have it, that means my theme supports pagination in general, right?) but one small step at a time — I’ll see if I can figure it out myself first! Thanks again!

Viewing 10 replies - 1 through 10 (of 10 total)
  • A quick follow up: I realized while posting a new question here that the php for my social links will have to be a bit more complicated than I first considered, because I do not want the separator | to be a link, but it needs to be “grouped” with the link to the right, so that if one field isn’t filled in, I won’t end up with extra separators hanging around: Twitter | |

    I’ve been doing some research and it looks like to get the URL imported from the custom field to display as a pretty link, I’ll need this:

    <?php echo “View“; ?>

    Only modified somehow so that the $url instead imports the mla field. I have no idea how to include the separator in this without making it part of the url though. I realize this is getting outside the scope of your plugin, but any help will be appreciated as I don’t even know what php terms to use to Google what I’m looking for! Thanks!

    Plugin Author David Lingren


    Thanks for the kind words about the plugin and the documentation (and for taking the time to read through it all). You’re pretty brave to jump into PHP and page templates and all that.

    There have been a few earlier support topics regarding PHP and image attachment pages; you may find them helpful in general:



    With regard to using custom fields in this context, there are two approaches you can take; 1) add an [mla_gallery] to the page and use the custom: prefix to access the fields, or 2) access the fields with WordPress API calls in the PHP code and use PHP to compose the page.

    The first approach is along the lines of your “do_shortcode … ” idea. You would compose a gallery with the single image and use the Gallery Display Content parameters or a custom markup template to display the fields you want. For example:

    <br />
    do_shortcode( sprintf( '[mla_gallery ids="%1$s" columns=1 size=full mla_caption="{+custom:Name+}, {+custom:City+}"]', get_the_ID() ) );<br />

    To make the caption into a link you’d code something like:

    do_shortcode( sprintf( '[mla_gallery ids="%1$s" columns=1 size=full mla_caption=\'<a href="{+custom:Twitter+}>My Twitter Link</a>\']', get_the_ID() ) );

    That assumes the content of the Twitter custom field is a valid URL. There’s no special coding for fields containing URLs; you add the HTML for the link yourself.

    If you want something more complex, like your “In more detail … “, example you can define a custom markup template like the “table-based template example” in the Settings/Media Library Assistant Documentation tab. The field delimiters are “[+ … +]” but the content is the same. The important points are that the MLA “custom:” prefix only works in the context of an [mla_gallery] shortcode and that the shortcode returns the HTML for a complete gallery. You’d put your <div> tags, etc. in the MLA markup template and run it through the shortcode. In the markup template you’d have something like:

    <br />
    <div class="name">[+custom:Name+]</div><br />
     <div class="city">[+custom:City+]</div><br />
     <div class="social">[+custom:Twitter+] | [+custom:LinkedIn+]</div><br />

    All of the template content is in a single string, so there are no ‘text separators’ within the template. The .'|'. notation you show is something that a PHP statement would require.

    If you want to put the <div> tags, etc., in your PHP for the page itself you would use the second approach to access the custom field contents. You would call a WordPress function to get the data you want:

    Function Reference/get metadata

    For example:

    <br />
    <?php<br />
    $data = get_metadata( 'post', get_the_ID() ); // GET ALL THE DATA<br />
    $name = $data['Name'];</p>
    <p>// OR GET ONE FIELD<br />
    $name = get_metadata( 'post', get_the_ID(), 'Name' );</p>
    <p>echo '<div class="name">' . $name . '</div>';<br />
    ?><br />

    For your follow up question on optional fields (which require PHP logic to suppress the separator for empty fields), you’d need something like:

    <br />
    <?php<br />
    $data = get_metadata( 'post', get_the_ID() ); // get all the data<br />
    $twitter = isset( $data['Twitter'] ) ? trim( $data['Twitter'] ) : '';<br />
    $linkedin = isset( $data['LinkedIn'] ) ? trim( $data['LinkedIn'] ) : '';</p>
    <p>if ( ! empty( $twitter ) ) {<br />
        $the_link = '<a href="' . $twitter . '">Twitter</a>';<br />
    <p>if ( ! empty( $linkedin ) ) {<br />
        if ( ! empty( $the_link ) )<br />
            $the_link .= ' | ';</p>
    <p>    $the_link .= '<a href="' . $linkedin . '">LinkedIn</a>';<br />
    <p>echo '<div class="social">' . $the_link . '</div>';<br />
    ?><br />

    That’s a lot of PHP to mull over. The isset() function handles the case where the custom field does not exist at all. The trim() function removes whitespace around the link. The empty() function tests for anything left over. The .= operator is a shorthand notation for concatenation.

    I am traveling and don’t have access to my development system, so I haven’t tested any of the above code. I apologize for any typos, omissions or other errors. Give it a try and please do let me know if you have any problems or more questions.

    Pagination, by the way, is a whole new ballgame. It’s very theme dependent and attachments are treated in a different way, not like posts/pages. When you get there, start another support topic and I’ll give you what help I can.

    Thanks again for your interest in the plugin and these questions, which should be useful to other customers as well. Have fun!

    Thank you for this detailed answer!

    I’m either brave or foolish to be jumping into php… unfortunately I don’t have much of a choice in this case because I need to achieve the functionality of adding custom fields to the attachments, and there doesn’t seem to be a way to do this without delving into it. I’m pretty good with html and css though, and usually I can get by by picking apart existing php elements… didn’t expect this one to be quite so complicated!!

    OK, so here’s where I’m at. Since I’ve added the social links to the caption field, I don’t need this bit of code anymore, though it was interested to ponder over:

    $data = get_metadata( 'post', get_the_ID() ); // get all the data
    $twitter = isset( $data['Twitter'] ) ? trim( $data['Twitter'] ) : '';
    $linkedin = isset( $data['LinkedIn'] ) ? trim( $data['LinkedIn'] ) : '';
    if ( ! empty( $twitter ) ) {
    $the_link = 'Twitter';
    if ( ! empty( $linkedin ) ) {
    if ( ! empty( $the_link ) )
    $the_link .= ' | ';
    $the_link .= 'LinkedIn';
    echo '<div class="social">' . $the_link . '</div>';

    However, for anyone else who stumbles across this thread, it returns the error “Warning: trim() expects parameter 1 to be string, array given in …”

    Regarding just the simple calling of the custom fields, I decided to go with the second approach, since the rest of my image attachment page including thumbnail etc. is already set up as I want it.

    I therefore tried this piece of code, since I want to pull each of the custom fields individually (it’s easier for my poor brain to grasp it this way!):

    <?php $name = get_metadata( 'post', get_the_ID(), 'Name' );
    echo '<div class="name">' . $name . '</div>'; ?>

    This returns a value of “Array”

    However if I change it to:

    <?php $data = get_metadata( 'post', get_the_ID(), 'Name' );
    echo '<div class="name">' . $name . '</div>'; ?>

    I successfully call the slug! So it seems the slug has the value of name already, and I can’t use it for a custom field. Fine, I’ll rename it. But I decided to try the code out with another of my custom fields:

    <?php $data = get_metadata( ‘post’, get_the_ID(), ‘City’ );
    echo ‘<div class=”city”>’ . $city . ‘</div>’;

    Unfortunately, this doesn’t call anything. BUT if I use the print_r () command on it (thanks to a friend who knows a bit more php than I do), it returns this:

    Array [0] => Seattle, WA

    There’s my city! So I think this must be close — any tweaks that might help get me the last bit there? The answer is possibly in one of the threads you referred me to, but I’m afraid I couldn’t make much sense of them…

    BTW I managed to figure out the pagination thanks to this thread, so this really is the last thing I have to solve, then I’m done!

    Plugin Author David Lingren


    As I said, I’m away from my system and can’t test my code suggestions. I believe that get_metadata returns an array, even if it only has one value. Thus, my example should have read trim( $data['Twitter'][0] ), I think.

    I don’t think your echo '<div class="city">' . $city . '</div>'; can work, and I think the success you had with $name is a fluke.

    I think you need something like:

    <?php $data = get_metadata( 'post', get_the_ID(), 'City' );
    echo '<div class="city">' . $data[0] . '</div>';

    Give that a try and let me know how it goes.

    Plugin Author David Lingren


    I’m glad you found the earlier topic on pagination helpful. Pagination was the inspiration behind the recent Support for Alternative Gallery Output enhancement.

    The native WordPress previous_image_link() and next_image_link() functions aren’t very helpful.

    If you run into pagination issues let me know and I can add details.

    Yes!!! That works perfectly!!! Thank you so so much, you went above and beyond. So sorry to interrupt your travels. You’re exceptionally generous with your time and help, and it’s greatly appreciated!!

    Forgot to mark as resolved 🙂

    Plugin Author David Lingren


    I have released MLA version 1.42, which includes pagination support for the [mla_gallery] shortcode. Complete information and examples are on the Settings/Media Library Assistant Documentation tab.

    I hope the new features will help with the pagination portion of your tasks. Let me know if you have any problems or further questions.

    Thank you I saw! The new feature is fantastic — and I’d just like to thank you again for making such a wonderful plugin! The more I use it, the more impressed I become with it!

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Adding MLA custom fields to theme php’ is closed to new replies.