九零不老心
发布于 2019-04-12 / 11 阅读 / 0 评论 / 0 点赞

wordpress常用自定义functions合集

  1. // 修改网站底部版权和其他信息
    function travelify_footer_info()
    {
    echo '<div class="copyright">' . __('Copyright ©', 'travelify') . ' ' . date('Y') . ' ' . travelify_site_link() . '. </div>';
    }
    function travelify_footer_rightinfo()
    {
    echo '<div class="footer-right">';
    echo "";
    echo '</div>';
    }
  2. // 添加分享代码 这个还需要额外的js支持
    function add_after_post_content($content)
    {
    if (!is_feed() && !is_home() && is_singular() && is_main_query()) {
    $content .= "<div class=\"bdsharebuttonbox\"><a href=\"#\" class=\"bds_weixin\" data-cmd=\"weixin\" title=\"分享到微信\"></a><a href=\"#\" class=\"bds_tsina\" data-cmd=\"tsina\" title=\"分享到新浪微博\"></a><a href=\"#\" class=\"bds_qzone\" data-cmd=\"qzone\" title=\"分享到QQ空间\"></a><a href=\"#\" class=\"bds_renren\" data-cmd=\"renren\" title=\"分享到人人网\"></a></div>" . "<script>window._bd_share_config={\"common\":{\"bdSnsKey\":{},\"bdText\":\"\",\"bdMini\":\"2\",\"bdMiniList\":false,\"bdPic\":\"\",\"bdStyle\":\"1\",\"bdSize\":\"24\"},\"share\":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>";
    }
    return $content;
    }
    add_filter('the_content', 'add_after_post_content');
  3. // Disable author archive and redirect home page
    function my_custom_disable_author_page()
    {
    global $wp_query;
    if (is_author()) {
    $wp_query->set_404();
    status_header(404);
    // Redirect to homepage
    // wp_redirect(get_option('home'));
    }
    }
    add_action('template_redirect', 'my_custom_disable_author_page');
  4. // 关闭评论作者的url链接
    function filter_get_comment_author_url($url, $id, $commentr)
    {
    return "";
    }
    add_filter('get_comment_author_url', 'filter_get_comment_author_url', 10, 3);
  5. // Disable rss feeds
    function disable_feed()
    {
    // wp_die(__('No feed available, please visit the <a href="' . esc_url(home_url('/')) . '">homepage</a>!'));
    wp_redirect(home_url(), 301);
    }
    add_action('do_feed', 'disable_feed', 1);
    add_action('do_feed_rdf', 'disable_feed', 1);
    add_action('do_feed_rss', 'disable_feed', 1);
    add_action('do_feed_rss2', 'disable_feed', 1);
    add_action('do_feed_atom', 'disable_feed', 1);
    add_action('do_feed_rss2_comments', 'disable_feed', 1);
    add_action('do_feed_atom_comments', 'disable_feed', 1);
    remove_action('wp_head', 'feed_links_extra', 3);
    remove_action('wp_head', 'feed_links', 2);
  6. // 注册支持中文
    function chinese_sanitize_user($username, $raw_username, $strict)
    {
    $username = wp_strip_all_tags($raw_username);
    $username = remove_accents($username);$username = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '', $username);
    $username = preg_replace('/&.+?;/', '', $username);
    if ($strict) {
    $username = preg_replace('|[^a-z\p{Han}0-9 _.\-@]|iu', '', $username);
    }
    $username = trim($username);$username = preg_replace('|\s+|', ' ', $username);
    return $username;
    }
    add_filter('sanitize_user', 'chinese_sanitize_user', 10, 3);
  7. // 关闭文章作者的url链接
    function travelify_posted_on()
    {
    $time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
    if (get_the_time('U') !== get_the_modified_time('U')) {
    $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
    }
    $time_string = sprintf(
    $time_string,
    esc_attr(get_the_date('c')),
    esc_html(get_the_date()),
    esc_attr(get_the_modified_date('c')),
    esc_html(get_the_modified_date())
    );
    $byline = sprintf(
    '<span class="author vcard">' . esc_html(get_the_author()) . '</span>'
    );
    echo '<span class="byline"> ' . $byline . '</span><span class="posted-on">' . '<a href="' . esc_url(get_permalink()) . '" rel="bookmark">' . $time_string . '</a>' . '</span>';
    }
  8. // 删除<meta name="generator" content="WordPress"
    remove_action('wp_head', 'wp_generator');
  9. // 自动关键词与描述
    function utf8Substr($str, $from, $len)
    {
    return preg_replace('#^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $from . '}' .
    '((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,' . $len . '}).*#s',
    '$1', $str);
    }
    function meta_SEO()
    {
    global $post;
    $output = '';
    if (is_single()) { //如果是文章页
    $keywords = '';
    $description = '';
    if ($post->post_excerpt) { //如果文章摘要存在就以文章摘要为描述
    $description = $post->post_excerpt;
    $description = str_replace("\r\n", "", $description);
    $description = str_replace("\n", "", $description);
    $description = str_replace("\"", "'", $description);
    $description .= '...';
    } else { //如果文章摘要不存在就截断文章前200字为描述
    $description = utf8Substr(strip_tags($post->post_content), 0, 200);
    $description = str_replace("\r\n", "", $description);
    $description = str_replace("\n", "", $description);
    $description = str_replace("\"", "'", $description);
    $description .= '...';
    }
    $tags = wp_get_post_tags($post->ID); //取文章标签
    foreach ($tags as $tag) {
    $keywordarray[] = $tag->name;
    }
    // 以文章标签为关键字
    $keywords = implode(',', array_unique((array) $keywordarray));
    } else if (is_page()) {
    $keywords = get_the_title($post->post_page);
    $description = utf8Substr(strip_tags($post->post_content), 0, 200);
    } else if (is_category()) {
    $keywords = single_cat_title('', false);
    $description = single_cat_title('', false);
    } else if (is_tag()) {
    $keywords = single_tag_title('', false);
    $description = single_tag_title('', false);
    } else { //如果不是文章页、分类页
    $keywords = 'IT技术,linux,windows,编程语言'; //在引号间写入你博客的关键字用,断开
    $description = '一个分享IT教程经验的站点,涉及windows和linux操作系统下的常用技术和软件使用的内容分享,包含shell编程、python编程、web架构搭建、开源框架、编程工具的使用、数据安全、运维经验分享等内容。'; //在引号间写入你博客的简单描述,不要过200字
    }
    //输出关键字
    $output .= '<meta name="keywords" content="' . $keywords . '" />' . "\n";
    $output .= '<meta name="description" content="' . $description . '" />' . "\n";
    //输出描述
    echo "$output";
    }
    add_action('wp_head', 'meta_SEO');
  10. // 所有url 新窗口打开
    function autoblank($text)
    {
    $return = str_replace('<a', '<a target="_blank"', $text);
    return $return;
    }
    add_filter('the_content', 'autoblank');
  11. //页面链接添加html后缀,还需固定链接重新保存下生效
    function html_page_permalink()
    {
    global $wp_rewrite;
    if (!strpos($wp_rewrite->get_page_permastruct(), '.html')) {
    $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
    }
    }
    add_action('init', 'html_page_permalink', -1);