如何使用WordPress评论元数据

在整个系列中,我们研究了WordPress提供的许多元数据API。 这包括Post Meta API和User Meta API。 今天,我们将通过查看WordPress Comment Meta API来完善该系列。

请注意,这是WordPress提供的最终元数据API。 从WordPress 4.4开始,现在有了术语元数据API。 为了完全理解它,了解WordPress上下文中的分类法,术语及其关系非常重要。 在即将到来的系列文章中,我将确切介绍这一点。

但是今天,我们将专注于管理与注释关联的元数据。 如果到目前为止您还没有阅读本系列的其他文章,那么建议您回顾一下到目前为止我们已经介绍的内容

如果您全都被赶上了,那就开始吧。

WordPress评论元API

在整个系列中,我们一直使用WordPress的元数据定义作为基础,以了解如何在我们的环境中表示此信息。

具体来说, 我们说过

元数据通过键/值对进行处理。 关键是元数据元素的名称。 该值是将显示在与该信息相关联的每个单独帖子上的元数据列表中的信息。

为了与我们在本系列文章中看到的其余内容保持一致,此定义在评论元数据中同样适用,就像在帖子元数据和用户元数据中一样。

这样做的好处是,一旦您了解了我们涵盖的各种元数据API,其他元数据功能的工作原理就不会感到惊讶。 取决于您使用的API,可能有些不同,但是功能的要点是相同的。

而且,您将看到,注释元数据API仍然是这种情况。

使用评论元数据API

与我们在本系列中已经研究过的其他API一样,我们将探索四个主要功能:

  • add_comment_meta
  • update_comment_meta
  • get_comment_meta
  • delete_comment_meta

到目前为止,您可能已经知道添加注释元数据和更新注释元数据之间的区别了。 或者说,删除评论元数据。

这并没有改变这样一个事实:值得详细研究这些API函数中的每一个,以确保我们涵盖了与它们一起使用时的所有已知知识。

对于本文的其余部分,我假设以下内容:

  • 您正在使用Web服务器,数据库服务器和PHP运行本地开发环境。
  • 您已经安装了WordPress的副本。
  • 您在主题目录中设置了tutsplus-metadata.php副本,该副本包含在主题的functions.php文件中。
  • 您可以随时选择IDE和数据库前端。

正如我在前面的文章中所使用的,我正在使用以下应用程序:

最后,我们将使用的所有元数据都存储在wp_commentmeta数据库表中,因此,您从数据库中看到的所有屏幕截图都将属于该特定表。

空的评论元数据表

与我们看到的其他一些元数据表不同, wp_commentmeta表开始时是空的(假设您使用的是相对较新的WordPress版本)。

这样做很好,因为它将为我们提供各种检查各种API函数时使用的方法。 请注意,对于以下所有示例,我们将确保所有这些操作均在Hello World上发生 发布。 该帖子的ID为1 。 如果要使用其他页面,只需将1替换为相关帖子的ID。

完成所有这些操作后,让我们开始看看可用的功能。

添加评论元

为了开始将元数据添加到我们的注释中,重要的是查看wp_comments表以查看已经存在的注释。 如果您正在使用全新安装的WordPress,则可能会看到一条记录:

单个默认WordPress注释

这是WordPress随附的默认注释,对于我们将要使用的示例而言,它会很好地完成工作。

另一方面,如果您正在使用充满注释的数据库,那就没问题! 您需要知道的是您正在使用的注释(通过其ID),并确保与下面使用的代码一致。

与本系列中其他API一样,向注释添加元数据有两种形式:唯一和非唯一。 我们将同时审查两者。

添加唯一元数据

add_comment_meta函数接受三个参数和一个可选的第四个参数。 如果将true作为第四个参数传递,则仅在指定的元键不存在时才添加元数据。

合理? 让我们来看看。 首先,我们将设置一个函数,该函数将添加一些与第一个评论关联的元数据(但只会在ID为1的帖子上这样做):

<?php

add_filter( 'the_content', 'tutsplus_add_unique_comment_meta' );
function tutsplus_add_unique_comment_meta( $content ) {

    if ( 1 === get_the_ID() ) {
		add_comment_meta( 1, 'twitter_handle', 'tommcfarlin', true );
	}

	return $content;

}

请注意,我已经传递了true参数。 因此,每次刷新页面时,WordPress都会看到我希望该值唯一,因此不会向与该元键关联的数据库表添加任何更多信息。

添加非唯一元数据

另一方面,如果我想将多个值与同一个元键相关联,则将删除true参数。 例如,使用以下代码:

<?php

add_filter( 'the_content', 'tutsplus_add_comment_meta' );
function tutsplus_add_comment_meta( $content ) {

    if ( 1 === get_the_ID() ) {
		add_comment_meta( 1, 'random_value', rand() );
	}

	return $content;

}

