二开时确实挺好用的function,可能会用到哟

各种好用的function,避免重复造轮子

<?php 
/*
get_stylesheet_directory().  获取当前路径
get_stylesheet_directory_uri() .获取当前路径url
get_template_directory(). 获取父级主题路径
get_template_directory_uri() .获取父级主题路径url
*/

//增加链接功能
add_filter('pre_option_link_manager_enabled', '__return_true');

//表格数据转数组的功能
function get_td_array($table) { 
  $table = preg_replace("'<table[^>]*?>'","",$table); 
  $table = preg_replace("'<tr[^>]*?>'","",$table); 
  $table = preg_replace("'<td[^>]*?>'","",$table); 
  $table = str_replace("</tr>","{tr}",$table); 
  $table = str_replace("</td>","{td}",$table); 
  //去掉 HTML 标记  
  $table = preg_replace("'<[/!]*?[^<>]*?>'","",$table); 
  //去掉空白字符  
  $table = preg_replace("'([rn])[s]+'","",$table); 
  $table = str_replace(" ","",$table); 
  $table = str_replace(" ","",$table); 
  $table = explode('{tr}', $table); 
  array_pop($table); 
  foreach ($table as $key=>$tr) { 
    $td = explode('{td}', $tr); 
    array_pop($td); 
    $td_array[] = $td; 
  } 
  return $td_array; 
} 

//引入依赖文件
require_once( get_stylesheet_directory(). '/core/store.php' );
require_once( get_stylesheet_directory(). '/core/woocommerce.php' );
require_once( get_stylesheet_directory(). '/core/notice-carousel.php' );

//修改登录的logo
add_action('login_head', 'custom_loginlogo'); 
function custom_loginlogo() { 
echo '<style type="text/css">h1 a {background-image: url('.get_stylesheet_directory_uri() .'/images/logo_sq.png) !important;}</style>'; 
} 

/** 移除wordpress版本更新提示 */
// add_filter( 'pre_site_transient_update_core', function(){ return "return null";});


//屏蔽 XML-RPC
add_filter('xmlrpc_enabled', '__return_false');
add_filter('xmlrpc_methods', '__return_empty_array');

//引用父主题css
add_action( 'wp_enqueue_scripts', 'zke_ptheme_enqueue_styles', 0 );
function zke_ptheme_enqueue_styles() {
    wp_enqueue_style( 'zke-justnews-style', get_template_directory_uri() . '/css/style.css' );
}


//获取地址中的省市区等信息的功能,在page-stores中调用了get_address()
function get_address($address){
    preg_match('/(.*?(省|自治区|北京市|天津市|上海市|重庆市|澳门特别行政区|香港特别行政区))/', $address, $matches);
    if (count($matches) > 1) {
        $province = $matches[count($matches) - 2];
        $address = preg_replace('/(.*?(省|自治区|北京市|天津市|上海市|重庆市|澳门特别行政区|香港特别行政区))/','', $address, 1);
    }
    preg_match('/(.*?(市|自治州|地区|区划|县))/', $address, $matches);
    if (count($matches) > 1) {
        $city = $matches[count($matches) - 2];
        $address = str_replace($city, '', $address);
    }
    preg_match('/(.*?(区|县|镇|乡|街道))/', $address, $matches);
    if (count($matches) > 1) {
        $area = $matches[count($matches) - 2];
        $address = str_replace($area, '', $address);
    }
    return [
        'province' => isset($province) ? $province : '',
        'city' => isset($city) ? $city : $area,
        'district' => isset($area) ? $area : '',
        "address" => $address
    ];
}



// 修复wpcom主题不兼容wp_get_current_user()->roles[0]为空的问题
add_action('init', 'zke_role_revise');
function zke_role_revise() {
    $user = wp_get_current_user();
    if( !$user->roles[0] ){
         $user->roles  = array_values($user->roles);
        }
}



// WordPress 隐藏加密 wp-login 后台登录地址 -登陆地址为/wp-login.php?[argument]
add_action('login_enqueue_scripts', 'zke_wp_login_protection');
function zke_wp_login_protection(){
    if( !isset($_GET['zke']) ){
        header( 'Location: ' . home_url() );
        exit;
    }
}

