我正在编写一个插件,需要在管理页面中保存用户特定的选项。我只想在选项中添加用户名前缀,例如:
$options = get_option(\'username-plugin_options\');
有没有更好的方法?我正在编写一个插件,需要在管理页面中保存用户特定的选项。我只想在选项中添加用户名前缀,例如:
$options = get_option(\'username-plugin_options\');
有没有更好的方法?注册设置时(假设使用的是设置API),可以使用wp_get_current_user
. 它返回一个用户对象,您要在设置中使用的属性是user_nicename
-- 把它想象成用户的鼻涕虫。
<?php
add_action( \'admin_init\', \'wpse27690_register_setting\' );
function wpse27690_register_setting()
{
$user = wp_get_current_user();
register_setting( $user->user_nicename . \'_plugin_options\', $user->user_nicename . \'_plugin_options\', \'wpse27690_sanitize\' );
}
function wpse27690_sanitize( $in )
{
// clean stuff here.
return $in;
}
在检索选项时,也可以执行相同的操作。正如拉斯特在评论中指出的那样,这可能不是一个好主意。而且它可能不适用于“订阅者”级别的用户或那些无法管理选项的用户。但如果只有一个或两个选项,最简单的方法就是向用户配置文件页面添加字段。不确定这是否是您想要的,但您可以这样做:
<?php
add_action( \'show_user_profile\', \'wpse27690_user_form\' ); // for you profile
add_action( \'edit_user_profile\', \'wpse27690_user_form\' ); // for other\'s profiles
function wpse27690_user_form( $user )
{
// put your form fields here, probably a nonce or something
}
add_action( \'personal_options_update\', \'wpse27690_user_save\' ); // you profile
add_action( \'edit_user_profile_update\', \'wpse27690_user_save\' ); // other\'s profiles
function wpse27690_user_save( $user_id )
{
// check nonces, permssions first
// then save
if( isset( $_POST[\'_your_fields_name\'] ) )
update_user_meta( $user_id, \'wpse27690_user_key\', esc_attr( $_POST[\'_your_fields_name\'] ) );
}
当您保存单个用户选项时update_user_meta
我正在设计一个主题,其中主页(即。is_home()) 应该只显示最新的博客文章。我想做点什么$wp_query->update_option(\'posts_per_page\', 1) 每次我在主页上。所有其他显示帖子(如归档)的页面都应该遵循用户定义的选项。每次都这么做似乎有点不必要,因为选项应该只设置一次,对吗?是否可以在循环中忽略posts_per_page 和力have_posts() 只设一个职位?一般来说,这种“一次性设置”的东西应该去哪里?我真的不认为它应该是一个插件,因为它是特定