Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

How can I add pre-defined options to the "add new" custom field dropdown?

enter image description here

Here's two examples of automatically adding and showing new custom fields:

  1. WordPress: Adding Default Custom Fields on New Posts
  2. Auto create custom field

That's close to what I want to do; my goal is to add pre-defined options to the "add new" custom filed dropdown, but not have them show as fields until the blogger adds them.

I'd like to know how to do this without using a plugin.

share|improve this question
    
If you need specific fields filled out, why not use a custom metabox? There are a lot of options out there for doing this easily: Meta Box, WP Alchemy, Advanced Custom Fields, etc –  helgatheviking May 4 '13 at 0:57
    
@helgatheviking Thanks for the tips! I'll check out those options (Meta Box, WP Alchemy, and Advanced Custom Fields); those look really nice! Honestly, I'm mostly curious how to do this without using a plugin as I am trying to stay minimal and avoid using a plugin for every problem that I need to solve (I know how fast plugins can build up if one (i.e., me) is not careful). :D –  mhulse May 4 '13 at 1:06
    
Sure thing. You can make metaboxes without plugins (plenty of examples on here, the Codex or just by googling). but sometimes plugins can do exactly what you need or make your life a lot simpler. –  helgatheviking May 5 '13 at 14:01
    
@helgatheviking Thanks! Great advice! I may end up using a plugin down the road depending on how much more meta box tweaks I need to make for this project. Thanks again for the help/links/advice, I really appreciate it! –  mhulse May 6 '13 at 19:34
    
FYI, to anyone else in same boat as me: WP Alchemy looks nice purely from the standpoint that it's made for programmers; out of the three listed above, I think I like WP Alchemy the best because of that reason. The others look really cool too, but I prefer to stay as close to the code as possible. Something to note though, I'm not sure if WP Alchemy, or the others, can add default options to the stock custom field dropdown. If I end up using one, or all, I'll post back here with confirmation (or, if anyone else can confirm, that would be cool!) –  mhulse May 7 '13 at 17:26

2 Answers 2

up vote 3 down vote accepted

You cannot do that with pure PHP, because the fields are fetched from existing fields, and there is no hook. But you can use JavaScript, check if the post type supports custom fields and the field does not exist already – and insert it:

<?php # -*- coding: utf-8 -*-
/* Plugin Name: Extend custom fields */

add_action( 'admin_footer-post-new.php', 'wpse_98269_script' );
add_action( 'admin_footer-post.php', 'wpse_98269_script' );

function wpse_98269_script()
{
    if ( ! isset ( $GLOBALS['post'] ) )
        return;

    $post_type = get_post_type( $GLOBALS['post'] );

    if ( ! post_type_supports( $post_type, 'custom-fields' ) )
        return;
    ?>
<script>
    if ( jQuery( "[value='demo_data']" ).length < 1 ) // avoid duplication
        jQuery( "#metakeyselect").append( "<option value='demo_data'>demo_data</option>" );
</script>
    <?php
}
share|improve this answer
    
Whoa, awesome! Trying now, thank you! (+1) I'll be back to mark as answer once tested. Just out of curiosity, what's the meaning behind # -*- coding: utf-8 -*-? Does that comment style mean anything special to WordPress? Thank you!!!! :) –  mhulse May 4 '13 at 0:55
    
@MickyHulse # -*- coding: utf-8 -*- is an encoding cookie, it helps editors detecting the file encoding. Vim, Scite and some other editors will read it. Part of my standard PHP template. –  toscho May 4 '13 at 0:58
    
Works perfectly! I've updated my post with some tweaks that I made to your code (I had to create the <select> if it didn't already exist). Many thanks for the help, I really appreciate it!!!!! –  mhulse May 4 '13 at 1:44
    
... and thanks for the info on the encoding cookie, I had not seen that before. Much appreciated. –  mhulse May 4 '13 at 1:48

Below is a modified version of the awesome script posted by @toscho. I just needed the ability to create the <select> if it didn't already exist.

/**
 * 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>');

                    }

                    // Does "demo_data" already exist?
                    if (jQuery("[value='demo_data']").length < 1) {

                        // Add option:
                        $metakeyselect.append("<option value='demo_data'>demo_data</option>");

                    }

                </script>

            <?php

        }

    }

}

add_action('admin_footer-post-new.php', 'wpse_98269_script');
add_action('admin_footer-post.php', 'wpse_98269_script');

I'm sure my JS adaptations could be improved, but it gets the job done. I'll post updated code back here if I make changes/improvements.

Thanks again @toscho!!!! I owe you one. :)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.