然后刷新页面,例如,三次。 如果一切按预期进行,您将看到三个新记录,每个记录都包含一个由meta值参数中对rand()的调用生成的随机数。

具有相同中继键的多个值

保持直线很容易,对吧? 如果您想要与单个键关联的单个值,则将true传递为可选的第四个参数; 否则,请勿指定任何内容。

更新评论元

如果要更新现有的注释元数据,那么了解注释ID,元键和元值很重要。 这意味着WordPress将查看指定的元密钥并将其与指定的元值关联。

<?php

add_filter( 'the_content', 'tutsplus_update_comment_meta' );
function tutsplus_update_comment_meta( $content ) {

    if ( 1 === get_the_ID() ) {
		update_comment_meta( 1, 'unique_value', time() );
	}

	return $content;

}

如果该元值存在,则它将被覆盖。 如果元值不存在,则将创建它。 请注意,在最后一句话中,添加元数据时可以使用一些功能:如果要将唯一的信息写入元数据表,则可以使用update_comment_meta并将其作为唯一值写入。

这可能会导致代码混乱(因为它读起来好像是在更新不存在的内容一样),但是它还允许您强制要求对于给定的元键仅存在一条记录。

当有一个与几个元值相关联的元键时,会发生什么? 在这种情况下,您需要知道要替换的值。 在某些情况下,您可能确切知道这是什么。 在其他情况下,您可能需要检索信息才能找到它(我们将在下一部分中介绍)。

假设我们要更新已创建的记录之一,例如在上面的示例中看到的随机数之一,我们可以使用数据库前端进行查找:

与多个键相关的更新后的随机值

并在以下代码中将其指定为先前的值:

<?php

add_filter( 'the_content', 'tutsplus_update_specific_meta' );
function tutsplus_update_specific_meta( $content ) {

    if ( 1 === get_the_ID() ) {
		update_comment_meta( 1, 'unique_value', time(), '17123683' );
	}

	return $content;

}

之后,我们可以刷新页面,查看数据库,然后查看更改。 请注意,我们刚才所做的工作与开发环境有关,而不是您在生产中进行处理的方式。

相反,您可能需要运行查询或检索一组值,然后再更新它们。 这使我们进入下一个主题。

检索评论元

每当检索注释元数据时,都需要确定是要检索单个值还是要检索与指定元键关联的所有值。

也许另一种看待方式如下:如果使用相同的元键添加了多个元数据(我们在上面的“ 添加唯一元数据”部分中介绍了),那么您可能希望检索整个记录集合。

另一方面,如果您只因为知道一条记录的唯一性或因为它是用update_comment_meta函数创建的而只想检索一条记录,则希望WordPress以单个值将其返回给您。

根据要检索的内容, get_comment_meta函数需要三个参数和一个可选的第四个参数。

检索数组

假设您要检索与单个元键关联的所有值的集合。 为此,您将调用get_comment_meta并指定注释ID和meta键。

<?php

add_filter( 'the_content', 'tutsplus_get_comment_meta' );
function tutsplus_get_comment_meta( $content ) {

    if ( 1 === get_the_ID() ) {

		$arr_metadata = get_comment_meta( 1, 'unique_value', true );
		var_dump( $arr_metadata );

	}

	return $content;

}

在上面的代码中,我们将其打印到Web浏览器中,但是一旦检索到数据,便可以使用任何方式使用该数据。 但是,要注意的最重要的是,该值以数组形式返回。

检索单个值

如果要检索单个值,则只需在get_comment_meta函数中指定注释ID和meta键。 如果您碰巧要处理具有多个值的元键,那么创建的第一个值就是要返回的值。

例如,假设有三个记录与一个元键关联,而您只想检索一个值。 您的代码将如下所示:

<?php

add_filter( 'the_content', 'tutsplus_get_one_comment_meta' );
function tutsplus_get_one_comment_meta( $content ) {

    if ( 1 === get_the_ID() ) {

		$arr_metadata = get_comment_meta( 1, 'random_value', true );
		var_dump( $arr_metadata );

	}

	return $content;

}

结果值将如下所示:

string(9) "967432645"

或者,如果您知道数据是唯一的,则代码将看起来与返回值相同。

唯一的区别是,第一个示例是从一组多个记录中检索的,而第二个示例是从单个记录中检索的。 另请注意,如果您要处理的是唯一值,则该值仍将作为数组(但是单个索引数组)返回给您。

请务必注意这一点,因为如果您打算进行任何类型的比较,尤其是对不同类型的值(例如整数)进行比较,那么您将需要从数组中获取该值,并且您可能希望进行类型比较进行比较之前进行转换。

