PHP function parsing for writing custom storage fields in WordPress

Source: Internet
Author: User
Compile PHP function parsing for custom storage fields in WordPress ,. PHP function parsing for writing custom storage fields in WordPress. WordPress's custom fields are the meta information (meta information) of the article, PHP function parsing related to custom storage fields can be compiled in WordPress of the document extension,

The custom field of WordPress is the meta information (meta information) of the article. This function can be used to expand the function of the article. it is an essential knowledge for learning WordPress plug-in development and deep topic development, this makes it easy to store additional custom content for the article.

Custom fields are not only used by plug-in developers, but are stored in the form of custom fields for the special WordPress images, custom page templates, and other functions.

In the "custom columns" section of the article editor interface, you can manage the fields whose names start with "_". (if the "custom columns" section is not displayed, enable "Display options" in the upper right corner), therefore, many custom fields used by WordPress cannot be managed here.
Storage principle

Custom fields are very flexible. First, let's talk about how they are stored and why they are so flexible. In the database, the wp_posts form for storing articles has only 20 items by default, which is used to store necessary article information.

The wp_postmeta table that stores custom fields has only four items: meta_id (ID of the custom field) and post_id (ID of the document where the custom field belongs) meta_key (name of a custom field) and meta_value (value of a custom field). Generally, you only need to pay attention to meta_key and meta_value.

Because of this, each article can have custom fields with the same name but different values, and can have unlimited custom fields, any plug-in or topic can use custom fields to expand the document information.

The names of custom fields in an article can be repeated.

Add Field

Custom fields can be operated on the article editing page, but this article focuses on development.

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) (required) ID of the document that adds a custom field.

Default value: None

$ Meta_key

(String) (required) name of the custom field.

Default value: None

$ Meta_value

(Mixed) (mandatory) value of the custom field.

Default value: None

$ Unique

(Boolean) (optional) whether to add multiple fields with the same name. True: Allowed; False: 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) (required) update the document ID of the custom field.

Default value: None

$ Meta_key

(String) (required) name of the updated custom field.

Default value: None

$ Meta_value

(Mixed) (mandatory) update the value of the custom field.

Default value: None

$ Prev_value

(Mixed) (optional) this parameter is useful only when an article has multiple custom fields with the same name. If it is left blank, all fields with the same name will be updated; otherwise, fields with the same value as this parameter value will be updated.

Default value: Null string

Obtain fields

You can use the get_post_meta () function to obtain fields:

get_post_meta( $post_id, $key, $single );

Parameters:

$ Post_id

(Integer) (required) the article ID of the field to be obtained. if it is in a loop, you can use get_the_ID () to set it.

Default value: None

$ Key

(String) (optional) name of the field to be obtained.

Default value: None

$ Single

(Boolean) (optional) if True, a string is returned. if False, an array is returned, the values of custom fields with the same key value are combined into a sequence array in the order of addition.

Default value: False

Example

The following is a simple example of browser 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' );
Count:
function Bing_get_views(){  global $post;  $views = number_format( (int) get_post_meta( $post->ID, 'views', true ) );  return $views;}

Invisible custom fields
Although many custom fields created by WordPress exist in the database, however, it is not displayed in the "custom columns" in the article editor. (if the "custom columns" section is not displayed, enable "Display options" in the upper right corner ), for example, whether to enable special images, custom page templates, and comments.

If you observe carefully, you will find that the names of these custom fields all start with the underscore "_". Therefore, the custom fields starting with the underscore "_" are not displayed to the user, the_meta () functions are not output, which is hidden for users.

With this feature, you can set fields that you do not want to modify at will, and fields that may confuse you, such as setting options on the settings page and caching data, to avoid errors.

Below is a small example:

add_post_meta( get_the_ID(), '_time_diff', time() );

In addition, if the content stored in the custom field is an array, it is not displayed even if the front side of the name is not an underscore.

Articles you may be interested in:
  • Code used by WordPress to determine whether a user is logged on
  • Install the plug-in and hide the functions of the plug-in WordPress.
  • Add code instances in the prompt box to the WordPress editing background
  • Describes how to filter attributes and use SQL statements in WordPress development.
  • PHP functions used to create user roles in WordPress

WordPress's custom field is the meta information (meta information) of the article. This function can be used to 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.