0

Is it possible to set an automatic value to a custom field while submitting the post in wordpress?

When I post I need that this custom field take automatically the first letter of the post title.

Example: If my title is "Example" The custom field value is "E".

3
  • How are you adding the custom field, are you using a plugin? Commented Jun 4, 2014 at 17:18
  • no i write the post normally, the custom field is a default one of my theme. Commented Jun 4, 2014 at 17:38
  • I'm not really sure what you mean by 'default one of my theme'? Commented Jun 4, 2014 at 17:39

1 Answer 1

0

Yes this is possible. If you're adding a custom field by a meta box you could do like this:

// add action to create your custom field
add_action( 'add_meta_boxes', 'create_meta_box'  );
// add a callback that will be called when saving the post
add_filter( 'save_post', 'save_meta_box', 10, 2 );

// function that will create a meta box
function create_meta_box() {
   add_meta_box('sample_meta_box', __('Sample Box'), 'display_meta_box');     
}

// function that will display the actual meta box
function display_meta_box($post, $metabox) {
  ?>
   <input name="sample" type="text" id="sample" value="<?php echo get_post_meta($post->ID, 'sample', true); ?>">
  <?php
}

// function that will be called when the post is saved
function save_meta_box($post_id, $post) {
   // default value for the field
   $value = $_POST['post_title'][0];
   // only use the default value if user haven't specified one
   if(!empty($_POST['sample'])) {
      $value = $_POST['sample'];
   }
   // update the meta data for the post
   update_post_meta($post_id, 'sample', $value);
}

Of course you will have to change sample in this case to the name of your meta data field.

If you already have created a custom field through the administration interface, you could do like this:

// add a callback that will be called when saving the post
add_filter( 'save_post', 'save_custom_field', 10, 2 ); 

// called when updating a post
function save_custom_field($post_id, $post) {
   // loop through the meta data
   if(isset($_POST['meta'])) {
     foreach($_POST['meta'] as $key => $value) {
        // check if it's the correct field
        if($value['key'] == 'sample') {
           // this will always set the custom field to the first letter in the title
           update_post_meta($post_id, 'sample', $_POST['post_title'][0]);
           break;
        }
     }
   }
}

Like in the first example you will have to change the sample to the name of your custom field.

5
  • but as "default value" i need the first letter of the post title. Commented Jun 4, 2014 at 17:45
  • I'm not showing a complete solution just pointing you in the right direction. I modify my code so the it will use the first letter of the title. Commented Jun 4, 2014 at 17:46
  • Really Thanks :) it's possible to do the same with custom taxonomy? Commented Jun 4, 2014 at 18:33
  • @user1825932 - If this answer helped you consider accept it and perhaps upvote=) And yes I think it would be possible but that is a different question. Commented Jun 4, 2014 at 18:34
  • I can't upvote, require 15 rep I have to open another question for custom taxonomy? Commented Jun 4, 2014 at 18:37

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.