wordpress中常用的方法笔记(一)


Update_option() 方法

更新一对配置属性值到数据库。$ option的值需要在插入到数据库之前用$wpdb->prepare方法来进行转义。这个值必须正确的处理。
这个方法可以用来代替add_option,尽管它确实没那么灵活。update_option会去检查这个配置属性是否存在。如果不存在,会使用add_option(‘option_name’,‘option_value’)来进行添加。除非你想用add_option来配置一些特殊参数,不然就用update_option则是个双全的添加与更新的好选择。

<?php update_option( $option, $new_value, $autoload ); ?>

admin_head 动作

//官方文档:http://codex.wordpress.org/Plugin_API/Action_Reference/admin_head
// 当wp后台的头部加载时,执行的 PHP函数 my_custom_admin_head
add_action(‘admin_head’, ‘my_custom_admin_head’);
// 输出一个css样式,改变body的背景颜色

function my_custom_admin_head() {
    
    
        echo '<style>body {background-color: #4AAF48 !important;}</style>';
}

用户权限

在这里插入代码片`if( current_user_can( ‘manage_options’ ) )
{ echo ‘这段文字只会在“管理员”角色登录后显示。’; }
if( current_user_can( ‘publish_pages’ ) && !current_user_can( ‘manage_options’ ) )
{ echo ‘这段文字只会在“编辑”角色登录后显示。’; }
if( current_user_can( ‘publish_posts’ ) && !current_user_can( ‘publish_pages’ ) )
{ echo ‘这段文字只会在“作者”角色登录后显示。’; }
if( current_user_can( ‘edit_posts’ ) && !current_user_can( ‘publish_posts’ ) )
{ echo ‘这段文字只会在“投稿者”角色登录后显示。’; }
if( current_user_can( ‘read’ ) && !current_user_can( ‘edit_posts’ ) )
{ echo ‘这段文字只会在“订阅者”角色登录后显示。’; }
?>`

WordPress 会员等级首先先来了解WordPress会员的等级区分:
管理者:Administrator: level 10
编辑:Editor: Level 7
作者:Author: Level 4
撰写人员:Contributor: Level 2
订阅者:Subscriber: Level 0
一般访客(非会员): Level 在 0 以下
以下将使用官方提供的函数: current_user_can 来做会员等级区分.
仅允许管理员可浏览
这里显示的内容为管理员可以看到的.
依照会员等级显示不同的内容
管理员可看到
编辑可看到
作者可看到
撰写者可看到
订阅者可看到
一般非会员(未登入者)可看到

after_setup_theme钩子

after_setup_theme钩子在主题被初始化之后在每个页面加载期间被调用。通常用于执行主题的基本设置,注册和初始化操作。
语法

<?php add_action( 'after_setup_theme', 'function_name' ); ?>

其中“function_name”是要调用的函数的名称。

实例
/ *当’after_setup_theme’钩子运行时,让WordPress运行viti_setup()。 /
add_action(‘after_setup_theme’,‘viti_setup’);

/ **
*设置主题默认值并打开WordPress各种自带功能。
*

  • functions.php文件中。

@uses add_theme_support()添加对帖子缩略图等功能的支持。
@uses register_nav_menus()添加对导航菜单的支持。
@uses add_custom_background()添加对自定义背景的支持。
@uses add_editor_style()设置可视化编辑器的样式。
@uses load_theme_textdomain()用于翻译/本地化支持。
@uses add_custom_image_header()添加对自定义标题的支持。
@uses register_default_headers()注册主题提供的默认自定义标题图像。
@uses set_post_thumbnail_size()设置自定义的发布缩略图大小。
*

  • /
function viti_setup(){
    
    

//这个主题使用editor-style.css风格的可视化编辑器来匹配主题风格。
add_editor_style();

// Post Format支持。也就是文章编辑页面右侧文章形式。
add_theme_support('post-formats'array'aside''gallery'));

//打开缩略图功能
add_theme_support('post-thumbnails';

//添加默认的帖子和评论RSS feed链接到head
add_theme_support('automatic-feed-links';

//使主题可用于翻译
//翻译可以在/ languages /目录中提交
load_theme_textdomain('viti',get_template_directory()。'/ languages';

$ locale = get_locale();
$ locale_file = get_template_directory()。“/语言/区域设置$。
require_once($ locale_file);

//这个主题在一个位置使用wp_nav_menu()。
register_nav_menus(array'primary'=> __'Primary Navigation''viti'),
'Secondary'=> __'Secondary Navigation''viti'),
));

//主题允许用户设置自定义背景
add_custom_background();
}

以上是这个钩子的常见用途,不经常使用的没有描述。

猜你喜欢

转载自blog.csdn.net/u014657752/article/details/124535157