wordpress 插件_WordPress插件:从首页删除类别中的帖子

wordpress 插件

WordPress Plugin

Being a WordPress user for some time, I've always wanted to create a WordPress plugin but I've hesitated.  I've not run into an issue that I couldn't find an existing plugin for, and the thought of maintenance on a WordPress plugin seemed daunting because I only use WordPress for this blog.  With an added feature to my redesign, however, I found the perfect use case:  preventing posts in a giving category from displaying on the homepage.  I've detailed the PHP snippet for preventing posts in a category from the homepage, but that only works within the theme that hosts the functions.php file.  By creating the Remove Posts in Category From Homepage plugin, this functionality now works across all themes!

作为WordPress用户已有一段时间了,我一直想创建一个WordPress插件,但是我很犹豫。 我还没有遇到找不到现有插件的问题,而对WordPress插件进行维护的想法似乎令人生畏,因为我仅在本博客中使用WordPress。 但是,通过重新设计,我找到了一个完美的用例:阻止给定类别中的帖子显示在首页上。 我已经详细介绍了PHP代码段,该代码段可阻止主页中某类别的帖子 ,但这仅在承载functions.php文件的主题内有效。 通过创建“从首页删除类别中的帖子”插件,此功能现在可以在所有主题下使用!

设置 (The Setup)

The goal was to ensure the plugin was as lightweight and simple as possible.  Other goals included:

目的是确保插件尽可能轻巧和简单。 其他目标包括:

  • Encompassing all plugin code in one file

    将所有插件代码包含在一个文件中
  • Using only one database record;  to do this, we'll keep an array of categories which should be prevented from hitting the homepage

    仅使用一个数据库记录; 为此,我们将保留一系列类别,应防止其访问首页
  • Allowing the ability to toggle this option on the "Add Category" and "Edit Category" pages

    允许在“添加类别”和“编辑类别”页面上切换此选项的功能

Achieving these goals became simple due to WordPress' awesome plugin API.

由于WordPress出色的插件API,实现这些目标变得很简单。

PHP (The PHP)

I'll explain the PHP code step by step.  We start out by defining a few important variables:

我将逐步解释PHP代码。 我们首先定义一些重要的变量:


$RCFH_LOOP_LABEL = 'Remove from main loop';
$RCFH_LOOP_DESCRIPTION = 'Check this box if you would like posts in this category to be prevented from displaying within the main loop.';
$RCFH_LOOP_OPTION_KEY = 'remove-loop-cats';


The first two variables will be used in the "Add Category" and "Edit Category" forms, while the last var represents the option name we'll use to set and get categories in the database.  Next we add a checkbox to the "Edit Category" screen:

前两个变量将在“添加类别”和“编辑类别”窗体中使用,而最后一个var表示将用于设置和获取数据库类别的选项名称。 接下来,我们在“编辑类别”屏幕中添加一个复选框:


<?php
	// Add the extra field to the EDIT category page
	add_action('category_edit_form_fields', 'rcfh_loop_field_edit');
	function rcfh_loop_field_edit($term) {
		global $RCFH_LOOP_LABEL, $RCFH_LOOP_DESCRIPTION, $RCFH_LOOP_OPTION_KEY;

		$value = get_option($RCFH_LOOP_OPTION_KEY);
		if(!$value) {
			$value = array();
		}

		$checked = in_array($term->term_id, $value);
 ?>
	<tr class="form-field">
		<th scope="row" valign="top"><label for="removeMainLoop"><?php _e($RCFH_LOOP_LABEL); ?></label></th>
		<td>
			<input type="checkbox" name="remove-loop" id="removeMainLoop"<?php echo $checked ? ' checked="checked"' : ''; ?> value="1" /><br />
			<span class="description"><?php _e($RCFH_LOOP_DESCRIPTION); ?></span>
		</td>
	</tr>
<?php } ?>


The cl_loop_field_edit function, which is executed during the category_edit_form_fields action, retrieves the option value and checks to see if the given category ID is in the list of categories which shouldn't display on the homepage - if so, the checkbox is checked.