删除评论元

删除元数据是一项简单的操作:它需要注释ID,元密钥和可选的元值。

如果您未指定元值,则与该元键关联的所有记录都将被删除。 但是,如果您指定元值,则仅删除该单个记录。

删除唯一值

就本示例而言,假定我们知道给定的元密钥存在单个元数据。 这意味着该元密钥对于每个用户必须是唯一的,因此它可能使用诸如唯一生成的ID,时间戳之类的东西。

要删除唯一值,我们只需传递注释ID和meta键:

<?php

add_filter( 'the_content', 'tutsplus_remove_unique_comment_meta' );
function tutsplus_remove_unique_comment_meta( $content ) {

    	if ( 1 === get_the_ID() ) {
			delete_comment_meta( 1, 'unique_value' );
		}

		return $content;

}

在运行此代码之前,数据库应如下所示:

注释元数据表中的元数据

刷新页面后,查看数据库,您应该看到记录已被删除,数据库应如下所示:

唯一值已被删除

在下一节中,我们将详细讨论删除数据时应采取的预防措施。

删除非唯一值

在其他情况下,假设有多个与单个元键关联的值。 在本文中,我们已经无数次地看到了这一点。 如果要删除与元键关联的所有记录,则无需指定元值。

也就是说,如果仅将注释ID和meta键传递给delete_comment_meta函数,它将删除所有注释元数据。 编写并执行以下代码:

<?php

add_filter( 'the_content', 'tutsplus_remove_comment_metadata' );
function tutsplus_remove_comment_metadata( $content ) {

    if ( 1 === get_the_ID() ) {
		delete_comment_meta( 1, 'random_value' );
	}

	return $content;

}

刷新页面,然后查看数据库。 如果没有问题,则您的数据库应清除先前具有该元键的所有记录:

注释元数据表中没有随机值

但是请记住,从数据库中删除数据可能很危险,特别是如果您意外删除了原本不想删除的内容。 为此,将数据库备份保留在生产环境中很重要,这样在出现问题时始终可以还原它。

此外,这证明了为什么在将代码部署到生产环境之前拥有本地开发环境和过渡环境进行测试如此重要的原因。

完整的源代码

正如整个系列所提供的,这是我们在本文中介绍的所有源代码的副本。 它已完全记录在案,并包含上面某些部分中未显示的注释。

<?php
/**
 * This file shows how to work with the common Comment Meta API functions.
 *
 * Namely, it demonstrates how to use:
 * - add_comment_meta
 * - update_comment_meta
 * - get_comment_meta
 * - delete_comment_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:
 * https://code.tutsplus.com/tutorials/how-to-work-with-wordpress-post-metadata--cms-25715
 *
 * @version    	1.0.0
 * @author		Tom McFarlin
 * @package		tutsplus_wp_metadata
 */

/* add_filter( 'the_content', 'tutsplus_add_unique_comment_meta' ); */
/**
 * Adds a unique meta key and meta value for a user's Twitter handle
 * to the comment metadata table associated with the comment having
 * the ID of 1.
 *
 * @param  string $content The content of the post.
 * @return string $content THe content of the post.
 */
function tutsplus_add_unique_comment_meta( $content ) {

	if ( 1 === get_the_ID() ) {
		add_comment_meta( 1, 'twitter_handle', 'tommcfarlin', true );
	}

	return $content;

}

/* add_filter( 'the_content', 'tutsplus_add_comment_meta' ); */
/**
 * Adds a unique meta key and random meta value
 * to the comment metadata table associated with the comment having
 * the ID of 1.
 *
 * @param  string $content The content of the post.
 * @return string $content THe content of the post.
 */
function tutsplus_add_comment_meta( $content ) {

	if ( 1 === get_the_ID() ) {
		add_comment_meta( 1, 'random_value', rand() );
	}

	return $content;

}

/* add_filter( 'the_content', 'tutsplus_update_comment_meta' ); */
/**
 * Updates unique meta key and unique meta value for
 * to the comment metadata table associated with the comment having
 * the ID of 1.
 *
 * @param  string $content The content of the post.
 * @return string $content THe content of the post.
 */
function tutsplus_update_comment_meta( $content ) {

	if ( 1 === get_the_ID() ) {
		update_comment_meta( 1, 'unique_value', time() );
	}

	return $content;

}

/* add_filter( 'the_content', 'tutsplus_update_specific_meta' ); */
/**
 * Updates a unique meta key and random meta value with a specified value
 * to the comment metadata table associated with the comment having
 * the ID of 1.
 *
 * @param  string $content The content of the post.
 * @return string $content THe content of the post.
 */
