分類
网站

wordpress中functions.php的常用代码

//** *From https://mariushosting.com/how-to-upload-webp-files-on-wordpress/*/
//** *Enable upload for webp image files.*/
//** *支持上傳 webp 格式的圖片文件 */
function webp_upload_mimes($existing_mimes) {
    $existing_mimes['webp'] = 'image/webp';
    return $existing_mimes;
}
add_filter('mime_types', 'webp_upload_mimes');

//** * Enable preview / thumbnail for webp image files.*/
//** *支持預覽 webp 圖片的縮略圖*/
function webp_is_displayable($result, $path) {
    if ($result === false) {
        $displayable_image_types = array( IMAGETYPE_WEBP );
        $info = @getimagesize( $path );

        if (empty($info)) {
            $result = false;
        } elseif (!in_array($info[2], $displayable_image_types)) {
            $result = false;
        } else {
            $result = true;
        }
    }

    return $result;
}
add_filter('file_is_displayable_image', 'webp_is_displayable', 10, 2);

在footer中添加谷歌分析代码:

/*在底部添加谷歌分析代码*/
<?php
function add_googleanalytics() { ?>
<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-********-1', 'yoursite.com');
  ga('send', 'pageview');

</script>
<?php }

add_action('wp_footer', 'add_googleanalytics');?>

添加Google+按钮:

/*此为g+异步js代码*/
<?php
function add_googleplus() { ?>
    <script type="text/javascript">
      window.___gcfg = {
        lang: 'zh-CN'
      };

      (function() {
        var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
        po.src = 'https://apis.google.com/js/plusone.js';
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
      })();
    </script>
<?php }

add_action('wp_footer', 'add_googleplus');?>

上面代码将g+异步js代码置于页底,如需显示g+按钮,还需在文章页添加显示代码,如我使用的Attorney主题,就是在content-single.php这个页面添加如下代码:

<div class="g-plusone" data-size="small" ></div>

详细g+按钮信息,可参考官方指南https://developers.google.com/+/web/+1button/?hl=zh-cn


去除头部的WP版本号

/*去除头部的WP版本号*/
<?php
function no_generators(){
return '';
}
add_filter('the_generator','no_generators');
?>

开启留言回复邮件提醒选项,并自动勾选


<?php
/* 开始邮件回复评论提醒*/
function comment_mail_notify($comment_id) {
  $admin_notify = '1'; // admin 要不要收回复通知 ( '1'=要 ; '0'=不要 )
  $admin_email = get_bloginfo ('admin_email'); // $admin_email 可改为你指定的 e-mail.
  $comment = get_comment($comment_id);
  $comment_author_email = trim($comment->comment_author_email);
  $parent_id = $comment->comment_parent ? $comment->comment_parent : '';
  global $wpdb;
  if ($wpdb->query("Describe {$wpdb->comments} comment_mail_notify") == '')
    $wpdb->query("ALTER TABLE {$wpdb->comments} ADD COLUMN comment_mail_notify TINYINT NOT NULL DEFAULT 0;");
  if (($comment_author_email != $admin_email && isset($_POST['comment_mail_notify'])) || ($comment_author_email == $admin_email && $admin_notify == '1'))
    $wpdb->query("UPDATE {$wpdb->comments} SET comment_mail_notify='1' WHERE comment_ID='$comment_id'");
  $notify = $parent_id ? get_comment($parent_id)->comment_mail_notify : '0';
  $spam_confirmed = $comment->comment_approved;
  if ($parent_id != '' && $spam_confirmed != 'spam' && $notify == '1') {
    $wp_email = 'no-reply@' . preg_replace('#^www.#', '', strtolower($_SERVER['SERVER_NAME'])); // e-mail 发出点, no-reply 可改为可用的 e-mail.
    $to = trim(get_comment($parent_id)->comment_author_email);
    $subject = '您在 [' . get_option("blogname") . '] 的留言有了回复';
    $message = '
    <div style="background-color:#eef2fa; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px;">
      <p>' . trim(get_comment($parent_id)->comment_author) . ', 您好!</p>
      <p>您曾在《' . get_the_title($comment->comment_post_ID) . '》的留言:<br /><span style="background-color:#ffffff; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px;">'
       . trim(get_comment($parent_id)->comment_content) . '</span></p>
      <p>' . trim($comment->comment_author) . ' 给了您回复:<br /><span style="background-color:#ffffff; border:1px solid #d8e3e8; color:#111; padding:0 15px; -moz-border-radius:5px; -webkit-border-radius:5px; -khtml-border-radius:5px;">'
       . trim($comment->comment_content) . '</span><br /></p>
      <p><a href="' . htmlspecialchars(get_comment_link($parent_id, array('type' => 'comment'))) . '">查看回应完整内容</a></p>
      <p>欢迎再度光临 <a href="' . get_option('home') . '">' . get_option('blogname') . '</a></p>
      <p>(此邮件由系统自动发送,别回,回复了我也收不到.)</p>
    </div>';
         $from = "From: "" . get_option('blogname') . "" <$wp_email>";
         $headers = "$fromnContent-Type: text/html; charset=" . get_option('blog_charset') . "n";
         wp_mail( $to, $subject, $message, $headers );
  }
}
add_action('comment_post', 'comment_mail_notify');

/* 自动加勾选栏 */
function add_checkbox() {
  echo '<input type="checkbox" name="comment_mail_notify" id="comment_mail_notify" value="comment_mail_notify" checked="checked" style="margin-left:20px;" /><label for="comment_mail_notify">有人回复时邮件通知我</label>';
}
add_action('comment_form', 'add_checkbox');
?>

本文更新於 2020/06/23。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *