面向开发人员的 20 多个 WordPress 黑客

已发表: 2017-08-17

WordPress 今天真正流行的一件事是有机会以一百万种方式扩展它。 例如,可以使用插件增强功能,而主题非常适合更改外观。 结果,该网站得到了调整,以满足博客、电子商务和其他方面的各种需求。

但是,如何通过调整来释放 WordPress 的真正力量呢? 让我们通过这些杀手锏进一步定制。

#1。 带有特色图片的 RSS 提要

RSS Feed with Featured Images

你有没有想过为什么 WordPress 默认不允许在 RSS 提要中显示特色图片? 如果您是博主,或者您只是想为人们订阅提供更多鼓励,这显然是一个更好的举措。

操作方法如下(将其添加到主题的 functions.php 文件中):

add_filter('the_content_feed', 'rss_post_thumbnail');
function rss_post_thumbnail($content) {
global $post;
if( has_post_thumbnail($post->ID) )
$content = '<p>' . get_the_post_thumbnail($post->ID, 'thumbnail') . '</p>' . $content;
return $content;
}

#2。 添加“发送到 Facebook”按钮

Send to Facebook

博主将 Facebook 视为潜在客户和访问者的庞大数据库。 没错,拥有超过 10 亿用户,它确实可以增加流量。 那么,如何创建“发送到 Facebook”按钮以添加到博客并增加流量?

这是通过在当前主题中打开 single.php 文件并将此代码粘贴到循环中来完成的:

<a href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&t=<?php the_title(); ?>" target="blank">Share on Facebook</a>

完毕!

#3。 消除图像压缩

Eliminate image compression

我们都知道 WordPress 不能提供 100% 质量的图像,因为它将它们压缩到 90%。 虽然这对于很多人来说听起来可能不需要改变,但我们开发人员知道我们可以做得更好。

例如,我们可以强制平台显示 100% 的原始图像,以确保完美的质量。 需要在当前主题的functions.php文件中添加以下内容:

add_filter( 'jpg_quality', 'high_jpg_quality' );
function high_jpg_quality() {
return 100;
}

#4。 使用 URL 引用站点

Reference a site with URL

通过使用 WordPress,可以添加站点的快捷方式作为参考。 因此,无需每次都输入 URL。 这是如何完成的:

<?php bloginfo('url'); ?>

然后像这样使用该函数:

<a href="<?php bloginfo('url'); >/about">About Our Company</a>

#5。 通过删除公开显示的 WordPress 版本来提高安全性

WordPress version

对于黑客来说,知道 WordPress 版本就足以尝试利用一些安全漏洞(尤其是在旧版本中:巴拿马文件泄露被归咎于旧安装!)。 要向任何人隐藏此信息,您可以使用以下 hack:

<?php
// Remove the WP version for extra WordPress Security
function remove_wp_version(){ 
return ''; 
} 
add_filter('the_generator', 'remove_wp_version'); 
?>

#6。 限制可以注册的人数

如果您注意到许多新的 WordPress 用户一直在访问用户页面,这意味着您可能允许任何人注册。 当您检查设置时,您会发现用户正在通过您的 RSS 订阅。

如果您不想要太多注册用户,请转到设置并取消选中允许每个人使用会员选项注册的框。 而已!

#7。 通过禁用评论中的 HTML 来防止垃圾邮件

Disabling HTML in comments

对于许多使用 WordPress 的人来说,垃圾邮件是一个真正的问题。 例如,博主经常在评论部分发现指向可疑网站的链接。 带有垃圾邮件的帖子不是引起注意的好方法,因此需要尽快消​​除问题。

要禁用 HTML 并防止垃圾邮件发送者包含链接和其他方法,需要将此代码添加到 functions.php 文件中:

// This will occur when the comment is posted
function plc_comment_post( $incoming_comment ) {
    // convert everything in a comment to display literally
    $incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
    // the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
    $incoming_comment['comment_content'] = str_replace( "'", '&apos;', $incoming_comment['comment_content'] );
    return( $incoming_comment );
}
// This will occur before a comment is displayed
function plc_comment_display( $comment_to_display ) {
    // Put the single quotes back in
    $comment_to_display = str_replace( '&apos;', "'", $comment_to_display );
    return $comment_to_display;
}
add_filter( 'preprocess_comment', 'plc_comment_post', '', 1 );
add_filter( 'comment_text', 'plc_comment_display', '', 1 );
add_filter( 'comment_text_rss', 'plc_comment_display', '', 1 );
add_filter( 'comment_excerpt', 'plc_comment_display', '', 1 );
// This stops WordPress from trying to automatically make hyperlinks on text:
remove_filter( 'comment_text', 'make_clickable', 9 );

#8。 使用电子邮件作为登录

这种非常简单的黑客攻击对于提高安全性非常有帮助。 对于黑客来说,预测电子邮件要比预测用户名困难得多,因此它是一种更安全的方法。

#9。 在主页上显示最近的帖子

Display recent posts on homepage

对于允许在主页上显示最新博客条目的博客作者来说,这是一个很棒的技巧。 无需将博客设为主页! 仅应使用以下代码:

<?php query_posts($query_string . '&showposts=5' ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="story">
<div class="story-content">
<h4><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<?php the_excerpt(); ?>
<?php endwhile; endif; ?>

#10。 更改管理员徽标

Change the admin logo

如上所述,WordPress 最大的功能之一就是自定义。 本节属于这一类。 以下 hack 允许使用客户的徽标而不是默认的管理员徽标。 你不需要插件或任何东西,只需将以下代码插入functions.php:

function custom_admin_logo() {
  echo '<style type="text/css">
          #header-logo { background-image: url('.get_bloginfo('template_directory').'/images/admin_logo.png) !important; }
        </style>';
}
add_action('admin_head', 'custom_admin_logo');

#11。 删除登录页面的错误提示

这是另一个安全黑客,它从登录页面中删除错误消息,从而防止黑客对不正确的登录名或密码发出警报。 插入此代码:

add_filter('login_errors',create_function('$a', "return null;"));

#12。 设置默认 HTML 编辑器

Set default HTML editor

我们列表中的下一个技巧让 WordPress 用户决定是使用可视化编辑器还是 HTML 编辑器。 如果您更喜欢特定的,请使用以下代码(将它们插入到 functions.php 中):

# HTML Editor as default
add_filter( 'ks29so_default_editor', create_function('', 'return "html";') );

# Visual Editor as default
add_filter( 'ks29so_default_editor', create_function('', 'return "tinymce";') );

#13。 更改破折号上的页脚文本

对于希望通过在破折号页脚上添加一些文本来让客户惊喜的开发人员来说,这个 hack 可能很有用。 只需将以下内容插入到 functions.php 文件中:

function remove_footer_admin () {
  echo "Your own text";
} 

add_filter('admin_footer_text', 'remove_footer_admin');

#14。 向小部件添加简码

Add a shortcode to widget

您可以通过将以下代码添加到 functions.php 中来教 WordPress 使用有用的短代码:

add_filter('widget_text', 'do_shortcode');

#15。 自定义破折号徽标

再一次让开发人员为他们的客户定制网站。 要个性化安装,您可以使用以下命令将徽标添加到破折号:

add_action('admin_head', 'custom_logo');

function custom_logo() {
echo '

<style type="text/css"><!--
#header-logo { background-image: url('.get_bloginfo('template_directory').'/images/custom-logo.gif) !important; }
--></style>';
}

#16。 列出即将发布的帖子

此技巧适用于希望将即将发布的帖子安排在访问者可见的列表中的博主。 将以下代码复制并粘贴到您的主题中的任何位置:

<div id="zukunft">
  <div id="zukunft_header"><p>Future events</p></div>

  <?php query_posts('showposts=10&post_status=future'); ?>
  <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div >
      <p class><b><?php the_title(); ?></b><?php edit_post_link('e',' (',')'); ?><br />

      <span class="datetime"><?php the_time('j. F Y'); ?></span></p>
    </div>
  <?php endwhile; else: ?><p>No future events scheduled.</p><?php endif; ?>

</div>

#17。 正确的网址

重要的是您的网站地址正确。 esc_url() 函数允许它保持无错误:

$my_url = 'http://myawesomesite.com/?awesome=true';
$url = esc_url( $my_url );

#18:减少后期修订

Reduce post revisions

WordPress 对存储在数据库中的后期修订没有默认限制。 随着时间的推移,他们的数量会急剧增加(这对网站没有任何好处)。 将以下代码添加到 wp-config.php 文件以设置限制:

define( 'WP_POST_REVISIONS', 3 );

该示例将限制设置为 3。

#19:禁用修订存储

Disable storage of revisions

这一篇延续了后期修订的话题。 如果要禁用它们,请使用以下代码:

define( 'WP_POST_REVISIONS', -1 );

#20。 更改摘录的长度

在某些情况下,标准摘录可能不是最适合布局的。 改变它们真的很容易。 打开functions.php并插入(“20”是长度的值):

function custom_excerpt_length( $length ) {
  return 20;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

#21。 添加 Twitter 样式的“时间前”日期

Time ago dates

一些开发人员不知道 WordPress 可以使用“Time Ago”格式显示日期。 这可以通过在循环中的任何位置粘贴以下代码来完成:

Posted <?php echo human_time_diff(get_the_time('U'), current_time('timestamp')) . ' ago';

是时候破解了!

准备好通过这些简单但方便的技巧来释放 WordPress 的力量了吗? 通过尝试它们,您可以发现网站的新功能并扩展其功能以使其成为功能更强大的环境。

一旦您看到了这些黑客的好处,您就可以使您的内容管理工作更加有效。 此外,其中一些确实可以增强您网站的安全性,这一点尤为重要。

所有这一切——无需安装单个插件! 鉴于 WordPress 网站依赖于安装的许多插件,这些改进对于保持快速加载速度非常有帮助。

希望这篇文章对您找到一些新的 WordPress 黑客并增强您的网站有所帮助。