//搜外友链功能短代码
add_shortcode('souwailinks', 'zke_souwai_links');
function zke_souwai_links() {
    echo "<script>jQuery(document).ready(function(){ 
            jQuery('#zke-yqlj a').attr('target', '_blank').attr('style', 'color:#DCDCDC;'); 
        }); 
       
        </script>";
    //https://didi.seowhy.com/api/link/auto?uid=xxxx&host=xxxx
    echo '<div id="zke-yqlj" class="list list-links">';
    echo '<a href="https://didi.seowhy.com/index.php?ruid=43666">搜外友链</a>';
         //提示:10分钟更新一次数据
    $didiId = 43666;//搜外友链id 例如:123
    $host = "xxxxxx";//host 例如:didi.seowhy.com
    $url = sprintf("https://didi.seowhy.com/api/link/auto?uid=%d&host=%s",$didiId,$host);
    $url2 = "https://www.seowhy.com/";
    $cacheUrl = "./didi.seowhy.com-cache.txt";
    $cacheBak = "./didi.seowhy.com-cache.bak";
    $mtime = @filemtime($cacheUrl);
             if ($mtime + 600 < time()) {
                $opts = array(
                            "http" => array(
                                            "header"=>array(
                                                             "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0 Seowhy",
                                                            ),
                                            "method"  => "GET",
                                            "timeout" => 2
                                            ),
                        );
                $context = stream_context_create($opts);
                $content = @file_get_contents($url,false, $context);
                if(is_array($http_response_header)){
                        $http_response_header = $http_response_header[0];
                }
                if(strpos($http_response_header,"200 OK") !==false && $content != "error"){
                    @file_put_contents($cacheUrl, $content);
                    echo $content;
                }else{
                      $content = @file_get_contents($cacheBak);
                     echo $content;}
            }else{
                  $content = @file_get_contents($cacheUrl);
                 echo $content;
            }
        echo "</div>";
    }



//屏蔽elementor插件更新提醒
add_filter('site_transient_update_plugins', 'remove_update_notifications');
function remove_update_notifications( $value ) {
if ( isset( $value ) && is_object( $value ) ) {
    unset( $value->response[ 'elementor/elementor.php' ] ); //替换为禁用的插件文件
    unset( $value->response[ 'elementor-pro/elementor-pro.php' ] ); //替换为禁用的插件文件
    unset( $value->response[ 'wp-optimize-premium/wp-optimize.php' ] ); //替换为禁用的插件文件
    unset( $value->response[ 'pw-bulk-edit/pw-bulk-edit.php' ] ); //替换为禁用的插件文件
 }
return $value;
}

//小丸子rest-api 缓存优化
/*
 * Plugin Name: MP API Cache
 * Plugin URI: https://www.imahui.com/minapp/1044.html
 * Description: 基于 WordPress REST Cache 插件扩展 Mini Program API 缓存。需要安装 WP REST Cache 插件。
 * Version: 2.1.0
 * Author: 艾码汇
 * Author URI: https://www.imahui.com/about.html
 * requires at least: 4.9.8
 * tested up to: 5.8.2
*/

add_filter( 'wp_rest_cache/allowed_endpoints', function ( $allowed_endpoints ) {
    if ( ! isset( $allowed_endpoints[ 'mp/v1' ] ) ) {
        $allowed_endpoints[ 'mp/v1' ][] = 'menu';
        $allowed_endpoints[ 'mp/v1' ][] = 'posts';
        $allowed_endpoints[ 'mp/v1' ][] = 'advert';
        $allowed_endpoints[ 'mp/v1' ][] = 'setting';
        $allowed_endpoints[ 'mp/v1' ][] = 'comments';
        $allowed_endpoints[ 'wp/v2' ][] = 'posts';
    }
    return $allowed_endpoints;
}, 10, 1 );


//一个引用本地html并增加缓存的短代码方法
function zke_load_html_file($atts) {
    $atts = shortcode_atts(array(
        'file' => ''
    ), $atts);

    if (empty($atts['file'])) {
        return '';
    }

    // 检查是否存在缓存
    $cache_key = 'html_' . $atts['file'];
    $output = wp_cache_get($cache_key);
    if ($output !== false) {
        return $output;
    }

    ob_start();
    include plugin_dir_path(__FILE__) . 'html/' . $atts['file'] . '.html';
    $output = ob_get_clean();

    // 将结果存入缓存,并设置过期时间为一周
    wp_cache_set($cache_key, $output, 'html_cache', 604800);

    return $output;
}
add_shortcode('load_html', 'zke_load_html_file');

 

本文作者:𝙕𝙆𝘾𝙊𝙄

文章名称:二开时确实挺好用的function,可能会用到哟

文章链接:https://www.zkcoi.com/365up/program/2350.html

本站资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
𝙕𝙆𝘾𝙊𝙄𝙕𝙆𝘾𝙊𝙄
上一篇 2022年10月19日
下一篇 2022年10月19日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

私聊博主

立即联系
一般有空就回复

qrcode_web

微信扫码联系我

分享本页
返回顶部