Custom Fields

Custom Profile Fields

Custom fields used to extend standard data structures. Go to your profile page in admin and click "add" button in custom fields area. Enter key and value for a new field. You can use HTML but be careful when using it in UI.

You can enter multiple fields if needed, just make sure they have unique keys.

To use custom profile field anywhere in the blog, you fist need to create author profile object, passing it user ID. In the example below, we created custom field "foo" for user "Admin", so in the code we instantiate Admin's profile and then check "CustomFields" collection for a key "foo". If it exists, we assign local variable value from this field and use it to display on the screen.

// custom/themes/standard/CommentView.ascx
var user = new BlogEngine.Core.AuthorProfile("Admin"); var foo = ""; if (user.CustomFields != null && user.CustomFields["foo"] != null) { foo = user.CustomFields["foo"].Value; } %> <div>This is a custom profile field: <%= foo %></div>

Here we just output field value on the comments form. It is up to you how/where to use this field, for example it can also be used in extension or widget. All depends on your needs and requirements.

Custom Post Fields

Just as user profile, posts also can be extended with custom fields. When editing any post, use "custom fields" button to add required values. They all will be saved in the CustomFields collection for this post.

Again, fields collection can be used anywhere in the blog. Just for simple example, here is a code from post view that uses created "pass" field to output it on the screen. Post view has "Post" object so we don't need to declare it, otherwise we would first create a post or use it where code is processing existing post. For example it could be used in extension that handles render post event and makes changes to the post body if post has a custom fields with specific value.

// custom/themes/standard/PostView.ascx
  var pass = "";
  if (Post.CustomFields != null && Post.CustomFields["pass"] != null)
    pass = Post.CustomFields["pass"].Value;
<div>This is a custom post field: <%= pass %></div>

Comments are closed