Steve Taylor photo

SLT Custom Fields 0.4 (beta)

There’s a new version of my custom fields WordPress plugin. Development got slowed to a halt and I’ve not included everything I wanted to in this release, but I’ve put it out because it contains a fix for a bug someone kindly found.

There’s a bunch of new features, which need thorough testing to iron out any further bugs. Check out the changelog in the readme.txt for more details.

Thanks to everyone using the plugin and reporting bugs. I’m aiming to get the plugin into good shape for the WP repository soon. It’ll probably stay in beta for a bit after that, but at least we’ll be able to auto-update :)

29 comments

  1. Hi Steve,

    This is a fantastic library. I’ve been doing something similar using objects for metaboxes and fields, and I stumbled on your library while researching some approaches to refactoring my code.

    I may have found a bug. Using WP 3.1, I was unable to get the metabox to draw on a new post. Using WiPeD (I’d welcome any additional debugger recommendations!) I finally isolated the scope checking code as the problem. The default empty array scope doesn’t make it through the check.

    I haven’t patched it yet, but I’d be happy to contribute if you don’t have time to look at it.

  2. Hi Dan, thanks for your feedback. I’m not spending as much time developing this plugin as I might, though what’s there already is very useful I think. I’m using it in every client site I build, so I guess it’s being put through its paces.

    Anyway, I’m can’t recall my precise thinking on the default for the scope argument, but it definitely seems like more a feature than a bug. It seems better to me that, if you forget to specify the scope, the field doesn’t turn up anywhere than it turns up everywhere. I guess the default could be array( 'post' )? Makes sense for WP. Still, I’ll have to think about this. Meantime, just specify the scope—easy!

    What do you think the default should be?

  3. BTW, I’m way behind on debugging. I’ve just shifted from editing with TopStyle (an old favourite but very basic) to phpDesigner, which has debugging facilities. But I’ve not got round to setting it all up. I’ll definitely check out WiPeD.

  4. I think the default could be array( 'post' ) or it could just be a required argument. The latter makes more sense for use with custom post types, which I’d expect is the most common application.

    It took me longer to discover the feature than it should have, because I had inadvertently set the argument to array( 'posts' ) (with an ‘s’) for my initial test field. Not until I had stripped out all but the required arguments did I start getting closer to the problem.

    Another idea would be to add a sample working code block to the readme. As terrible as it is for folks to copy/pasting code from the internet directly into their functions.php, it would have saved me some time on my initial “does it work” test.

  5. Damian avatar Damian

    I would also love to see a few examples of how you are implementing this in the readme. I am just now realizing the power of add_meta_box() for managing the many custom fields on each site I build…however dealing with building forms over and over kind of a pain so I have been looking for a library something like what you have built.

    Have you seen what advanced custom fields can do also? Seems like a gravity forms style interface for building the custom meta box admin forms….unfortunately it has a few too many bugs right now to be used for my clients.

  6. Hi Damian, yeah, the documentation needs improving to get people going. I’m planning a new release soon, which should include a “gmap” field type – it’ll include a Google Map that you can position anywhere and optionally plonk a marker somewhere. Then on saving it’ll store the lat/long of the map centre and marker, and the zoom level, in hidden custom fields :)

    Anyway, I’ve seen a few of the other plugins offering similar functionality. Some have great things that I’ve not done. Most seem to be focused on the interface, but this is something I’m consciously avoiding. I’m not denying that some developers will prefer an easy interface, but I’m aiming mine at people who don’t mind using code because of the greater flexibility. Of course, without an interface, the first step in making it easier to use is good documentation, which… yes, I’ll get to it soon I hope! :)

  7. Damian avatar Damian

    Awesome to hear you are still working on it. I definitely will be watching….in the mean time I’ll just dig into it and see what I can make of it.

  8. Joseph avatar Joseph

    Quick question, I installed this plugin and set it up to where it is showing the new form fields in the default posts and default pages sections.

    I also created new post types via register_post_type

    When I change the parameter ‘type’ from ‘post’ to the name of my custom post type – the new fields vanish from my add a new post page (as they should), but never show up on the add a new *post type* page.

    I thought maybe I needed to define the new box under the “supports” item in the register post type array – but that didn’t do any good either, even when using the _slt_ prefix.

    What am I doing wrong? I’m sorry in advance for my silly question!

    Thank you again for writing this plugin, this is a great resource and a huge help!

  9. Hi Joseph, I suspect what’s going wrong is that you’re passing your custom post type to the box’s type parameter instead of to the field’s scope parameter. The type for a box defines whether the box should show up for posts, users, or maybe both. The scope for a field is where you say what the field applies to within that, e.g. post types, user roles, etc.

    The terminology is maybe a bit confusing, but I think the real problem is the documentation – I need to give more examples. Coming soon!

  10. Joseph avatar Joseph

    Thanks so much, Steve. That did the trick! I’ve tried the other custom posts / custom fields plugins and they either don’t have enough functionality / customization or in the case of the “More” line of plugins, don’t work properly!

    I’m going to stick with this plugin for my future developments, and would rather learn the code in more depth than use anything else. Thanks again!

  11. I’m not sure whether this is directly related to the plugin or not, but I have a couple places where I’ve defined an options_query field and set ‘numberposts’ => -1 . It’s worked fine in the past, but I just noticed today that I’m now only getting 10 options returned for the queries, regardless of what I put for ‘numberposts’ (tried 5, 20, removing it entirely).

    I’m guessing it’s something that’s changed after the most recent WP update? Queries elsewhere on my site haven’t changed.

    (By the way, I’ve been using your plugin since before it was a plugin and love it!)

  12. Hi Erin, wow, you’re old skool :) I think I’ve hit that problem before, and I think you just have to use posts_per_page instead of numberposts – a recent WP update I think. Let me know if that doesn’t work.

  13. Sean avatar Sean

    Hi Steve,

    Thanks very much for this – it seems awesome and I’m hoping it’s going to make life a lot more straight forward for clients!

    I’m confident enough setting up the meta boxes as I want them, but I’m getting stuck on something so elementary, and I can’t figure out why – I can’t get the contents of the fields to display on the site. I’m using the wrapper function in my custom template for retrieving the values – in place of a call to get_post_meta() etc. Am I missing something obvious?

  14. Sean, hard to tell what the problem might be with so little information, but something like this would output a custom field value on a page or post:

    echo slt_cf_field_value( 'field-name' );

    If you want to output the value for a post that you have the ID for but which isn’t the current post:

    echo slt_cf_field_value( 'field-name', 'post', $post_id );

    There are also parameters to control the output (check the readme.txt):

    slt_cf_field_value( 'field-name', 'post', $post_id, '<p>', '</p>', true );

  15. Sean avatar Sean

    Sorry for the sparse information – I’d misread the snippet in the readme – working fine now.

    I have one other question which might be a bit more complicated – I’m trying to allow someone I’m building a site for to select from a list of her Nextgen galleries using a dropdown. I have the code to echo an array of the gallery names and IDs, but I’m not sure of the easiest way to integrate this with a field? Any hints would be much appreciated!

    Thanks again for the great work.

  16. Sean, glad you got it going!

    Regarding the NextGen dropdown, again it’s all in the (admittedly unfriendly!) readme. For that field, set options_type to “static”, and pass the array of options to the options parameter. The array keys will be the option labels, the array values will be the option values – you might have to jig your array around, depending.

  17. Sean avatar Sean

    Thank you. I think I’m getting there – I have my array outputting like this:

    Array
    (
    [0] => 'homepage'=>'[ngggallery id=1]'
    [1] => 'Test Project'=>'[ngggallery id=2]'
    [2] => 'Barrabackslarrabang Video Stills'=>'[ngggallery id=3]'
    [3] => 'By Ours Installation Shots'=>'[ngggallery id=4]'
    [4] => 'Get Here Installation Shots'=>'[ngggallery id=5]'
    [5] => 'I Hate Installation Shots'=>'[ngggallery id=6]'
    [6] => 'I Hate Video Stills'=>'[ngggallery id=7]'
    [7] => 'I Hate Video Stills - Speech Therapy'=>'[ngggallery id=8]'
    [8] => 'Jokes - Installation Shots'=>'[ngggallery id=9]'
    [9] => 'Jokes - Video Stills'=>'[ngggallery id=10]'
    [10] => 'Left - Installation Shots'=>'[ngggallery id=11]'
    [11] => 'Left - Supporting Material'=>'[ngggallery id=12]'
    [12] => 'Scotland Road - Video Stills'=>'[ngggallery id=13]'
    [13] => 'The Work - Installation Images'=>'[ngggallery id=14]'
    [14] => 'Topography of a Voice'=>'[ngggallery id=15]'
    [15] => 'Untitled 3\"18'=>'[ngggallery id=16]'
    )

    which I’m getting using this:


    global $wpdb;
    $values = array();
    $attachments = $wpdb->get_results('SELECT gid,title,path FROM wp_ngg_gallery');
    foreach ($attachments as $attachment) {
    $str = "'" . $attachment->title . "'" . '=>' . "'" . '[ngggallery id=' . $attachment->gid . ']' . "'" ;
    $values[] = $str;
    }

    echo '';
    echo '$values: ' . print_r($values);
    echo '';

    I’m just not sure how to pass the array to the options parameter. Trying my best to hack away and learn as I go along… but not quite there.

  18. Looks like you’re getting the array mixed up a bit. Try something like this:


    global $wpdb;
    $options = array();
    $attachments = $wpdb->get_results('SELECT gid,title FROM wp_ngg_gallery');
    foreach ($attachments as $attachment) {
    $options[ $attachment->title ] = $attachment->gid;
    }

    I haven’t tested this, but you’re basically building an array with the keys set to the titles (for the text visible in the drop-down) and the values set to the gids (which will be stored in the custom field). Then when you declare the field inside slt_cf_register_box, set options_type to “static” and pass the above $options array to the options parameter.

  19. Sean avatar Sean

    Brilliant – it’s working perfectly.

    Thanks very much – heading over to the donate page to buy you a coffee…!

  20. Sean avatar Sean

    Hahaha – or I would if that’s where the donate page took me…

  21. Your choice whether to donate or not – but if it was a donate button to me personally and I just donated all the money I got Corporate Watch, you would donate? Just an admin shortcut for me really.

  22. Sean avatar Sean

    I donated – only meant you wouldn’t be getting a coffee.

  23. Sean avatar Sean

    Ah – encountered one more issue – since the options_type is static, is there any way to have an empty option? I have set the required parameter to false and added an empty_option_text string but those haven’t worked.

    Thanks.

  24. If the options are static, i.e. an array you’re passing in, you can obviously add the empty option in yourself, into the array you pass to options.

  25. Sean avatar Sean

    Thank you. I have an empty option now, but I can’t seem to get the code in my template to recognise it as empty, so it’s displaying the associated divs and a [gallery not found] message. My code is here:

    http://www.chopapp.com/#3wful80c

    Really appreciate how much you’ve helped already – if you had a second to see if there was anything glaringly bad that would be brilliant – though understand you must be busy!

  26. Check the database and see what the “gallery” field is storing. If that’s OK, you’ve got some issue with the output that’s beyond my scope. Maybe trying just storing the gallery ID and using that with whatever template tags the NG plugin provides on output?

Comments are closed.