PHP function parsing for writing custom storage fields in WordPress, _php tutorial

Source: Internet
Author: User

WordPress in the writing of custom storage fields related to PHP function parsing,


WordPress custom field is the meta-information of the article (meta-information), the use of this feature, you can expand the functionality of the article, is to learn the WordPress plugin development and the topic of deep development of the necessary knowledge, convenient for the article to store some additional custom content.

Custom fields are not only used by plug-in developers, but information about features such as WordPress's featured images, custom page templates, and more are stored in the custom field's form.

In the article editor interface, the "Custom Columns" section can be managed in addition to the first name is "_" field (there is no "custom column" in the upper right corner of the "Display Options" open), so many WordPress own custom fields used cannot be managed here.
Storage principle

The custom field is very flexible, first of all it's how it's stored and why it's so flexible. The Wp_posts form of the article is stored in the database, and only the default 20 items are used to store the required article information.

Instead of storing the custom field's Wp_postmeta table, there are only four items, namely meta_id (the ID of the custom field), post_id (the ID of the article the custom field belongs to), Meta_key (the name of the custom field), and Meta_ Value (custom field values), generally we only need to pay attention to Meta_key and meta_value two items can be.

Because of this, each article can have a custom field with the same name but different values, and there can be an unlimited number of custom fields, and any plug-ins and themes can use custom fields to extend the information of the article.

The name of a custom field in an article can be duplicated.

Add Field

Custom fields can be manipulated in the article editing page, but this article mainly writes about the development of things.

To add a custom field, you can use the Add_post_meta () function:

Add_post_meta ($post _id, $meta _key, $meta _value, $unique);

Parameters:

$post _id

(integer) (must) add the ID of the article for the custom field.

Default value: None

$meta _key

(string) (must) the name of the custom field.

Default value: None

$meta _value

(mixed) (must) the value of the custom field.

Default value: None

$unique

(Boolean) (optional) If a field with the same name already exists, add it repeatedly. True allows; False is not allowed.

Default value: False

Update fields

To update the value of a field, you can use the Update_post_meta () function:

Update_post_meta ($post _id, $meta _key, $meta _value, $prev _value);

Parameters:

$post _id

(integer) (must) update the ID of the article for the custom field.

Default value: None

$meta _key

(string) (must) update the name of the custom field.

Default value: None

$meta _value

(mixed) (must) update the value of the custom field.

Default value: None

$prev _value

(mixed) (optional) This parameter is only useful if you have more than one custom field with the same name in an article. If left blank, all fields with the same name are updated, otherwise the fields with the same value as this parameter value are updated.

Default value: Empty string

Get field

The Get field can use the Get_post_meta () function:

Get_post_meta ($post _id, $key, $single);

Parameters:

$post _id

(integer) (must) to get the article ID of the field, if it is in a loop, you can use GET_THE_ID () to set it.

Default value: None

$key

(string) (optional) The name of the field to get.

Default value: None

$single

(Boolean) (optional) returns a string if True, or returns an array if False, and the values of the custom fields of the same key value are combined into a sequence array in the order in which they are added.

Default value: False

Example

Below is a simple example of browsing statistics:

function Bing_statistics_visitors ($cache = False) {  if (!is_singular ()) return;  Global $post;  $id = $post->id;  if (Get_post ($id)->post_status! = ' publish ') return;  $post _views = (int) Get_post_meta ($id, ' views ', true);  Update_post_meta ($id, ' views ', ($post _views + 1)) | | Add_post_meta ($id, ' views ', 1, true);} Add_action (' Wp_head ', ' bing_statistics_visitors ');
Get Count:
function Bing_get_views () {  global $post;  $views = Number_format ((int) Get_post_meta ($post->id, ' views ', true));  return $views;}

Custom fields that are not visible
Although there are many custom fields that WordPress has created in the database, it is not shown in the "Custom columns" in the article editor (there is no "custom section" in the upper right corner of the "display Options"), such as featured images, custom page templates and comments open and so on.

If you look closely, you will find that the names of these custom fields begin with the underscore "_", so that the custom fields beginning with the underscore "_" are not displayed to the user, and the The_meta () function is not output and is hidden from the user.

Using this feature, we can put the fields that we don't want to be modified by the user, which will confuse the user, such as setting options for setting up the interface, caching data, and so on, so as not to make any mistakes.

Below is a small example:

Add_post_meta (get_the_id (), ' _time_diff ', Time ());

Also, if the custom field stores the contents of an array, it will not appear even if the first name is not underlined "_".

Articles you may be interested in:

    • WordPress to determine whether the user is logged in the code
    • Detailed description of WordPress in the reminder to install plug-ins and hidden plug-in function implementation
    • Add a code instance to the edit background of WordPress
    • A detailed description of the filter properties in WordPress development and the use of SQL statements function
    • A detailed description of the PHP functions used to create user roles in WordPress

http://www.bkjia.com/PHPjc/1084529.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084529.html techarticle WordPress Write custom storage field of the relevant PHP function parsing, WordPress custom field is the meta-information (meta-information), the use of this feature, you can expand the article ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.