在上一篇文章中 ,我们讨论了使用提供的API在WordPress中处理帖子元数据。 我们还介绍了各种工具,安全性概念以及在整个教程中设置与代码一起使用的环境所需的工具。
如果您尚未阅读该文章,那么我强烈建议您对其进行审查,不仅因为它涵盖了如何处理帖子元数据,而且还因为它涉及了与本系列其余文章相关的一些重要主题(并且它暗示了一些将于今年晚些时候推出的产品)。
假设您都准备好了,并准备学习另一种元数据API,那么让我们开始使用WordPress User Meta API。
WordPress用户元API
回想一下本系列前面的内容,WordPress 通过以下方式定义元数据:
元数据通过键/值对进行处理。 关键是元数据元素的名称。 该值是将显示在与该信息相关联的每个单独帖子上的元数据列表中的信息。
当我们继续使用各种元数据API时,无论使用哪种API,您都将发现该定义成立。
令人高兴的是,一旦掌握了处理一个元数据API的知识,便对每个相关API的工作原理有了一个大致的了解。 当然,到处都有细微差别,但总体功能是相同的。
使用用户元数据API
在查看WordPress Post Meta API时,我们查看并使用了以下功能:
-
add_post_meta
-
update_post_meta
-
get_post_meta
-
delete_post_meta
是的,它们之间存在特质,尤其是与add_post_meta
和update_post_meta
工作方式以及get_post_meta
和delete_post_meta
的各种工作方式有关,而我们将要研究的API也会以相同的方式工作。
在本文的其余部分中,我假设您具有本地Web服务器,可以访问数据库前端,IDE,并且可以轻松使用tutsplus-metadata.php
文件。
如果您感到好奇,我将使用以下工具集:
注意,用户元数据将存储在wp_usermeta
数据库表中,因此我们将在数据库的任何屏幕快照中引用该数据。 与最初的发布元数据表不同,用户元数据表中实际上已经有一些数据。
这是因为存储在用户个人资料屏幕上的某些数据:
尽管如此,API将允许我们将自己的信息写入表中。 因此,让我们继续来看一下如何使用WordPress提供的功能。
请注意,在给出的所有示例中,由于第一个用户始终是站点管理员,因此我们将第一个参数的1
传递给API函数。 通常可以保证在任何给定的安装中都存在该文件。
添加用户元
您可以在Codex中找到对add_user_meta
函数的引用。 该函数的定义尽可能简洁:
将元数据添加到用户的记录。
这有什么好处? 就是说,如果您要使用基于WordPress的插件或Web应用程序,而您想扩展一个人能够与其个人资料相关联的内容,那么这是一种实现方式。
它可以像在给定的社交网络上提供用户个人资料一样简单,也可以是更高级的东西,您可以将用户与另一个表中包含的数据,信息数组或其他东西相关联。
无论如何,这就是您要做的事情。 事情是这样的:还记得使用add_post_meta
函数为帖子编写元数据如何导致使用相同的键可以写入多行吗?
使用add_user_meta
同样可能。 但是,API函数接受一个可选的第四个参数,即所插入的值是否应该唯一。
非唯一值
因此,首先,让我们看一下添加一些用户元数据的代码,并通过不指定它应该唯一的方式进行操作。
执行此操作的代码如下所示:
<?php
add_filter( 'the_content', 'tutsplus_add_user_meta' );
/**
* Determines if the current post is the default 'Hello World' post and, if so,
* adds non-unique user meta data to the database.
*
* @param string $content The post content.
* @return string $content The post content.
*/
function tutsplus_add_user_meta( $content ) {
if ( 1 === get_the_ID() ) {
add_user_meta( 1, 'twitter_account', 'https://twitter.com/tommcfarlin/' );
}
return $content;
}
请注意,我们正在使用与本系列前面的策略相同的策略:
- 我们
the_content
。 - 我们检查是否在 你好世界的帖子。
- 如果是这样,我们添加用户元数据。
- 我们将
$content
返回给WordPress。
安装此代码并在浏览器中加载Hello World帖子后,刷新页面几次。
完成后,结果数据库表将如下所示:
就像我说的那样,它与后元数据API的执行方式非常相似。
独特价值
使用您的数据库前端,删除已创建的行或随意选择一个新键(例如, instagram_username
东西)。 我将要删除行。
其次,我还将创建第二个函数,而不是更改上面的函数,以便在教程末尾提供完整的源代码,因此请仔细阅读以下代码:
<?php
add_filter( 'the_content', 'tutsplus_unique_add_user_meta' );
/**
* Determines if the current post is the default 'Hello World' post and, if so,
* adds unique user meta data to the database.
*
* @param string $content The post content.
* @return string $content The post content.
*/
function tutsplus_unique_add_user_meta( $content ) {
if ( 1 === get_the_ID() ) {
add_user_meta( 1, 'twitter_account', 'https://twitter.com/tommcfarlin/', true );
}
return $content;
}
首先,在函数调用中为元值(或第三个参数)提供唯一值。 刷新页面几次,然后查看数据库。 它看起来应该像这样:
注意有趣的是什么? 仍然有多个值,但是它们都是相同的。
现在尝试几次更改元值参数,然后看一下数据库,您应该看到类似以下的内容:
注意区别吗? 确实-没有一个。 那是因为我们说只能有一个唯一的密钥。 因此,这不一定意味着仅创建一条记录。 这意味着在调用该函数时将创建多个记录,但是它将始终使用与该键关联的存储的第一个值。
如果愿意,请继续并删除我们刚刚创建的行,因为这可以很好地了解下一个功能。
更新用户元
与Post Meta API的工作方式类似, 更新功能的工作方式如下:
根据用户ID更新用户元字段。 使用$ prev_value参数可区分具有相同键和用户ID的元字段。 如果用户的meta字段不存在,则将其添加。
使用此功能时,有助于在两种情况下进行考虑:
- 使用
add_user_meta
函数添加了先前的元数据并且有多个记录具有相同信息时 - 当未添加任何元数据并且我们要添加新记录并希望它唯一时
在第一种情况下,它有助于提供$prev_value
因为您是在告诉WordPress定位和更新哪个值。
当我们添加元数据时
例如,假设我们的数据库看起来像本教程前面的样子:
并且我们要更新以前值为https://twitter.com/tommcfarlin/
的记录。 为此,我们将更新如下所示的代码。
<?php
add_filter( 'the_content', 'tutsplus_update_user_meta' );
/**
* Determines if the current post is the default 'Hello World' post and, if so,
* updates user meta data with the specified previous value.
*
* @param string $content The post content.
* @return string $content The post content.
*/
function tutsplus_update_user_meta( $content ) {
if ( 1 === get_the_ID() ) {
update_user_meta( 1, 'twitter_account', 'https://twitter.com/tutspluscode/', 'https://twitter.com/tommcfarlin/' );
}
return $content;
}
然后,对数据库的更新将如下所示:
请注意,这将更新与此元键关联的所有值。 当然,这只是该功能的一种用途。
添加新的元数据时
在第二种情况下,您将不需要指定先前的值,因为您将是第一次添加信息。
为了明确update_user_meta
当您要将信息添加到数据库时,可以使用update_user_meta
函数。 使用它之前不必存在它。
每当您要添加一条唯一的,尚未添加到数据库的唯一记录时,此功能就很有用。 使用该功能很简单。 假设我们要保存用户的兄弟名称。
在这种情况下,我们可以这样做:
<?php
add_filter( 'the_content', 'tutsplus_unique_update_user_meta' );
/**
* Determines if the current post is the default 'Hello World' post and, if so,
* updates user meta data with the specified value.
*
* @param string $content The post content.
* @return string $content The post content.
*/
function tutsplus_unique_update_user_meta( $content ) {
if ( 1 === get_the_ID() ) {
update_user_meta( 1, 'siblings_name', 'Ben' );
}
return $content;
}
这导致将以下记录输入数据库:
如果您刷新页面几次,然后检查数据库表,您会注意到与使用add_user_meta
时出现的多个值相比,只写入了一个值实例。
然后,如果我们想更改该值,我们将更新与指定的元键关联的元值,它将更新该单个记录。
检索用户元
在检索用户元数据时,我们具有get_user_meta
函数 。 此时,应该清楚预期的参数将是用户ID和元密钥。
但是元值呢?
请记住,当我们检索信息时,我们只需要用户ID和元密钥,因为这是特定值的标识信息。
但是,如果开发人员对一个键有多个记录,会发生什么? 更具体地说,如果他们像我们上面那样使用add_user_meta
函数并且有多个记录怎么办?
这是可选的第四个参数起作用的地方:一个布尔值,我们指定是要检索单个值还是值数组。 默认值(如果未指定,则传递的值)为false
因此,除非另行指定,否则我们将始终返回一个数组。
检索所有记录
假设我们正在处理本教程前面的相同数据集。 也就是说,我们有一个用户的Twitter帐户的多个条目。 回想一下数据库看起来像这样:
为了从数据库中获取所有这些信息并显示在屏幕上,我们将使用以下代码:
<?php
add_filter( 'the_content', 'tutsplus_get_all_user_meta' );
/**
* Determines if the current post is the default 'Hello World' post and, if so,
* retrieves all user meta data for the admin user and the specified key.
*
* @param string $content The post content.
* @return string $content The post content.
*/
function tutsplus_get_all_user_meta( $content ) {
if ( 1 === get_the_ID() ) {
var_dump( get_user_meta( 1, 'twitter_account' ) );
}
return $content;
}
假设一切顺利,那么您应该在Hello World帖子的顶部看到以下内容:
{ [0]=> string(32) "https://twitter.com/tommcfarlin/" [1]=> string(32) "https://twitter.com/tommcfarlin/" [2]=> string(32) "https://twitter.com/tommcfarlin/" [3]=> string(32) "https://twitter.com/tommcfarlin/" }
如果不是,请仔细检查您对var_dump的调用,并确保该信息 已准备好在数据库中进行检索。
检索单个记录
如果要检索单个记录,则可以将true作为最终参数传递给函数。 这将检索以字符串格式创建的第一条记录。
<?php
add_filter( 'the_content', 'tutsplus_get_user_meta' );
/**
* Determines if the current post is the default 'Hello World' post and, if so,
* retrieves a single record of user meta data for the admin user and
* the specified key.
*
* @param string $content The post content.
* @return string $content The post content.
*/
function tutsplus_get_user_meta( $content ) {
if ( 1 === get_the_ID() ) {
echo esc_textarea( get_user_meta( 1, 'twitter_account', true ) );
}
return $content;
}
这段代码的结果将把它打印在我们一直在工作的Hello World帖子的顶部:
https://twitter.com/tommcfarlin/
请注意,如果您使用的是update_user_meta
且未将true
指定为最终参数,则将获得一个单索引数组。
array(1) { [0]=> string(32) "https://twitter.com/tommcfarlin/" }
因此,如果您要查找信息的字符串表示形式,请始终传递true
。
删除用户元
我们需要讨论的最后一件事是如何实际删除已写入数据库的数据。 如果到目前为止,您仍然遵循本系列文章,那么您可能会对这种特定功能的工作方式产生某种直觉。
从用户删除元数据匹配条件。 您可以根据键或键和值进行匹配。 根据键和值进行删除,将不会删除具有相同键的重复元数据。 如果需要,它还允许删除所有元数据匹配键。
请注意,此功能旨在在存在多个记录且您希望将其全部删除的情况下使用,或者当您存在单个记录并希望将其删除时使用。
删除多条记录
首先,我们将研究在有相同信息的多个记录时如何使用此功能。 就本示例而言,我们假设数据库看起来像这样:
在这里,我们有多个记录。 为了删除具有相同键的记录,我们使用一次对delete_user_meta
函数的调用,并传递用户ID和元键。
<?php
add_filter( 'the_content', 'tutsplus_delete_all_user_meta' );
/**
* Determines if the current post is the default 'Hello World' post and, if so,
* deletes all associated meta data with the specified key.
*
* @param string $content The post content.
* @return string $content The post content.
*/
function tutsplus_delete_all_user_meta( $content ) {
if ( 1 === get_the_ID() ) {
delete_user_meta( 1, 'twitter_account' );
}
return $content;
}
而且,如果刷新数据库表中的信息,您会注意到所有记录已被删除:
尽管这是一个易于使用的函数,但重要的是要记住,它可以在单个调用中删除多行,因此请谨慎使用。
单条记录
另一方面,如果您要删除单个记录,则需要三项信息:
- 用户的ID
- 中继键
- 元价值
具有所有三个值将允许您删除单个记录。 显然,它比以前使用此功能的精度更高。
因此,在我们的示例中,假设我们有两个记录,两个记录都具有twitter_account
元键。 每个键具有以下值:
在我们的示例中,我们只关心删除第二个值。 为此,我们将使用以下代码:
<?php
add_filter( 'the_content', 'tutsplus_delete_user_meta' );
/**
* Determines if the current post is the default 'Hello World' post and, if so,
* deletes a single record based on the specified meta key and meta value.
*
* @param string $content The post content.
* @return string $content The post content.
*/
function tutsplus_delete_user_meta( $content ) {
if ( 1 === get_the_ID() ) {
delete_user_meta( 1, 'twitter_account', 'https://twitter.com/pressware' );
}
return $content;
}
然后,如果刷新数据库,则应该看到以下内容(或类似内容):
如果API的性能完全符合您的期望,那就太好了。
完整的源代码
这是本文中介绍的所有源代码的副本。 请注意, add_action
调用已被注释掉,因为您需要根据对代码进行实验时要执行的操作来取消注释它们。
<?php
/**
* This file shows how to work with the common User Meta API functions.
*
* Namely, it demonstrates how to use:
* - add_user_meta
* - update_user_meta
* - get_user_meta
* - delete_user_meta
*
* Each function is hooked to 'the_content' so that line will need to be
* commented out depending on which action you really want to test.
*
* Also note, from the tutorial linked below, that this file is used form
* demonstration purposes only and should not be used in a production
* environment.
*
* Tutorial:
* http://code.tutsplus.com/tutorials/how-to-work-with-wordpress-user-metadata--cms-25800
*
* @version 1.0.0
* @author Tom McFarlin
* @package tutsplus_wp_metadata
*/
// add_filter( 'the_content', 'tutsplus_add_user_meta' );
/**
* Determines if the current post is the default 'Hello World' post and, if so,
* adds non-unique user meta data to the database.
*
* @param string $content The post content.
* @return string $content The post content.
*/
function tutsplus_add_user_meta( $content ) {
if ( 1 === get_the_ID() ) {
add_user_meta( 1, 'twitter_account', 'https://twitter.com/pressware' );
}
return $content;
}
// add_filter( 'the_content', 'tutsplus_unique_add_user_meta' );
/**
* Determines if the current post is the default 'Hello World' post and, if so,
* adds unique user meta data to the database.
*
* @param string $content The post content.
* @return string $content The post content.
*/
function tutsplus_unique_add_user_meta( $content ) {
if ( 1 === get_the_ID() ) {
add_user_meta( 1, 'twitter_account', 'https://twitter.com/photomatt/', true );
}
return $content;
}
// add_filter( 'the_content', 'tutsplus_update_user_meta' );
/**
* Determines if the current post is the default 'Hello World' post and, if so,
* updates user meta data with the specified previous value.
*
* @param string $content The post content.
* @return string $content The post content.
*/
function tutsplus_update_user_meta( $content ) {
if ( 1 === get_the_ID() ) {
update_user_meta( 1, 'twitter_account', 'https://twitter.com/tutspluscode/', 'https://twitter.com/tommcfarlin/' );
}
return $content;
}
// add_filter( 'the_content', 'tutsplus_unique_update_user_meta' );
/**
* Determines if the current post is the default 'Hello World' post and, if so,
* updates user meta data with the specified value.
*
* @param string $content The post content.
* @return string $content The post content.
*/
function tutsplus_unique_update_user_meta( $content ) {
if ( 1 === get_the_ID() ) {
update_user_meta( 1, 'siblings_name', 'Ben' );
}
return $content;
}
// add_filter( 'the_content', 'tutsplus_get_all_user_meta' );
/**
* Determines if the current post is the default 'Hello World' post and, if so,
* retrieves all user meta data for the admin user and the specified key.
*
* @param string $content The post content.
* @return string $content The post content.
*/
function tutsplus_get_all_user_meta( $content ) {
if ( 1 === get_the_ID() ) {
var_dump( get_user_meta( 1, 'twitter_account' ) );
}
return $content;
}
// add_filter( 'the_content', 'tutsplus_get_user_meta' );
/**
* Determines if the current post is the default 'Hello World' post and, if so,
* retrieves a single record of user meta data for the admin user and
* the specified key.
*
* @param string $content The post content.
* @return string $content The post content.
*/
function tutsplus_get_user_meta( $content ) {
if ( 1 === get_the_ID() ) {
var_dump( get_user_meta( 1, 'twitter_account' ) );
}
return $content;
}
// add_filter( 'the_content', 'tutsplus_delete_all_user_meta' );
/**
* Determines if the current post is the default 'Hello World' post and, if so,
* deletes all associated meta data with the specified key.
*
* @param string $content The post content.
* @return string $content The post content.
*/
function tutsplus_delete_all_user_meta( $content ) {
if ( 1 === get_the_ID() ) {
delete_user_meta( 1, 'twitter_account' );
}
return $content;
}
add_filter( 'the_content', 'tutsplus_delete_user_meta' );
/**
* Determines if the current post is the default 'Hello World' post and, if so,
* deletes a single record based on the specified meta key and meta value.
*
* @param string $content The post content.
* @return string $content The post content.
*/
function tutsplus_delete_user_meta( $content ) {
if ( 1 === get_the_ID() ) {
delete_user_meta( 1, 'twitter_account', 'https://twitter.com/pressware' );
}
return $content;
}
此外,请随意将其添加到我们在上一教程中创建的文件中。 这是我在处理示例时所做的; 但是,在处理文件时,您可能需要小心,以便根据您要执行的操作设置正确的add_action
调用。
结论
如本文前面所述,您可以查看WordPress Codex中的每个功能,对于WordPress开发人员而言,应始终可以单击它们。
在本系列的最后一篇文章中,我们将研究如何处理评论元数据。 鉴于我们到目前为止所学的知识,应该相对容易掌握。
当然,这仍然给我们提供了与分类法相关的元数据。 由于分类法,术语和API的性质,我们将在后续系列中进行复习。
现在,继续尝试本文提供的代码。 请记住,它仅用于演示目的,不应在生产环境中运行。
在整个系列中,我们正在努力为将来的WordPress开发人员打下基础,从他们继续为自己的雇主,客户或自己的项目开发解决方案时开始。
话虽如此,我期待继续本系列。 记住,如果您只是入门,那么可以阅读我的有关如何开始使用WordPress的系列文章,该系列集中于专门针对WordPress初学者的主题。
同时,如果您正在寻找其他实用程序来帮助您构建不断增长的WordPress工具集或用于研究代码并精通WordPress的代码,请别忘了看看Envato中提供的功能市场 。
最后,您可以在个人资料页面上看到我所有的课程和教程,并且可以在我的博客上阅读有关WordPress和WordPress开发的更多文章。 随时在Twitter和@tommcfarlin上关注我,在这里我讨论了各种软件开发实践以及如何在WordPress中使用它们。
请不要在下面的提要中留下任何问题或评论,我将尽力回答每个问题或评论。
翻译自: https://code.tutsplus.com/tutorials/how-to-work-with-wordpress-user-metadata--cms-25800