0% found this document useful (0 votes)
12 views8 pages

functions

The document is a PHP script for a WordPress theme that includes various functionalities such as enabling featured images, registering navigation menus, and creating custom post types. It also includes features for managing categories and tags, disabling Google Fonts, and customizing the comment form. Additionally, it provides pagination, visitor tracking, and modifications to the admin update notifications.

Uploaded by

wristrong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views8 pages

functions

The document is a PHP script for a WordPress theme that includes various functionalities such as enabling featured images, registering navigation menus, and creating custom post types. It also includes features for managing categories and tags, disabling Google Fonts, and customizing the comment form. Additionally, it provides pagination, visitor tracking, and modifications to the admin update notifications.

Uploaded by

wristrong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 8

<?

php

/**
* 开启特色图像功能
*/
function wordpresskt_support_thumbnail()
{
// 声明使用特色图像功能
add_theme_support('post-thumbnails');
}
add_action('after_setup_theme', 'wordpresskt_support_thumbnail');

// 定义一个 php 函数
function wordpresskt_register_menus()
{
// 注册导航,也就是声明要使用导航菜单功能,同时表明要注册几个导航,用不同的位置标识
register_nav_menus(array(
// '位置标记' => '位置标题'
'nav-1' => '顶部导航',
// 可以注册多个菜单
'nav-2' => '底部导航',
));
}
//禁用更新提示
add_action('admin_menu','wp_hide_nag');
function wp_hide_nag() {
remove_action( 'admin_notices', 'update_nag', 3 );
}
// 将自定义函数挂载到 after_setup_theme 钩子下
add_action('after_setup_theme', 'wordpresskt_register_menus');

// 自定义一个 php 函数
function wordpresskt_mytheme_setup()
{
// 声明要使用 wordpress 集成好的页面标题功能
add_theme_support('title-tag');
}
// 将自定义函数,添加到 after_setup_theme 钩子上
add_action('after_setup_theme', 'wordpresskt_mytheme_setup');

/**
* 截取正文
* @param int $len 要截取的字符数量
* @param string $suffix 如果发生截取,添加什么标记
* @return string
*/
function wordpresskt_strim_post_content($len = 100, $suffix = '...')
{

// 获取正文信息,并做必要处理
$content = get_the_content();
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);

// 去除正文中的 HTML 标签
$content = strip_tags($content);

if (mb_strlen($content) <= $len) {


// 字符数量少于要截取的长度,则展示全部
return $content;
} else {
// 截取指定长度的字符
return $content = mb_substr($content, 0, $len) . $suffix;
}
}

class PTCFP
{

function __construct()
{

add_action('init', array($this, 'taxonomies_for_pages'));

/**
* 确保这些查询修改不会作用于管理后台,防止文章和页面混杂
*/
if (!is_admin()) {
add_action('pre_get_posts', array($this, 'category_archives'));
add_action('pre_get_posts', array($this, 'tags_archives'));
} // ! is_admin

} // __construct

/**
* 为“页面”添加“标签”和“分类”
*
* @uses register_taxonomy_for_object_type
*/
function taxonomies_for_pages()
{
register_taxonomy_for_object_type('post_tag', 'page');
register_taxonomy_for_object_type('category', 'page');
} // taxonomies_for_pages

/**
* 在标签存档中包含“页面”
*/
function tags_archives($wp_query)
{

if ($wp_query->get('tag'))
$wp_query->set('post_type', 'any');
} // tags_archives

/**
* 在分类存档中包含“页面”
*/
function category_archives($wp_query)
{

if ($wp_query->get('category_name') || $wp_query->get('cat'))
$wp_query->set('post_type', 'any');
} // category_archives

} // PTCFP
$ptcfp = new PTCFP();

/**
* 注册边栏
*/
// 自定义的一个函数
function wordpresskt_register_sidebar()
{
// 注册一个边栏
register_sidebar(array(
'name' => 'sidebar1',
'id' => 'sidebar-1',
'description' => '',
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
));

// 注册另一个边栏
register_sidebar(array(
'name' => 'sidebar2',
'id' => 'sidebar-2',
'description' => '',
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
));
}
// 将自定义函数挂载到 widgets_init 中
add_action('widgets_init', 'wordpresskt_register_sidebar');

