


Analysis of related PHP functions for writing custom storage fields in WordPress, _PHP tutorial
Related PHP function analysis for writing custom storage fields in WordPress.
The custom fields of WordPress are the meta information (meta information) of the article. Using this function, the article can be expanded The function is essential knowledge for learning WordPress plug-in development and theme in-depth development, and it is convenient for storing some additional customized content for articles.
Custom fields are not only used by plug-in developers. Information about WordPress’s featured images, custom page templates and other functions are stored in the form of custom fields.
The "Custom Column" section in the article editor interface can manage fields except for fields whose names begin with "_" (if there is no "Custom Column" section, please turn it on in the "Display Options" in the upper right corner), so many WordPress itself The custom fields used cannot be managed here.
Storage Principle
Custom fields are very flexible. First, let’s talk about its storage principle and why it is so flexible. In the database, the wp_posts form that stores articles has only the default 20 items, which are used to store necessary article information.
The wp_postmeta table that stores custom fields has only four items, namely meta_id (the ID of the custom field), post_id (the ID of the article to which the custom field belongs), meta_key (the name of the custom field) and meta_value (the value of a custom field), generally we only need to pay attention to the two items meta_key and meta_value.
Because of this, each article can have custom fields with the same name but different values, and there can be unlimited custom fields. Any plug-ins and themes can use custom fields to expand article information.
The name of a custom field in an article can be repeated.
Add field
Custom fields can be operated on the article editing page, but this article mainly writes about development.
To add custom fields, you can use the add_post_meta() function:
add_post_meta( $post_id, $meta_key, $meta_value, $unique );
Parameters:
$post_id
(integer) (required) The ID of the post to which the custom field is added.
Default value: None
$meta_key
(String) (Required) The name of the custom field.
Default value: None
$meta_value
(Mixed) (Required) The value of the custom field.
Default value: None
$unique
(Boolean) (optional) If there is already a field with the same name, whether to add it repeatedly. True allows; False disallows.
Default value: False
Update field
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) The ID of the article to update the custom field.
Default value: None
$meta_key
(String) (Required) The name of the custom field to update.
Default value: None
$meta_value
(Mixed) (Required) The value of the custom field to update.
Default value: None
$prev_value
(Mixed) (Optional) This parameter is only useful if a post has multiple custom fields with the same name. If left blank, all fields with the same name are updated, otherwise fields with the same value as this parameter are updated.
Default value: empty string
Get field
To obtain fields, you can use the get_post_meta() function:
get_post_meta( $post_id, $key, $single );
Parameters:
$post_id
(integer) (required) The article ID of the field to be obtained, if in a loop, can be set using get_the_ID().
Default value: None
$key
(String) (optional) The name of the field to get.
Default value: None
$single
(Boolean) (optional) If True, returns a string; if False, returns an array, and the values of custom fields with the same key value will be combined into a sequence array in the order of addition.
Default value: False
Example
The following 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' );
function Bing_get_views(){ global $post; $views = number_format( (int) get_post_meta( $post->ID, 'views', true ) ); return $views; }
Invisible custom fields
Although there are many custom fields created by WordPress in the database, they are not displayed in the "Custom Column" of the article editor (there is no "Custom Column" section, please click the "Display Options" in the upper right corner enabled), such as whether featured images, custom page templates, and comments are enabled, etc.
If you observe carefully, you will find that the names of these custom fields all start with an underscore "_". Therefore, the custom fields starting with an underscore "_" will not be displayed to the user, and the_meta() function will not be displayed to the user. It is not output and is hidden from the user.
利用这个特性,我们可以把不想让用户随便修改的字段、会让用户感到困惑的字段,比如已经提供设置界面的设置选项、缓存数据等等,以免出现错误。
下边是一个小例子:
add_post_meta( get_the_ID(), '_time_diff', time() );
另外,如果自定义字段储存的内容是数组即使名字前边不是下划线 “_” 也不会显示。
您可能感兴趣的文章:
- WordPress判断用户是否登录的代码
- 详解WordPress中提醒安装插件以及隐藏插件的功能实现
- 给WordPress的编辑后台添加提示框的代码实例分享
- 详解WordPress开发中过滤属性以及Sql语句的函数使用
- WordPress中创建用户角色的相关PHP函数使用详解

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP and Flutter are popular technologies for mobile development. Flutter excels in cross-platform capabilities, performance and user interface, and is suitable for applications that require high performance, cross-platform and customized UI. PHP is suitable for server-side applications with lower performance and not cross-platform.

You can easily modify your WordPress page width by editing your style.css file: Edit your style.css file and add .site-content { max-width: [your preferred width]; }. Edit [your preferred width] to set the page width. Save changes and clear cache (optional).

Create a product page in WordPress: 1. Create the product (name, description, pictures); 2. Customize the page template (add title, description, pictures, buttons); 3. Enter product information (stock, size, weight); 4 . Create variations (different colors, sizes); 5. Set visibility (public or hidden); 6. Enable/disable comments; 7. Preview and publish the page.

WordPress posts are stored in the /wp-content/uploads folder. This folder uses subfolders to categorize different types of uploads, including articles organized by year, month, and article ID. Article files are stored in plain text format (.txt), and the filename usually includes its ID and title.

WordPress template files are located in the /wp-content/themes/[theme name]/ directory. They are used to determine the appearance and functionality of the website, including header (header.php), footer (footer.php), main template (index.php), single article (single.php), page (page.php), Archive (archive.php), category (category.php), tag (tag.php), search (search.php) and 404 error page (404.php). By editing and modifying these files, you can customize the appearance of your WordPress website

The most stable WordPress version is the latest version because it contains the latest security patches, performance enhancements, and introduces new features and improvements. In order to update to the latest version, log into your WordPress dashboard, go to the Updates page and click Update Now.

Search for authors in WordPress: 1. Once logged in to your admin panel, navigate to Posts or Pages, enter the author name using the search bar, and select Author in Filters. 2. Other tips: Use wildcards to broaden your search, use operators to combine criteria, or enter author IDs to search for articles.

WordPress is developed using PHP language as its core programming language for handling database interactions, form processing, dynamic content generation, and user requests. PHP was chosen for reasons including cross-platform compatibility, ease of learning, active community, and rich library and frameworks. Apart from PHP, WordPress also uses languages like HTML, CSS, JavaScript, SQL, etc. to enhance its functionality.