在category_edit_form_fields操作期间执行的cl_loop_field_edit函数检索选项值,并检查给定的类别ID是否在不应在首页上显示的类别列表中-如果是,则选中该复选框。

The next step is adding the same form field to the "Add Category" screen:

下一步是将相同的表单字段添加到“添加类别”屏幕:


<?php
	// Add the extra field to the ADD category page
	add_action('category_add_form_fields', 'rcfh_loop_field_create');
	function rcfh_loop_field_create() { 
		global $RCFH_LOOP_LABEL, $RCFH_LOOP_DESCRIPTION;
?>
	<div class="form-field">
		<label for="removeMainLoop"><?php _e($RCFH_LOOP_LABEL); ?></label>
		<input type="checkbox" name="remove-loop" id="removeMainLoop" value="1" />
		<p><?php _e($RCFH_LOOP_DESCRIPTION); ?></p>
	</div>
<?php } ?>


With the checkbox added to both forms, the next step is creating a function which saves the value of the checkbox during add and edit transactions.  The function, named cl_save_loop_value, will execute during edit_category and create_category actions:

将复选框添加到两个表单后,下一步是创建一个函数,该函数在添加和编辑事务期间保存复选框的值。 名为cl_save_loop_value的函数将在edit_category和create_category操作期间执行:


// Add action for saving extra category information
add_action('edit_category', 'rcfh_save_loop_value');
add_action('create_category', 'rcfh_save_loop_value');
function rcfh_save_loop_value($id) {
	global $RCFH_LOOP_OPTION_KEY;

	$value = get_option($RCFH_LOOP_OPTION_KEY);
	if(!$value) {
		$value = array();
	}

	// Add or remove the value
	if(isset($_POST['remove-loop'])) {
		array_push($value, $id);
	}
	else {
		$value = array_diff($value, array($id));
	}

	// Ensure no duplicates, just for cleanliness
	$value = array_unique(array_values($value));

	// Save
	update_option($RCFH_LOOP_OPTION_KEY, $value);
}


You'll note that some cleanup is done at the end to ensure there are no duplicate category keys in the array.  Also know that WordPress serializes the array before placing it in the database.  The last step is creating a pre_get_posts action function to prevent posts in the selected categories from displaying in the main loop:

您会注意到,最后会进行一些清理,以确保数组中没有重复的类别键。 还知道WordPress在将数组放入数据库之前会对其进行序列化。 最后一步是创建一个pre_get_posts操作函数,以防止所选类别中的帖子显示在主循环中:


// Filter for removing said category posts from main loop
add_action('pre_get_posts', 'rcfh_prevent_posts');
function rcfh_prevent_posts($query) {
	global $RCFH_LOOP_OPTION_KEY;

	// Only remove categories if it's the main query/homepage
	if($query->is_home() && $query->is_main_query()) {
		$value = get_option($RCFH_LOOP_OPTION_KEY);

		// Modify query to remove posts which shouldn't be shown
		if(count($value)) {
			$query->set('cat', '-'.implode(',-', $value));
		}
	}
}


And with that, the plugin is complete!  There's a fair amount of PHP but all the user needs to do is enable the plugin and toggle checkboxes!

至此,该插件就完成了! 有大量PHP,但是用户需要做的就是启用插件并切换复选框!

Taking the time to finally create a WordPress plugin was worth it.  I gained appreciation for the numerous WordPress plugin creators out there, and I got better acquainted with WordPress' hook/action system.  Hopefully you find this plugin useful and you find inspiration to have a go at creating your own plugin!

花时间最终创建一个WordPress插件是值得的。 我对众多WordPress插件创建者表示赞赏,并且我对WordPress的钩子/动作系统有了更深入的了解。 希望您发现此插件有用,并从中获得灵感来创建自己的插件!

翻译自: https://davidwalsh.name/wordpress-plugin-homepage

wordpress 插件

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值