class Disable_Google_Fonts
{
public function __construct()
{
add_filter('gettext_with_context', array($this, 'disable_open_sans'), 888, 4);
}
public function disable_open_sans($translations, $text, $context, $domain)
{
if ('Open Sans font: on or off' == $context && 'on' == $text) {
$translations = 'off';
}
return $translations;
}
}
$disable_google_fonts = new Disable_Google_Fonts;
//分页
function mo_paging()
{
$p = 3;
if (is_singular()) return;
global $wp_query, $paged;
$max_page = $wp_query->max_num_pages;
if ($max_page == 1) return;
echo '';
if (empty($paged)) $paged = 1;
//echo '<a class="prev">';
previous_posts_link('«');
//echo '</a>';
// if ($paged > $p + 1) _paging_link(1, '<li>第一页</li>');
if ($paged > $p + 2) echo "";
for ($i = $paged - $p; $i <= $paged + $p; $i++) {
if ($i > 0 && $i <= $max_page) $i == $paged ? print "<span
class='thisclass'>{$i}</span>" : _paging_link($i);
}
if ($paged < $max_page - $p - 1) echo "";
//echo '<a class="next">';
next_posts_link('»');
//echo '</a>';
// echo '<li><span>共 ' . $max_page . ' 页</span></li>';
echo '';
}

function _paging_link($i, $title = '')


{
if ($title == '') $title = "第 {$i} 页";
echo "<a href='", esc_html(get_pagenum_link($i)), "'>{$i}</a>";
}

//获取根分类 id
function get_category_root_id($cat)
{
$this_category = get_category($cat); // 取得当前分类
while ($this_category->category_parent) // 若当前分类有上级分类时,循环
{
$this_category = get_category($this_category->category_parent); // 将当前分类设为上级分类
(往上爬)
}
return $this_category->term_id; // 返回根分类的 id 号
}

//如何获得 WordPress 文章浏览次数的统计


function record_visitors()
{
if (is_singular())
{
global $post;
$post_ID = $post->ID;
if($post_ID)
{
$post_views = (int)get_post_meta($post_ID, 'views', true);
if(!update_post_meta($post_ID, 'views', ($post_views+1)))
{
add_post_meta($post_ID, 'views', 1, true);
}
}
}
}
add_action('wp_head', 'record_visitors');
/// 函数名称:post_views
/// 函数作用:取得文章的阅读次数
function post_views($before = '(点击 ', $after = ' 次)', $echo = 1)
{
global $post;
$post_ID = $post->ID;
$views = (int)get_post_meta($post_ID, 'views', true);
if ($echo) echo $before, number_format($views), $after;
else return $views;
}

//修改后台显示更新的代码