function tutsplus_update_specific_meta( $content ) {

	if ( 1 === get_the_ID() ) {
		update_comment_meta( 1, 'random_value', time(), '17123683' );
	}

	return $content;

}

/* add_filter( 'the_content', 'tutsplus_get_comment_meta' ); */
/**
 * Gets an array of the comment metadata associated with the meta key
 * in the comment metadata table associated with the comment having
 * the ID of 1.
 *
 * @param  string $content The content of the post.
 * @return string $content THe content of the post.
 */
function tutsplus_get_comment_meta( $content ) {

	if ( 1 === get_the_ID() ) {

		$arr_metadata = get_comment_meta( 1, 'unique_value' );
		var_dump( $arr_metadata );

	}

	return $content;

}

/* add_filter( 'the_content', 'tutsplus_get_all_comment_meta' ); */
/**
 * Gets an array of all the meta values associated with the specified meta key
 * in the comment metadata table associated with the comment having
 * the ID of 1.
 *
 * @param  string $content The content of the post.
 * @return string $content THe content of the post.
 */
function tutsplus_get_all_comment_meta( $content ) {

	if ( 1 === get_the_ID() ) {

		$arr_metadata = get_comment_meta( 1, 'random_value' );
		var_dump( $arr_metadata );

	}

	return $content;

}

/* add_filter( 'the_content', 'tutsplus_get_one_comment_meta' ); */
/**
 * Gets a single value from a set of values associated with a meta key
 * in the comment metadata table associated with the comment having
 * the ID of 1.
 *
 * @param  string $content The content of the post.
 * @return string $content THe content of the post.
 */
function tutsplus_get_one_comment_meta( $content ) {

	if ( 1 === get_the_ID() ) {

		$arr_metadata = get_comment_meta( 1, 'random_value', true );
		var_dump( $arr_metadata );

	}

	return $content;

}

/* add_filter( 'the_content', 'tutsplus_remove_unique_comment_meta' ); */
/**
 * Removes a unique meta value associated with the specified key
 * to the comment metadata table associated with the comment having
 * the ID of 1.
 *
 * @param  string $content The content of the post.
 * @return string $content THe content of the post.
 */
function tutsplus_remove_unique_comment_meta( $content ) {

	if ( 1 === get_the_ID() ) {
		delete_comment_meta( 1, 'unique_value' );
	}

	return $content;

}

/* add_filter( 'the_content', 'tutsplus_remove_comment_metadata' ); */
/**
 * Removes all meta values associated with the specified key
 * to the comment metadata table associated with the comment having
 * the ID of 1.
 *
 * @param  string $content The content of the post.
 * @return string $content THe content of the post.
 */
function tutsplus_remove_comment_metadata( $content ) {

	if ( 1 === get_the_ID() ) {
		delete_comment_meta( 1, 'random_value' );
	}

	return $content;

}

请注意,如果将此内容包含在本系列其余文章的代码中,则需要格外小心。 由于所有内容都使用相同的钩子注册,因此您可能会得到奇怪的结果。

为了解决这个问题,我建议以下两种情况之一:

  1. 创建三个单独的文件:为我们检查过的每种元数据API类型创建一个文件。
  2. 注释add_filter调用,但当前正在使用的除外。

这些方法中的任何一个都将使我们到目前为止所涉及的所有代码的使用变得更加容易。

结论

无论您使用的是元数据API还是其他API之一,每个WordPress开发人员都应该始终可以快速访问WordPress Codex,以便快速研究可用的API以及如何使用它们。

在完成本系列文章的过程中,您应该了解围绕元数据设计的API如何保持一致。 这使得易于使用的强大API可以扩展某些WordPress核心功能。

此外,我已经提到WordPress 4.4中引入了另一个元数据API。 我计划在即将到来的系列文章中对此进行介绍,但是这需要检查基于它的其他一些功能。

目前,我们已经介绍了许多材料,以帮助您提高现有的WordPress技能。 在下一篇文章之前,请记住我的个人资料页面上提供了我的所有课程和教程,并且您可以在我的博客上阅读有关WordPress和WordPress开发的更多文章。

WordPress经济活跃。 有主题,插件,库和许多其他产品可帮助您构建站点和项目。 该平台的开源性质也使其成为一个很好的选择,您可以从中提高自己的编程技能。 无论如何,您都可以在Envato Marketplace中看到我们所能提供的一切

随时在Twitter和@tommcfarlin上关注我,在这里我谈论各种软件开发实践,其中许多与Web开发相关,其中许多专门针对WordPress。

与我的所有文章一样,请不要在下面的提要中留下任何问题或评论,我将致力于回答每一个。

翻译自: https://code.tutsplus.com/tutorials/how-to-work-with-wordpress-comment-metadata--cms-25885

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值