我认为您需要的是一个自定义重写规则,特别是一个自定义端点。
这将不得不住在s2member之外。
首先,将所有内容包装在一个类中:
<?php
class WPSE71804
{
// post type key, whatever this happens to be.
const TYPE = \'customer\';
// endpoint mask, 2 ^ 18
const EP = 262144;
// key prefix, used for options
const PREFIX = \'wpse71804_key_\';
// container for the instance of this class
private static $ins = null;
public static function instance()
{
is_null(self::$ins) && self::$ins = new self;
return self::$ins;
}
public static function init()
{
add_action(\'plugins_loaded\', array(self::instance(), \'_setup\'));
}
// add actions and such.
public function _setup()
{
// we\'ll add actions here later.
}
}
这里有一些常量,我们稍后将使用。
您需要修改post类型注册,以便在rewrite
论点
<?php
class WPSE71804
{
// snip snip
// add actions and such.
public function _setup()
{
add_action(\'init\', array($this, \'register\'));
}
// register the post type
public function register()
{
// rewrite is the args to pay attention to we need
// to set a custom endpoint mask
register_post_type(self::TYPE, array(
\'label\' => __(\'Customers\', \'wpse\'),
\'public\' => true,
\'rewrite\' => array(
\'slug\' => \'customer\',
\'ep_mask\' => self::EP,
\'with_front\' => false,
),
));
}
}
从那里,我们可以
init
和电话
add_rewrite_endpoint
.
这将设置重写,以便我们可以转到yoursite.com/customers/the-post/key/some_key_here
.
<?php
class WPSE71804
{
// snip snip
// add actions and such.
public function _setup()
{
add_action(\'init\', array($this, \'register\'));
add_action(\'init\', array($this, \'endpoint\'), 11);
}
// snip snip
public function endpoint()
{
add_rewrite_endpoint(\'key\', self::EP);
}
}
现在只需要
template_redirect
并验证密钥。
<?php
class WPSE71804
{
// snip snip
public static function init()
{
add_action(\'plugins_loaded\', array(self::instance(), \'_setup\'));
register_activation_hook(__FILE__, array(__CLASS__, \'activate\'));
}
// add actions and such.
public function _setup()
{
add_action(\'init\', array($this, \'register\'));
add_action(\'init\', array($this, \'endpoint\'), 11);
add_action(\'template_redirect\', array($this, \'validate_key\'));
}
// snip snip
public function validate_key()
{
// not a a singular customer page? Or have an admin user? bail.
if(!is_singular(self::TYPE) || current_user_can(\'manage_options\'))
return;
if(!($_key = get_query_var(\'key\')) || !($key = self::get_key($_key)))
{
global $wp_query;
$wp_query->set_404();
}
// if we\'re here, the key is okay, let the request go through
}
}
创建一个好的API来使用可能也很有帮助(上面的代码使用了其中的一种方法)。
<?php
class WPSE71804
{
// snip snip
/********** API **********/
// create a new key
public static function create_key()
{
$k = wp_generate_password(24, false);
self::update_key($k, \'notdone\');
return $k;
}
// update a key
public static function update_key($key, $val=\'done\')
{
return update_option(self::PREFIX . $key, $val);
}
// delete a key
public static function delete_key($key)
{
return delete_option(self::PREFIX . $key);
}
public static function get_key($key)
{
return get_option(self::PREFIX . $key);
}
}
现在你可以使用上面的类似。。。
<?php
// create a key
$k = WPSE71804::create_key();
// send mail with key here!
// after they submit the survey, you might want to make a note of that.
WPSE71804::update_key($k, \'done\');
// or maybe just delete it and revoke access to the page
WPSE71804::delete_key($k);
不确定s2member的使用效果如何,但基本上这将阻止所有前端没有键的页面访问。您可能根本不需要限制对s2member的访问。这些都是
as a plugin.