近年来,大多数在线新闻和信息发布网站都采用了免费增值模式,即非注册会员的读者只能阅读一定数量的文章 ; 另一方面,付费的注册用户无限制地访问文章。
在本文中,我们将向您展示如何构建一个简单的插件, 该插件使WordPress驱动的网站的管理员能够将某些帖子,页面和帖子内容的一部分仅限制为注册用户。
编码插件
在编写WordPress插件时,标题(PHP注释块)部分包含诸如插件的名称,描述,作者和作者URL之类的信息。 这是插件头:
<?php
/*
Plugin Name: Restrict Content to registered users
Plugin URI: http://hongkiat.com
Description: Restricting content to registered users only
Version: 0.1
Author: Agbonghama Collins
Author URI: http://tech4sky.com
*/
插件将具有一个由表单字段组成的设置页面,其中将包含要限制的帖子或页面ID。
下面的代码将在“将Restrict content To Registered User
”的设置中添加一个子菜单。
add_action('admin_menu', 'rcru_plugin_menu');
// Adding Submenu to settings
function rcru_plugin_menu() {
add_options_page(
'Restrict content To Registered User',
'Restrict content To Registered User',
'manage_options',
'rcru-restrict-content-user',
'rcru_content_user_settings'
);
}
上面传递给add_options_page
的第五个参数rcru_content_user_settings
是将输出插件设置内容的函数。
function rcru_content_user_settings() {
echo '<div class="wrap">';
screen_icon();
echo '<h2>Restrict content To Registered User</h2>';
echo '<form action="options.php" method="post">';
do_settings_sections('rcru-restrict-content-user');
settings_fields('rcru_settings_group');
submit_button();
}
表单缺少<input>
字段,并且还不能将数据保存到数据库,因为我们尚未实现WordPress 设置API 。
函数plugin_option
定义设置部分和字段。
// plugin field and sections
function plugin_option() {
add_settings_section(
'rcru_settings_section',
'Plugin Options',
null,
'rcru-restrict-content-user
');
add_settings_field(
'post-page-id',
'<label for="post-page-id">Post and page ID to be restricted.</label>',
'post_page_field',
'rcru-restrict-content-user',
'rcru_settings_section'
);
// register settings
register_setting('rcru_settings_group', 'rcru_post-id-option');
请注意,传递给上述add_settings_field
函数的第三个参数post_page_field
被调用以回显<input>
字段形式。
function post_page_field() {
echo "Enter Post or Page ID separated by comma. ";
echo '<input style="width: 300px; height:80px" type="text" id="post-page-id" name="rcru_post-id-option" value="' . get_option('rcru_post-id-option') . '">';
}
最后,将plugin_option
函数挂接到admin_init
操作以使其生效。
add_action('admin_init', 'plugin_option');
我们已经完成了构建插件设置页面的工作,但是如果不使用设置页面保存到数据库的数据有什么用呢?
接下来是函数restrict_content_register_user
的编码,该函数将检索仅限于注册用户的帖子或页面ID(已将其保存在插件设置页面中的数据库中)。 这样可以确保当前正在查看帖子的用户已注册; 否则将显示一条错误消息,提示用户进行注册。
最后,该函数将挂接到the_content过滤器上,以影响帖子或页面中的更改。
function restrict_content_register_user($content) {
global $post;
$post_database = get_option('rcru_post-id-option');
$post_database = explode(',', $post_database);
$current_user = wp_get_current_user();
/* If there is no content, return. */
if (is_null($content))
return $content;
foreach ($post_database as $posts) {
$posts = trim($posts);
if ($posts == $post -> ID) {
if (username_exists($current_user -> user_login)) {
/* Return the private content. */
return $content;
} else {
/* Return an alternate message. */
return '<div align="center" style="color: #fff; padding: 20px; border: 1px solid border-color: rgb(221, 204, 119); background-color: #3B5998">
You must be a registered user to read this content.
<br/>
<a style="font-size: 20px;" href="' . get_site_url() . '/wp-login.php?action=register">Register Here</a>
</div>';
}
}
}
return $content;
}
add_filter('the_content', 'restrict_content_register_user');
现在,我们完成了插件工作的第一种方式:使用插件设置页面。
剩下的就是向插件添加metabox和shortcode功能 。
添加元框
要在帖子和页面编辑屏幕中添加包含复选框的metabox,则在勾选复选框时将允许该帖子或页面限制为注册用户。 钩到add_meta_boxes
动作时,函数rcru_mb_create
将在所有帖子和页面中包含元框。
function rcru_mb_create() {
/**
* @array $screens Write screen on which to show the meta box
* @values post, page, dashboard, link, attachment, custom_post_type
*/
$screens = array(
'post',
'page'
);
foreach ($screens as $screen) {
add_meta_box('rcru-meta',
'Restrict Post / Page',
'rcru_mb_function',
$screen,
'normal',
'high');
}
}
add_action('add_meta_boxes', 'rcru_mb_create');
函数rcru_mb_function
包含复选框和对元框的描述。
function rcru_mb_function($post) {
//retrieve the metadata values if they exist
$restrict_post = get_post_meta($post -> ID, '_rcru_restrict_content', true);
// Add an nonce field so we can check for it later when validating
wp_nonce_field('rcru_inner_custom_box', 'rcru_inner_custom_box_nonce');
echo '<div style="margin: 10px 100px; text-align: center">
<table>
<tr>
<th scope="row"><label for="rcru-restrict-content">Restrict content?</label></th>
<td>
<input type="checkbox" value="1" name="rcru_restrict_content" id="rcru-restrict-content"' . checked($restrict_post, 1, false) . '>
<span class="description">Checking this setting will restrict this post to only registered users.</span>
</td>
</tr>
</table>
</div>';
}
function rcru_mb_save_data($post_id) {
/*
* We need to verify this came from the our screen and with proper authorization,
* because save_post can be triggered at other times.
*/
// Check if our nonce is set.
if (!isset($_POST['rcru_inner_custom_box_nonce']))
return $post_id;
$nonce = $_POST['rcru_inner_custom_box_nonce'];
// Verify that the nonce is valid.
if (!wp_verify_nonce($nonce, 'rcru_inner_custom_box'))
return $post_id;
// If this is an autosave, our form has not been submitted, so we don't want to do anything.
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
return $post_id;
// Check the user's permissions.
if ('page' == $_POST['post_type']) {
if (!current_user_can('edit_page', $post_id))
return $post_id;
} else {
if (!current_user_can('edit_post', $post_id))
return $post_id;
}
/* OK, its safe for us to save the data now. */
// If old entries exist, retrieve them
$old_restrict_post = get_post_meta($post_id, '_rcru_restrict_content', true);
// Sanitize user input.
$restrict_post = sanitize_text_field($_POST['rcru_restrict_content']);
// Update the meta field in the database.
update_post_meta($post_id, '_rcru_restrict_content', $restrict_post, $old_restrict_post);
}
//hook to save the meta box data
add_action('save_post', 'rcru_mb_save_data');
函数restrict_content_metabox
将检查给定的帖子或页面,看是否有限制,检查阅读该帖子的用户是否已注册,并显示通知用户注册。
function restrict_content_metabox($content) {
global $post;
//retrieve the metadata values if they exist
$post_restricted = get_post_meta($post -> ID, '_rcru_restrict_content', true);
// if the post or page has restriction and the user isn't registered
// display the error notice
if ($post_restricted == 1 && (!username_exists(wp_get_current_user()->user_login)) ) {
return '<div align="center" style="color: #fff; padding: 20px; border: 1px solid #ddd; background-color: #3B5998">
You must be a registered user to read this content.
<br/>
<a style="font-size: 20px;" href="' . get_site_url() . '/wp-login.php?action=register">Register Here</a>
</div>';
}
return $content;
}
// hook the function to the post content to effect the change
add_filter('the_content', 'restrict_content_metabox');
添加简码
使用简码,帖子的一部分可以只限于注册用户。
函数rcru_user_shortcodes
包含定义了简码标签[rcru-private]
的简码挂钩函数add_shortcode
。 传递给add_shortcode
的第二个参数是在使用简码时调用的回调函数。
/* Function for registering the shortcode. */
function rcru_user_shortcodes() {
/* Adds the [rcru-private] shortcode. */
add_shortcode('rcru-private', 'rcru_shortcode');
}
然后将该功能注册为init
操作,以便WordPress内部人员可以识别该功能。
/* Register shortcodes in 'init'. */
add_action('init', 'rcru_user_shortcodes');
最后, rcru_shortcode
回调函数处理简码输出。
/* Function for handling shortcode output. */
function rcru_shortcode($attr, $content = '') {
/* Check if the current user has the 'read_private_content' capability. */
$current_reader = wp_get_current_user();
if (!username_exists($current_reader -> user_login)) {
/* Return an alternate message. */
return '<div align="center" style="color: #fff; padding: 20px; border: 1px solid border-color: rgb(221, 204, 119); background-color: #3B5998">
You must be a registered user to read this content.
<br/>
<a style="font-size: 20px;" href="' . get_site_url() . '/wp-login.php?action=register">Register Here</a>
</div>';
}
}
插件如何工作
插件将具有一个设置页面,该页面具有一个表单字段,该表单字段接受要限制的邮递区号,并以逗号分隔。
要限制给定的帖子或页面,请在相应的ID中输入相应的ID,并用逗号(,)分隔。 表格栏位。 要获取帖子的ID,请转到帖子编辑屏幕(用于编写帖子内容的TinyMCE编辑器),然后查看页面的URL。 附加到?post=
是帖子ID。
例如,对于http://wordpress.dev/wp-admin/post.php?post=88&action=edit
,数字88是帖子或页面ID。
帖子和页面编辑屏幕中的Metabox
通过选中位于metabox处的Restrict content复选框(由插件添加到post edit屏幕 ),也可以将帖子或页面限制为注册用户。
短代码
帖子或页面内容的部分或部分可以在公共视图中隐藏,并可以使用插件短代码仅向注册成员显示:
[rcru-private]Part of post or page content to be restricted to only registered users.[/rcru-private]
最后,这是插件文件的链接。 随意探索代码并编写愉快的代码!
编者注 :这篇文章是由Collins Agbonghama为Hongkiat.com撰写的。 Agbonghama是白天的Web开发人员,而晚上是自由作家/博客作者。 不用担心编写代码时,他喜欢睡在沙发上并在个人博客w3guy.com上写作。 您可以在Facebook和G +上找到他。
翻译自: https://www.hongkiat.com/blog/build-a-wordpress-plugin-to-restrict-content-to-registered-user/