2

I´m trying to find a snippet to add a predefined custom field. Does anyone know how or where i can find this?

I know I can do this with CustomPress, but I´m trying to do this without a plugin.

1

2 Answers 2

3

Take a look at adding a custom meta box for any type of predefined meta field.

add_meta_box - Docs - WordPress

You will need to add a hook to the 'save_post' action in order to save the meta data also.

Here is a quick and simple version:

    add_action( 'save_post', 'your_meta_box_save' );

    add_action( 'add_meta_boxes', 'your_meta_box_add' );

function your_meta_box_add(){
        add_meta_box( 'predefined_field', 'Your Predefined Info', 'your_meta_box_html', 'post', 'normal', 'high' );
}

function your_meta_box_save( $post_id ){

    // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    // if our nonce isn't there, or we can't verify it, bail
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'your_meta_box_nonce' ) ) return;

    // if our current user can't edit this post, bail
    if( !current_user_can( 'edit_post' ) ) return;

    // now we can actually save the data
    $allowed = array( 
        'a' => array( // on allow a tags
            'href' => array() // and those anchords can only have href attribute
        )
    );

    $your_predefined_value = isset($_POST['your_predefined_field']) ? $_POST['your_predefined_field'] : '';

    if( $your_predefined_value )
        update_post_meta($post_id,'your_predefined_field',$your_predefined_value);

}

function your_meta_box_html( $post ){

    wp_nonce_field( 'your_meta_box_nonce', 'meta_box_nonce' );

    //if you know it is not an array, use true as the third parameter
    $your_predefined_value = get_custom_meta($post->ID,'your_predefined_field',true);

    ?>

            <input name="your_predefined_field" id="your_predefined_field" type="text"  value="<?php echo $your_predefined_value; ?>" class="mws-textinput" />
    <?php
}

That should get you started; comment back if something is amiss.

0
function add_predefined_custom_field_names( $query ) {
    $predefined = array(
        'www.cyberxoft.com'
    );

    global $table_prefix;

    $query = preg_replace('/[\r\n\t]/', ' ', $query); //minify by removing all tabs and line breaks
    $query = preg_replace('/\s+/', ' ', $query); //minify by replacing spaces, tabs and carriages to single space

    //SELECT meta_key FROM wp_postmeta GROUP BY meta_key HAVING meta_key NOT LIKE '\\_%' ORDER BY meta_key LIMIT 30
    $pattern = ("/SELECT meta_key FROM ".$table_prefix."postmeta/i");   

    if( preg_match($pattern, $query) ) {
        $keys = '';     

        foreach($predefined as $key){$keys .= (" UNION SELECT '$key' AS meta_key");}        

        $query = preg_replace('/SELECT/i', 'SELECT meta_key FROM (SELECT', $query);
        $query = preg_replace('/FROM wp_postmeta/i', ('FROM wp_postmeta'.$keys), $query);
        $query = preg_replace('/ GROUP BY/i', ')t GROUP BY', $query);
    }

    return $query;
}
add_filter('query', 'add_predefined_custom_field_names');

Just add the above code anywhere in your themes function.php. After you've added the above code, it would add 'www.cyberxoft.com' to the drop down as one of the option to select.

If you get to see it, then just replace 'www.cyberxoft.com' with your required field name and refresh the admin page and when you see that happened just go ahead and add as many as you like BUT remember that only 30 could be viewed as thats the default limit set for it.

Enjoy...

You could also find this solution at Programatically add options to "add new" custom field dropdown

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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