从没有CSS的Admin菜单中删除不必要的文本
-
16-10-2019 - |
题
我正在寻找一种从默认的Worpdress Metaboxes中删除所有不超级文本的方法。
最好确保内容不仅通过CSS隐藏,而且实际上是从HTML中删除的,因此甚至不会出现在源中。
我对删除感兴趣的领域包括:
- 单击右上角的帮助按钮,并关联Div/html/html/文本
- 在右现在的仪表板上,我想删除与使用哪个主题以及WordPress版本以及更改主题按钮相关的文本。
- 在底部的“页面属性” Metabox上,它具有文本“需要帮助?使用屏幕右上角的“帮助”选项卡。我希望将此文本删除
- 在“摘录Metabox”下,有我想删除的文本
- 您可能还知道如何删除清理WordPress的任何其他文本。
解决方案
这是问题1的答案。现在没有足够的时间来完成其余的时间。
1.)删除管理员帮助链接按钮
不幸的是,wordpress不能提供钩子来影响管理员右上方的帮助按钮,但是您可以使用一个 有点肮脏的黑客 实现您要完成的目标。
现在您看到了:
(资源: mikeschinkel.com)
现在您没有:
(资源: mikeschinkel.com)
通过打电话 contextual_help
和 admin_notices
挂钩,当帮助链接按钮分别输出之前和之后立即调用的挂钩,您可以捕获输出缓冲区并使用违规的HTML删除 preg_replace()
. 。这 ob_start()
/ob_get_clean()
PHP中的一对功能是您需要缓冲输出,然后捕获缓冲输出的Viola:
class RemoveAdminHelpLinkButton {
static function on_load() {
add_filter('contextual_help',array(__CLASS__,'contextual_help'));
add_action('admin_notices',array(__CLASS__,'admin_notices'));
}
static function contextual_help($contextual_help) {
ob_start();
return $contextual_help;
}
static function admin_notices() {
echo preg_replace('#<div id="contextual-help-link-wrap".*>.*</div>#Us','',ob_get_clean());
}
}
RemoveAdminHelpLinkButton::on_load();
通常,您可以使用此技术来修改或删除WordPress生成的几乎所有HTML输出,通过找到挂钩后和之后,但请注意,这是一种非常脆弱的技术;如果另一个插件从您期望的内容中修改了HTML输出 preg_replace()
可能无法匹配。反正...
3.)从页面属性删除帮助文本metabox
要删除页面的帮助文本属性metabox不需要正则表达式,一个简单的 str_replace()
会做。 (注意找到合适的钩子要使用的时间最多。):
class RemovePageAttributesHelpText {
static function on_load() {
add_action('admin_notices',array(__CLASS__,'admin_notices'));
add_action('dbx_post_sidebar',array(__CLASS__,'dbx_post_sidebar'));
}
static function admin_notices() {
ob_start();
}
static function dbx_post_sidebar() {
$match_text = '<p>Need help? Use the Help tab in the upper right of your screen.</p>';
echo str_replace($match_text,'',ob_get_clean());
}
}
RemovePageAttributesHelpText::on_load();
当您想完成所有操作时,您还可以使用另一种方法 'gettext'
钩。以下代码从页面属性Metabox中删除了帮助文本:
class RemovePageAttributesHelpText {
static function on_load() {
add_filter('gettext',array(__CLASS__,'gettext'),10,4);
}
function gettext($translation, $text, $domain) {
if ($text=='Need help? Use the Help tab in the upper right of your screen.') {
$translation = '';
}
return $translation;
}
}
RemovePageAttributesHelpText::on_load();
请注意,我谨慎使用此钩子,因为它被称为每页负载数百次。 577次仅是为了加载管理仪表板,例如我刚刚测试。因此,如果您使用它,请确保不做任何计算上的事情 “昂贵的” 例如运行RAW SQL查询或类似查询。
4.)从 “摘录Metabox”
我们将使用Technique#3的选项#2从摘录Metabox中剥离帮助文本 (我包括技术#3中的代码,因此该代码替换了#3中的代码):
class RemoveUnwantedPageEditingText {
static function on_load() {
add_action('admin_notices',array(__CLASS__,'admin_notices'));
add_action('dbx_post_sidebar',array(__CLASS__,'dbx_post_sidebar'));
}
static function admin_notices() {
ob_start();
}
static function dbx_post_sidebar() {
$html = str_replace('<p>Need help? Use the Help tab in the upper right of your screen.</p>','',ob_get_clean());
echo str_replace('Excerpts are optional hand-crafted summaries of your content that can be used in your theme.' .
' <a href="http://codex.wordpress.org/Excerpt" target="_blank">Learn more about manual excerpts.</a>','',$html);
}
}
RemoveUnwantedPageEditingText::on_load();
其他提示
杰克·戈德曼(Jake Goldman)在WordCamp Mid-Atlantic的出色演讲提供了一个绝佳的指南: http://www.cmurrayconsulting.com/wordpress-tips/customizing-wordpress-admin/#more-939
下载幻灯片下方出现的良好的主题。 (他问,如果您使用他的代码,请在您的代码评论中将其归因于他。)
他提到您可以按以下方式获得所有窗口小部件ID:var_dump($ wp_meta_boxes ['dashboard']);
您可以:unset($ wp_meta_boxes ['dashboard'] ['normal'] ['core'] ['dashboard_right_now']);
然后使用wp_add_dashboard_widget()添加自己的小部件。
查看WP-ADMIN/INCLAY/DASHBOARD.PHP以获取函数,WP_DASHBOOD_RICHT_NOW()用于添加内容的模型(根据需要删除主题信息。)
functions.php
<?php
/*
Based on Jake Goldman's approach
*/
add_action('wp_dashboard_setup', 'custom_dashboard_widgets');
function custom_dashboard_widgets(){
global $wp_meta_boxes;
unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
wp_add_dashboard_widget('my_dashboard_right_now', 'Right Now', 'right_now_no_theme');
}
function right_now_no_theme() {
global $wp_registered_sidebars;
$num_posts = wp_count_posts( 'post' );
$num_pages = wp_count_posts( 'page' );
$num_cats = wp_count_terms('category');
$num_tags = wp_count_terms('post_tag');
$num_comm = wp_count_comments( );
echo "\n\t".'<div class="table table_content">';
echo "\n\t".'<p class="sub">' . __('Content') . '</p>'."\n\t".'<table>';
echo "\n\t".'<tr class="first">';
// Posts
$num = number_format_i18n( $num_posts->publish );
$text = _n( 'Post', 'Posts', intval($num_posts->publish) );
if ( current_user_can( 'edit_posts' ) ) {
$num = "<a href='edit.php'>$num</a>";
$text = "<a href='edit.php'>$text</a>";
}
echo '<td class="first b b-posts">' . $num . '</td>';
echo '<td class="t posts">' . $text . '</td>';
echo '</tr><tr>';
/* TODO: Show status breakdown on hover
if ( $can_edit_pages && !empty($num_pages->publish) ) { // how many pages is not exposed in feeds. Don't show if !current_user_can
$post_type_texts[] = '<a href="edit-pages.php">'.sprintf( _n( '%s page', '%s pages', $num_pages->publish ), number_format_i18n( $num_pages->publish ) ).'</a>';
}
if ( $can_edit_posts && !empty($num_posts->draft) ) {
$post_type_texts[] = '<a href="edit.php?post_status=draft">'.sprintf( _n( '%s draft', '%s drafts', $num_posts->draft ), number_format_i18n( $num_posts->draft ) ).'</a>';
}
if ( $can_edit_posts && !empty($num_posts->future) ) {
$post_type_texts[] = '<a href="edit.php?post_status=future">'.sprintf( _n( '%s scheduled post', '%s scheduled posts', $num_posts->future ), number_format_i18n( $num_posts->future ) ).'</a>';
}
if ( current_user_can('publish_posts') && !empty($num_posts->pending) ) {
$pending_text = sprintf( _n( 'There is <a href="%1$s">%2$s post</a> pending your review.', 'There are <a href="%1$s">%2$s posts</a> pending your review.', $num_posts->pending ), 'edit.php?post_status=pending', number_format_i18n( $num_posts->pending ) );
} else {
$pending_text = '';
}
*/
// Pages
$num = number_format_i18n( $num_pages->publish );
$text = _n( 'Page', 'Pages', $num_pages->publish );
if ( current_user_can( 'edit_pages' ) ) {
$num = "<a href='edit.php?post_type=page'>$num</a>";
$text = "<a href='edit.php?post_type=page'>$text</a>";
}
echo '<td class="first b b_pages">' . $num . '</td>';
echo '<td class="t pages">' . $text . '</td>';
echo '</tr><tr>';
// Categories
$num = number_format_i18n( $num_cats );
$text = _n( 'Category', 'Categories', $num_cats );
if ( current_user_can( 'manage_categories' ) ) {
$num = "<a href='edit-tags.php?taxonomy=category'>$num</a>";
$text = "<a href='edit-tags.php?taxonomy=category'>$text</a>";
}
echo '<td class="first b b-cats">' . $num . '</td>';
echo '<td class="t cats">' . $text . '</td>';
echo '</tr><tr>';
// Tags
$num = number_format_i18n( $num_tags );
$text = _n( 'Tag', 'Tags', $num_tags );
if ( current_user_can( 'manage_categories' ) ) {
$num = "<a href='edit-tags.php'>$num</a>";
$text = "<a href='edit-tags.php'>$text</a>";
}
echo '<td class="first b b-tags">' . $num . '</td>';
echo '<td class="t tags">' . $text . '</td>';
echo "</tr>";
do_action('right_now_content_table_end');
echo "\n\t</table>\n\t</div>";
echo "\n\t".'<div class="table table_discussion">';
echo "\n\t".'<p class="sub">' . __('Discussion') . '</p>'."\n\t".'<table>';
echo "\n\t".'<tr class="first">';
// Total Comments
$num = '<span class="total-count">' . number_format_i18n($num_comm->total_comments) . '</span>';
$text = _n( 'Comment', 'Comments', $num_comm->total_comments );
if ( current_user_can( 'moderate_comments' ) ) {
$num = '<a href="edit-comments.php">' . $num . '</a>';
$text = '<a href="edit-comments.php">' . $text . '</a>';
}
echo '<td class="b b-comments">' . $num . '</td>';
echo '<td class="last t comments">' . $text . '</td>';
echo '</tr><tr>';
// Approved Comments
$num = '<span class="approved-count">' . number_format_i18n($num_comm->approved) . '</span>';
$text = _nx( 'Approved', 'Approved', $num_comm->approved, 'Right Now' );
if ( current_user_can( 'moderate_comments' ) ) {
$num = "<a href='edit-comments.php?comment_status=approved'>$num</a>";
$text = "<a class='approved' href='edit-comments.php?comment_status=approved'>$text</a>";
}
echo '<td class="b b_approved">' . $num . '</td>';
echo '<td class="last t">' . $text . '</td>';
echo "</tr>\n\t<tr>";
// Pending Comments
$num = '<span class="pending-count">' . number_format_i18n($num_comm->moderated) . '</span>';
$text = _n( 'Pending', 'Pending', $num_comm->moderated );
if ( current_user_can( 'moderate_comments' ) ) {
$num = "<a href='edit-comments.php?comment_status=moderated'>$num</a>";
$text = "<a class='waiting' href='edit-comments.php?comment_status=moderated'>$text</a>";
}
echo '<td class="b b-waiting">' . $num . '</td>';
echo '<td class="last t">' . $text . '</td>';
echo "</tr>\n\t<tr>";
// Spam Comments
$num = number_format_i18n($num_comm->spam);
$text = _nx( 'Spam', 'Spam', $num_comm->spam, 'comment' );
if ( current_user_can( 'moderate_comments' ) ) {
$num = "<a href='edit-comments.php?comment_status=spam'><span class='spam-count'>$num</span></a>";
$text = "<a class='spam' href='edit-comments.php?comment_status=spam'>$text</a>";
}
echo '<td class="b b-spam">' . $num . '</td>';
echo '<td class="last t">' . $text . '</td>';
echo "</tr>";
do_action('right_now_table_end');
do_action('right_now_discussion_table_end');
echo "\n\t</table>\n\t</div>";
echo "\n\t".'<div class="versions">';
$ct = current_theme_info();
echo "\n\t<p>";
/*
if ( !empty($wp_registered_sidebars) ) {
$sidebars_widgets = wp_get_sidebars_widgets();
$num_widgets = 0;
foreach ( (array) $sidebars_widgets as $k => $v ) {
if ( 'wp_inactive_widgets' == $k )
continue;
if ( is_array($v) )
$num_widgets = $num_widgets + count($v);
}
$num = number_format_i18n( $num_widgets );
$switch_themes = $ct->title;
if ( current_user_can( 'switch_themes') ) {
echo '<a href="themes.php" class="button rbutton">' . __('Change Theme') . '</a>';
$switch_themes = '<a href="themes.php">' . $switch_themes . '</a>';
}
if ( current_user_can( 'edit_theme_options' ) ) {
printf(_n('Theme <span class="b">%1$s</span> with <span class="b"><a href="widgets.php">%2$s Widget</a></span>', 'Theme <span class="b">%1$s</span> with <span class="b"><a href="widgets.php">%2$s Widgets</a></span>', $num_widgets), $switch_themes, $num);
} else {
printf(_n('Theme <span class="b">%1$s</span> with <span class="b">%2$s Widget</span>', 'Theme <span class="b">%1$s</span> with <span class="b">%2$s Widgets</span>', $num_widgets), $switch_themes, $num);
}
} else {
if ( current_user_can( 'switch_themes' ) ) {
echo '<a href="themes.php" class="button rbutton">' . __('Change Theme') . '</a>';
printf( __('Theme <span class="b"><a href="themes.php">%1$s</a></span>'), $ct->title );
} else {
printf( __('Theme <span class="b">%1$s</span>'), $ct->title );
}
}
*/
echo '</p>';
update_right_now_message();
echo "\n\t".'<br class="clear" /></div>';
do_action( 'rightnow_end' );
do_action( 'activity_box_end' );
}
?>
替代和非常简单:使用插件 管理, ,该插件有很多选项,您可以添加自己的选项。这个插件是我自己的插件,我将编码一个具有更好代码的新插件。也许有助于CSUTOM解决方案来读取源。