add_filter('pre_site_transient_update_core', create_function('$a', "return


null;")); // 关闭核心提示

add_filter('pre_site_transient_update_plugins', create_function('$a', "return


null;")); // 关闭插件提示

add_filter('pre_site_transient_update_themes', create_function('$a', "return


null;")); // 关闭主题提示

remove_action('admin_init', '_maybe_update_plugins'); // 禁止 WordPress 更新插件

remove_action('admin_init', '_maybe_update_core'); // 禁止 WordPress 检查更新

remove_action('admin_init', '_maybe_update_themes'); // 禁止 WordPress 更新主题

/*
Plugin Name: WP No Category Base
Plugin URI: http://blinger.org/wordpress-plugins/no-category-base/
Description: Removes '/category' from your category permalinks.
Version: 1.1.1
Author: iDope
Author URI: http://efextra.com/
*/

// Refresh rules on activation/deactivation/category changes


register_activation_hook(__FILE__, 'no_category_base_refresh_rules');
add_action('created_category', 'no_category_base_refresh_rules');
add_action('edited_category', 'no_category_base_refresh_rules');
add_action('delete_category', 'no_category_base_refresh_rules');
function no_category_base_refresh_rules() {
global $wp_rewrite;
$wp_rewrite -> flush_rules();
}

register_deactivation_hook(__FILE__, 'no_category_base_deactivate');
function no_category_base_deactivate() {
remove_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
// We don't want to insert our custom rules again
no_category_base_refresh_rules();
}

// Remove category base


add_action('init', 'no_category_base_permastruct');
function no_category_base_permastruct() {
global $wp_rewrite, $wp_version;
if (version_compare($wp_version, '3.4', '<')) {
// For pre-3.4 support
$wp_rewrite -> extra_permastructs['category'][0] = '%category%';
} else {
$wp_rewrite -> extra_permastructs['category']['struct'] = '%category%';
}
}

// Add our custom category rewrite rules


add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
function no_category_base_rewrite_rules($category_rewrite) {
//var_dump($category_rewrite); // For Debugging

$category_rewrite = array();
$categories = get_categories(array('hide_empty' => false));
foreach ($categories as $category) {
$category_nicename = $category -> slug;
if ($category -> parent == $category -> cat_ID)// recursive recursion
$category -> parent = 0;
elseif ($category -> parent != 0)
$category_nicename = get_category_parents($category -> parent,
false, '/', true) . $category_nicename;
$category_rewrite['(' . $category_nicename . ')/(?:feed/)?(feed|rdf|
rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
$category_rewrite['(' . $category_nicename . ')/page/?([0-9]{1,})/?$']
= 'index.php?category_name=$matches[1]&paged=$matches[2]';
$category_rewrite['(' . $category_nicename . ')/?$'] = 'index.php?
category_name=$matches[1]';
}
// Redirect support from Old Category Base
global $wp_rewrite;
$old_category_base = get_option('category_base') ?
get_option('category_base') : 'category';
$old_category_base = trim($old_category_base, '/');
$category_rewrite[$old_category_base . '/(.*)$'] = 'index.php?
category_redirect=$matches[1]';

//var_dump($category_rewrite); // For Debugging


return $category_rewrite;
}

// For Debugging
//add_filter('rewrite_rules_array', 'no_category_base_rewrite_rules_array');
//function no_category_base_rewrite_rules_array($category_rewrite) {
// var_dump($category_rewrite); // For Debugging
//}

// Add 'category_redirect' query variable


add_filter('query_vars', 'no_category_base_query_vars');
function no_category_base_query_vars($public_query_vars) {
$public_query_vars[] = 'category_redirect';
return $public_query_vars;
}

// Redirect if 'category_redirect' is set


add_filter('request', 'no_category_base_request');
function no_category_base_request($query_vars) {
//print_r($query_vars); // For Debugging
if (isset($query_vars['category_redirect'])) {
$catlink = trailingslashit(get_option('home')) .
user_trailingslashit($query_vars['category_redirect'], 'category');
status_header(301);
header("Location: $catlink");
exit();
}
return $query_vars;
}

// applied when calling get_tag_link()


add_filter('tag_link', 'ludou_html_tag_link', 10, 2);

/**
* tag 页链接转换,将 /tag/tag-name/ 转成 /tag/tag-name.html
*/
function ludou_html_tag_link($tag_link, $tag_id) {
return rtrim($tag_link, '/');
}

//禁用古腾堡
add_filter('use_block_editor_for_post', '__return_false');
// Custom comment walker.
require get_template_directory() . '/classes/class-twentytwenty-walker-
comment.php';

//移除网址表单
function url_filtered($fields){
if(isset($fields['url']))
unset($fields['url']);
return $fields;
}
add_filter('comment_form_default_fields', 'url_filtered');

// 移除邮箱地址
add_filter('comment_form_defaults','remove_email');
function remove_email($comment_form_html_arr){
//删除 email 文本框
unset($comment_form_html_arr['fields']['email']);
//修改评论提醒内容,去掉电子邮件相关的提醒。
$comment_form_html_arr['comment_notes_before'] = '<p class="comment-
notes">'.sprintf( ' ' . __( 'Required fields are marked %s' ), '<span
class="required">*</span>' ).'</p >';
return $comment_form_html_arr;
}

//增加英文字体,修改字体大小
function MBT_add_editor_buttons($buttons) {
$buttons[] = ‘fontselect’;
$buttons[] = ‘fontsizeselect’;
$buttons[] = ‘cleanup’;
$buttons[] = ‘styleselect’;
$buttons[] = ‘del’;
$buttons[] = ‘sub’;
$buttons[] = ‘sup’;
$buttons[] = ‘copy’;
$buttons[] = ‘paste’;
$buttons[] = ‘cut’;
$buttons[] = ‘image’;
$buttons[] = ‘anchor’;
$buttons[] = ‘backcolor’;
$buttons[] = ‘wp_page’;
$buttons[] = ‘charmap’;
return $buttons;
}
add_filter(“mce_buttons_2”, “MBT_add_editor_buttons”);

You might also like