• Resolved cizko

    (@cizko)


    Hi, I just updated a 3.5.2 system to 3.6 and I am now facing a problem regarding custom fields: the old system had 2 (TWO!) registered custom fields but after the update the custom fields section is now showing a vast dropdown list. fun fact: one of the two previously registered custom fields can be found in that list but the other one is missing.
    so i created a new post and simply re-registered the missing custom field (add new -> insert key -> insert value -> save). so far so good – the custom field’s value is correctly displayed in the frontend.

    *BUT*: The field’s key is then NOT added to the custom fields drop-down! πŸ˜›

    So I re-read http://codex.wordpress.org/Custom_Fields just to find this paragraph:

    “On your next post, you can add a new book and mood to your meta-data. In the Custom Fields section, the Key will now feature a pull down list with the previously entered Custom Fields. Choose “Currently Reading” and then enter the new book you are reading in the value. Click Add Custom Field and then repeat the process to add “Today’s Mood”. You only need to create a new “KEY” once, after which you can assign a value to that key for every post, if you so desire. You can also assign more than one Value to a key, for a post. This will come in handy for people who read more than one book at a time.”

    So what am I missing here? *confused*

    Thanks in advance! Cheers, ciz

Viewing 14 replies - 1 through 14 (of 14 total)
  • Having the same problem! Would someone be so kind as to suggest a fix for this? Our updating relies heavily on the custom keys and their being there in the dropdown.

    Thanks!

    Thread Starter cizko

    (@cizko)

    I found a solution to this – or more likely a workaround. Append this to your theme’s functions.php and adjust the section “Add your options” accordingly to fit your needs:

    /**
     * Programatically add custom fields.
     *
     * @see http://wordpress.stackexchange.com/questions/98269/programatically-add-options-to-add-new-custom-field-dropdown/
     */
    
    function wpse_98269_script() {
    
        if (isset($GLOBALS['post'])) {
    
            $post_type = get_post_type($GLOBALS['post']);
    
            if (post_type_supports($post_type, 'custom-fields')) {
    
                ?>
    
                    <script>
    
                        // Cache:
                        var $metakeyinput = jQuery('#metakeyinput'),
                            $metakeyselect = jQuery('#metakeyselect');
    
                        // Does the default input field exist and is it visible?
                        if ($metakeyinput.length && ( ! $metakeyinput.hasClass('hide-if-js'))) {
    
                            // Hide it:
                            $metakeyinput.addClass('hide-if-js'); // Using WP admin class.
    
                            // ... and create the select box:
                            $metakeyselect = jQuery('<select id="metakeyselect" name="metakeyselect">').appendTo('#newmetaleft');
    
                            // Add the default select value:
                            $metakeyselect.append('<option value="#NONE#">β€” Select β€”</option>');
    
                        }
    
                        // Add your options:
                        $metakeyselect.append("<option value='Your_option_1'>Your option 1</option>");
                        $metakeyselect.append("<option value='Your_option_2'>Your option 2</option>");
                        $metakeyselect.append("<option value='Your_option_3'>Your option 3</option>");
                        $metakeyselect.append("<option value='Your_option_4'>Your option 4</option>");
                        $metakeyselect.append("<option value='Your_option_5'>Your option 5</option>");
                        $metakeyselect.append("<option value='Your_option_6'>Your option 6</option>");
    
                    </script>
    
                <?php
    
            }
    
        }
    
    }
    
    add_action('admin_footer-post-new.php', 'wpse_98269_script');
    add_action('admin_footer-post.php', 'wpse_98269_script');

    Thank you! That does add the missing option. Unfortunately it adds it to the bottom of a very long list of other useless options. Any way of removing those?

    And can we expect WP to fix this – should a bug report be filed? Not sure how to alert them to this.

    Thanks again, cizko!

    Thread Starter cizko

    (@cizko)

    filtering the list or simply resetting it to no content at all can be easily done with jquery…

    Ha ha – not easy for me at all. Any hints at how to do so? πŸ™‚

    And if you reset it to no content does that undo the above action (code above)?

    Thanks!

    Thread Starter cizko

    (@cizko)

    BTW: If I got my reading right, this is *not a bug* but intended behaviour as of WP 3.6

    Thread Starter cizko

    (@cizko)

    Oh Daffydd57 – you are killing me here. Please tell me you are not making your money with wordpress…. πŸ˜›

    http://lmgtfy.com/?q=clearing+select+using+jquery

    Thread Starter cizko

    (@cizko)

    Just add the following right above the line “// Add your options:”

    jQuery('#metakeyselect').find('option').remove();
    $metakeyselect.append('<option value="#NONE#">β€” Select β€”</option>');

    But seriously – google is your friend here. Consider yourself lucky that I am in a great mood today, since you can not expect other people to do all the work for you…

    Got it – thanks!

    For the record – it turns out that the values that I was “missing” were not missing after all – just at the bottom of a very long list. Apparently WP will only show a certain number of options in that dropdown – if the list exceeds that number the remainder will not be shown.

    Plugins like NextGen add a LOT of entries to the list thus pushing the rest to the bottom.

    Thanks again.

    But seriously – google is your friend here. Consider yourself lucky that I am in a great mood today, since you can not expect other people to do all the work for you…

    Ha ha. Everybody has their strengths and weaknesses, cizko. I can google jquery stuff all day and not have a clue what to do with the results. That’s why people come to these help forums for advice. My profession is medicine, I’m a great deal more knowledgeable in that field – I’ll tell you what… should you ever come to one of the free clinics I give once a month instead of treating your illness I’ll tell you to google the diagnosis and treatment instead. πŸ˜‰

    I do sincerely thank you for your help today – no disrespect intended with the above. πŸ™‚

    Thread Starter cizko

    (@cizko)

    Hehehe… alright. I’m not about busting your a.. if you are not one of those – well – let’s just call them “semi”-pros which seem to float around here a lot, bugging real pros for solutions and then charging their customers for copy and pasting those.
    And I don’t think I will take you up on your offer – here in Germany our health care system is working pretty well. But thanks anyway. πŸ™‚

    Great workaround cizko!

    Daffydd57, about those values you where missing in the dropdown:
    Wordpress, by default, will only list up to 30 custom fields. Sometimes that is not enough. In those cases I recomend you to try this plugin http://wordpress.org/plugins/list-more-custom-field-names/ by Scott Reilly.

    This plugin increases the limit to 200 custom field key names.
    It’s light, easy to use and understand, and it gets the job done.

    Thanks, morollian – but the problem was actually having too many values – and the ones I needed were at the very bottom of the list. I ended up having to delete quite a few from the database – seems that NextGen Gallery adds a couple dozen to the list.

    Thanks!

    Sometimes when you need just specific fields to display in the backend it’s easier to use meta_boxes (codex reference). But I understand that was not an option for some reason…

    Anyway, didn’t the jquery solution by cizko worked for you? It can be a litte tricky to mess with the database, but if you find yourself with no other option, allways remember to backup first!

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘WP3.6 custom fields dropdown with strange behaviour after update’ is closed to new replies.