I:1和b:1中的UPDATE_META_USER差异,如何写入b:1

时间:2014-02-18 作者:LPH

此代码导致将wp\\U能力写入数据库,但前面有一个i:1。

if( $new_roles === \'administrator\' ) {
    $capabilities = array ("administrator",\'activate_plugins\',\'delete_others_pages\',\'delete_others_posts\',\'delete_pages\',\'delete_plugins\',\'delete_posts\',\'delete_private_pages\',\'delete_private_posts\',\'delete_published_pages\',\'delete_published_posts\',\'edit_dashboard\',\'edit_files\',\'edit_others_pages\',\'edit_others_posts\',\'edit_pages\',\'edit_posts\',\'edit_private_pages\',\'edit_private_posts\',\'edit_published_pages\',\'edit_published_posts\',\'edit_theme_options\',\'export\',\'import\',\'list_users\',\'manage_categories\',\'manage_links\',\'manage_options\',\'moderate_comments\',\'promote_users\',\'publish_pages\',\'publish_posts\',\'read_private_pages\',\'read_private_posts\',\'read\',\'remove_users\',\'switch_themes\',\'upload_files\',\'create_product\');
    $user->set_role(\'administrator\');
    update_user_meta( $user_id, \'wp_capabilities\', $capabilities );
    update_user_meta( $user_id, \'wp_user_level\', 10 );
}
这是放置在数据库中的内容:

a:39:i:0;s:13:"administrator";i:1;s:16:"activate_plugins";i:2;s:19:"delete_others_pages";i:3;s:19:"delete_others_posts";i:4;s:12:"delete_pages";i:5;s:14:"delete_plugins";i:6;s:12:"delete_posts";i:7;s:20:"delete_private_pages";i:8;s:20:"delete_private_posts";i:9;s:22:"delete_published_pages";i:10;s:22:"delete_published_posts";i:11;s:14:"edit_dashboard";i:12;s:10:"edit_files";i:13;s:17:"edit_others_pages";i:14;s:17:"edit_others_posts";i:15;s:10:"edit_pages";i:16;s:10:"edit_posts";i:17;s:18:"edit_private_pages";i:18;s:18:"edit_private_posts";i:19;s:20:"edit_published_pages";i:20;s:20:"edit_published_posts";i:21;s:18:"edit_theme_options";i:22;s:6:"export";i:23;s:6:"import";i:24;s:10:"list_users";i:25;s:17:"manage_categories";i:26;s:12:"manage_links";i:27;s:14:"manage_options";i:28;s:17:"moderate_comments";i:29;s:13:"promote_users";i:30;s:13:"publish_pages";i:31;s:13:"publish_posts";i:32;s:18:"read_private_pages";i:33;s:18:"read_private_posts";i:34;s:4:"read";i:35;s:12:"remove_users";i:36;s:13:"switch_themes";i:37;s:12:"upload_files";i:38;s:14:"create_product";}
这些功能不适用于该用户。

将以下内容简单复制/粘贴到数据库中gives the user the capabilities

a:27:{s:13:"administrator";b:1;s:12:"delete_posts";b:1;s:22:"delete_published_posts";b:1;s:10:"edit_posts";b:1;s:20:"edit_published_posts";b:1;s:13:"publish_posts";b:1;s:4:"read";b:1;s:12:"upload_files";b:1;s:19:"delete_others_pages";b:1;s:19:"delete_others_posts";b:1;s:12:"delete_pages";b:1;s:11:"delete_post";b:1;s:20:"delete_private_pages";b:1;s:20:"delete_private_posts";b:1;s:22:"delete_published_pages";b:1;s:17:"edit_others_pages";b:1;s:17:"edit_others_posts";b:1;s:10:"edit_pages";b:1;s:18:"edit_private_pages";b:1;s:18:"edit_private_posts";b:1;s:20:"edit_published_pages";b:1;s:17:"manage_categories";b:1;s:12:"manage_links";b:1;s:17:"moderate_comments";b:1;s:13:"publish_pages";b:1;s:18:"read_private_pages";b:1;s:18:"read_private_posts";b:1;}

What is the difference between the i:1 and b:1 ? How can I get the update_meta_user to add the b:1?

我找不到一个文档,但很好奇这是否是I表示整数,b表示布尔值。在这种情况下,如何获取此代码来更新wp\\u cabailities数据库字段?

1 个回复
SO网友:s_ha_dum

您的第一个序列化字符串已断开。我不知道为什么,但您不应该试图手动操作功能。您已经有一个用户对象。使用它。

if( $new_roles === \'administrator\' ) {
  $capabilities = array ("administrator",\'activate_plugins\',\'delete_others_pages\',\'delete_others_posts\',\'delete_pages\',\'delete_plugins\',\'delete_posts\',\'delete_private_pages\',\'delete_private_posts\',\'delete_published_pages\',\'delete_published_posts\',\'edit_dashboard\',\'edit_files\',\'edit_others_pages\',\'edit_others_posts\',\'edit_pages\',\'edit_posts\',\'edit_private_pages\',\'edit_private_posts\',\'edit_published_pages\',\'edit_published_posts\',\'edit_theme_options\',\'export\',\'import\',\'list_users\',\'manage_categories\',\'manage_links\',\'manage_options\',\'moderate_comments\',\'promote_users\',\'publish_pages\',\'publish_posts\',\'read_private_pages\',\'read_private_posts\',\'read\',\'remove_users\',\'switch_themes\',\'upload_files\',\'create_product\');
  $user->set_role(\'administrator\');
  foreach ($capabilities as $cap) {
    $user->add_cap($cap);
  }
}
未经测试,但应该有效。但是,您只需要显式添加角色尚未暗示的功能。您可以使该功能阵列大大缩短。

结束

相关推荐

Plugins_url函数混合了系统路径和URL

在我的WordPress小部件中,我使用以下代码:wp_register_script(\'jquery-ui.widget\', plugins_url(\'assets/js/jquery-ui-1.9.2.widget.js\', dirname( __FILE__ ))); 不幸的是,代码给了我一个无效的URL,它与我的系统路径混合在一起:http://test.dev/wp-content/plugins/C:/projects/wordpress/plugins/assets/js/