一小段代码,扩展了 WordPress 的功能。 这就是如何在一个句子中描述片段的方式。 那模块也起到这个作用? 当然,它们在本质上非常相似,但有两个关键因素决定了片段的独特性:它们不需要安装,并且由于它们的体积最小,它们不会给服务器带来负担。
Obsah
如何使用 WordPress 片段?
存储片段最常见的位置是活动主题中的 functions.php 文件,并且对于在 wp-config.php 文件中的全局使用同样有用。 只需打开文本编辑器并将适当的代码添加到这些文件中。 手动插入片段的一个缺点是它们与当前激活的主题的连接,除了上面提到的 wp-config.php 例外。
解决方案是Code Snippets模块,它可以系统地运行并在管理环境中提供舒适的管理环境,除了强制添加、修改、删除自己的代码之外,还可以通过 XML 文件对其进行备份。 可以根据需要激活和关闭单个片段,代码片段也可用于多站点安装,该模块的斯洛伐克语翻译肯定会为您带来好处。
实践中的 WordPress 片段
随着 WordPress 的日益普及,有趣的片段的数量也在增加。 以下选择是一些有用的解决方案的示例,文章末尾可以找到一些其他资源的链接。
此代码段从页眉中删除 WP 版本标签
function remove_wp_version() { return ''; } add_filter('the_generator', 'remove_wp_version');
我们将来自 Google API 的 jQuery 插入到标题中
if( !is_admin() ){ wp_deregister_script('jquery'); wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"), false, ''); wp_enqueue_script('jquery'); }
或支持来自 Google Fonts 的斯洛伐克口音的字体
function load_fonts() { wp_register_style ('googleFonts', 'http://fonts.googleapis.com/css?family=News+Cycle&subset=latin,latin-ext'); wp_enqueue_style( 'googleFonts'); } add_action('wp_print_styles', 'load_fonts');
我们将为除管理员以外的所有用户关闭管理栏显示
if (!current_user_can('administrator')): show_admin_bar(false); endif;
我们将在他的个人资料中编辑用户的信息
function new_contactmethods( $contactmethods ) { $contactmethods['twitter'] = 'Twitter'; // pridame Twitter $contactmethods['facebook'] = 'Facebook'; // pridame Facebook unset($contactmethods['yim']); // odoberieme Yahoo IM unset($contactmethods['aim']); // odoberieme AIM unset($contactmethods['jabber']); // odoberieme Jabber return $contactmethods; } add_filter('user_contactmethods','new_contactmethods',10,1);
我们将删除预设的 WordPress 小部件
function unregister_default_widgets() { unregister_widget('WP_Widget_Pages'); unregister_widget('WP_Widget_Calendar'); unregister_widget('WP_Widget_Archives'); unregister_widget('WP_Widget_Links'); unregister_widget('WP_Widget_Meta'); unregister_widget('WP_Widget_Search'); unregister_widget('WP_Widget_Text'); unregister_widget('WP_Widget_Categories'); unregister_widget('WP_Widget_Recent_Posts'); unregister_widget('WP_Widget_Recent_Comments'); unregister_widget('WP_Widget_RSS'); unregister_widget('WP_Widget_Tag_Cloud'); unregister_widget('WP_Nav_Menu_Widget'); unregister_widget('Twenty_Eleven_Ephemera_Widget'); } add_action('widgets_init', 'unregister_default_widgets', 11);
我们将禁用可视化编辑器
add_filter('user_can_richedit' , create_function('' , 'return false;') , 50);
我们将选择自己的贡献列表长度
custom_excerpt_length($length) { return 100; } add_filter('excerpt_length', 'custom_excerpt_length');
我们将在用户登录后更改重定向
add_action('login_form', 'redirect_after_login'); function redirect_after_login() { global $redirect_to; if (!isset($_GET['redirect_to'])) { $redirect_to = get_option('siteurl'); // presmerujeme na homepage } }
我们将允许作者在 7 天内编辑已发表的文章
function stop_post_editing_filter( $capauser, $capask, $param){ global $wpdb; $post = get_post( $param[2] ); if( $post->post_status == 'publish' ){ // obmedzenie bude platit pre rolu Autor if( $capauser['author'] == 1 ){ if( ( $param[0] == "edit_post") || ( $param[0] == "delete_post" ) ) { $post_time_unix = strtotime( str_replace('-', ':', $post->post_date ) ); $current_time_unix = time(); $diff = $current_time_unix - $post_time_unix; $hours_after_publication = floor( $diff / 60 / 60 ); // po 168 hodinach od publikovania bude automaticky vypnuta moznost dalsej upravy prispevku if( $hours_after_publication >= 168 ){ foreach( (array) $capask as $capasuppr) { if ( array_key_exists($capasuppr, $capauser) ) { $capauser[$capasuppr] = 0; } } } } } } return $capauser; } add_filter('user_has_cap', 'stop_post_editing_filter', 100, 3 );
我们将自动保存帖子的时间间隔设置为 10 分钟 – 60 秒 x 10(在文件 wp-config.php 中)
define('AUTOSAVE_INTERVAL', 600);
禁用发布修订(在 wp-config.php 中)
define('WP_POST_REVISIONS', false);
或者我们将修订后限制设置为 5
define('WP_POST_REVISIONS', 5)
我们每 5 天自动清空回收站(在 wp-config.php 文件中)
define('EMPTY_TRASH_DAYS', 5 );
我们将插入一个链接来编辑帖子(例如在 single.php 或 page.php 文件中)
<?php edit_post_link ('Upraviť príspevok'); ?>
最著名的 WordPress 片段来源
备份,备份,备份!
墨比定律很清楚:代码中的错误越大,就越会出现意想不到的情况。 为避免可能出现的并发症,请不要忘记备份已编辑的文件和数据库。
您是否创建了自己的片段或在漫游互联网时发现了一些有趣的片段? 在评论中写信给我们。
Was this article helpful for you? Support me by sharing, please. 👍