alessandrotesoro / wp-codeless-lib
一个辅助库,包含我所有WP项目中共用的功能。
Requires
- php: >=5.3.2
- composer/installers: ~1.0
This package is not auto-updated.
Last update: 2020-01-24 15:52:32 UTC
README
一个辅助库,包含我所有WP项目中共用的功能。
功能
- 2个函数用于检查WP_DEBUG和SCRIPT_DEBUG是否已定义。
- 实用类,用于显示管理员通知,并可通过AJAX取消通知。
- 实用函数,用于添加插件操作链接。
- 实用函数,用于添加插件行元链接。
- 实用函数,用于添加插件消息(类似于插件页面中的更新消息)。
- 计数气泡。
- 辅助函数,用于添加管理员菜单栏项目。
- 向用户表添加列。
- 向文章类型表添加列。
- 向分类表添加列。
- 向文章行添加新的操作链接。
- 一个jQuery模态界面,模仿WP媒体管理器模态的外观和感觉。
初始化类
将 wp-codeless-lib.php
文件包含到您的项目中,然后
$helper = new TDP\Codeless;
检查WP_DEBUG是否已定义
if( $helper::is_development() )
检查WP_DEBUG和SCRIPT_DEBUG是否已定义
if( $helper::is_script_debug() )
显示管理员通知
$helper::show_admin_notice( $content = 'The message' , $type = 'success' , $id );
如果 $id
已定义,消息将被设置为“粘性”。粘性通知将保持可见,直到用户取消消息。
已取消的消息存储在 wp_codeless_dismissed_notices
选项中。
添加插件操作链接
$helper::add_plugin_action_link( $plugin_slug = 'plugin-folder/plugin-file.php', $label = 'Custom link', $link = '#' );
添加插件行元链接
$helper::add_plugin_meta_link( $plugin_slug = 'plugin-folder/plugin-file.php', $label = 'Custom link', $link = '#' );
添加插件消息
此消息出现在插件页面中的插件行下方。通常此区域也用于显示WP的更新通知。
$helper::add_plugin_message( $plugin_slug = 'plugin-folder/plugin-file.php', $message = 'Message', $type = 'update-message' );
$type
定义添加到消息容器的类。当使用 update-message
时,消息将使用与更新通知相同的布局。
添加计数气泡
$helper::add_count_bubble( $key = '10', $counter = '11' );
访问全局变量 $menu
以找到您的菜单键。
向管理员菜单栏添加项目
$helper::add_menu_bar_item( $args );
有关 $args 的详细信息,请参阅 https://codex.wordpress.org/Class_Reference/WP_Admin_Bar/add_menu
向用户表添加列
$helper::add_user_column( $label = 'Custom column', $callback = 'my_callback_function', $priority = 10 ); function my_callback_function( $user_id ) { echo $user_id; }
添加文章类型列
$helper::add_post_type_column( $post_types = 'post', $label = 'Custom column', $callback = 'my_callback_function', $priority = 10 ); function my_callback_function( $post_id ) { echo $post_id; }
$post_types
也可以是一个文章类型的数组。
添加分类列
$helper::add_taxonomy_column( $taxonomies = 'post_tag', $label = 'Custom column', $callback = 'my_callback_function', $priority = 10 ); function my_callback_function( $tax_id ) { echo $tax_id; }
$taxonomies
也可以是一个分类的数组。
添加帖子行操作链接
php $helper::add_post_row_action( $post_type = 'post', $label = 'Custom link', $link = '#' );
使用内置模态界面
加载所需的脚本
function codeless_modal_scripts() { $helper = new TDP\Codeless; $helper::add_ui_helper_files(); } add_action( 'admin_enqueue_scripts', 'codeless_modal_scripts' );
在管理页面内创建模态框
jQuery部分
<script> jQuery(document).ready(function ($) { jQuery( '.trigger' ).click(function() { var popup = null; popup = codelessUi.popup() .modal( true ) .size( 740, 480 ) .title( 'Window title' ) .content( '.popupcontainer' ) .show(); }); }); </script>
HTML部分
<a href="#" class="trigger">Trigger</a> <div class="popupcontainer" style="display:none"> Yo! </div>
Ps: 不要使用内联CSS,这只是一个示例。
插件模板加载器
模板文件是主题的标准。例如,如果用户想要自定义模板文件,他们只需将其复制到子主题中并修改即可。遗憾的是,除非开发者构建自己的自定义模板加载器,否则插件无法实现这一点。
许多插件,如WooCommerce和EDD,都有它们自己的模板构建器,以便开发者可以自定义插件的外观。
Codeless库附带自己的模板加载器,可以重复使用任意多次。它高度灵感来源于WP Job Manager插件和WooCommerce。
Codeless库中的模板加载器也能识别哪些模板文件被主题覆盖,以及主题中的模板文件是否过时。
以下是以何种顺序加载模板文件的
- 子主题
- 父主题
- 插件文件夹
以下是一个如何使用模板加载器的示例。
扩展模板加载器类并为其插件进行配置
define( 'RESTAURANT_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); class Restaurant_Template_Loader extends \TDP\Plugin_Template_Loader { // Prefix for the filters within the class. protected $filter_prefix = 'restaurant_plugin'; // Path to the plugin. protected $plugin_directory = RESTAURANT_PLUGIN_DIR; // Name of the folder that contains all the templates within the plugin. protected $plugin_template_directory = 'templates'; // Name of the folder that contains the templates within a theme. protected $theme_template_directory = 'restaurant-templates'; } $restaurant_template_loader = new Restaurant_Template_Loader;
加载模板文件
要加载模板文件,您现在可以访问get_template方法
$restaurant_template_loader->get_template( 'file.php', array( 'my_variable' => 'value' ) );
您可以通过数组传递参数到模板文件,然后访问其中的数据 echo $my_variable
。
检查被覆盖的文件
如果您想检查哪些文件被主题覆盖,可以访问get_overwritten_template_files
方法
$restaurant_template_loader->get_overwritten_template_files();
该方法将返回一个包含被覆盖文件详情以及文件是否过时的数组的数组。
如果您想使用此功能,您的模板文件必须在文件顶部有以下注释。您必须定义@version。
/** * Template file comments here... * * @version 1.0.0 */