Wordpress
添加小部件
1class Custom_My_Widge extends WP_Widget {
2
3 public function __construct() {
4 parent::__construct(
5 'custom_widget_id',
6 __( '我的小部件', 'my_widget' ),
7 );
8 }
9
10 public function widget( $args, $instance ) {
11 // Before widget tag
12 echo $args['before_widget'];
13
14 //写你的代码
15
16 // After widget tag
17 echo $args['after_widget'];
18 }
19
20}
21
22add_action( 'widgets_init', function() {
23 // 上面类名
24 register_widget( 'Custom_My_Widge' );
25});
添加管理员菜单
1//主菜单
2function wporg_options_page_html() {
3 ?>
4 <div class="wrap">
5 <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
6 <form action="options.php" method="post">
7 <?php
8 // output security fields for the registered setting "wporg_options"
9 settings_fields( 'wporg_options' );
10 // output setting sections and their fields
11 // (sections are registered for "wporg", each field is registered to a specific section)
12 do_settings_sections( 'wporg' );
13 // output save settings button
14 submit_button( __( 'Save Settings', 'textdomain' ) );
15 ?>
16 </form>
17 </div>
18 <?php
19}
20add_action('admin_menu', function () {
21 add_menu_page(
22 'WP Developer', // 页面内标题
23 'WP Developer', // 侧边栏名称
24 'manage_options', // 菜单所需的功能
25 'wp_developer_slug', // id (slug)
26 'wporg_options_page_html', // 页面的内容
27 plugin_dir_url(__FILE__) . 'images/icon.svg', // 图标
28 20 // 菜单顺序中的位置
29 );
30});
31
32//子菜单
33function wporg_options_page_html2() {
34 // check user capabilities
35 if ( ! current_user_can( 'manage_options' ) ) {
36 return;
37 }
38 ?>
39 <div class="wrap">
40 <h1><?php echo esc_html( get_admin_page_title() ); ?></h1>
41 <form action="options.php" method="post">
42 <?php
43 // output security fields for the registered setting "wporg_options"
44 settings_fields( 'wporg_options' );
45 // output setting sections and their fields
46 // (sections are registered for "wporg", each field is registered to a specific section)
47 do_settings_sections( 'wporg' );
48 // output save settings button
49 submit_button( __( 'Save Settings', 'textdomain' ) );
50 ?>
51 </form>
52 </div>
53 <?php
54}
55add_action('admin_menu', function () {
56 add_submenu_page(
57 'wp_developer_slug',
58 '添加',
59 '添加页面',
60 'manage_options',
61 'wp_developer_add_slug',
62 'wporg_options_page_html2'
63 );
64});
使用第三方smtp邮件服务
1function mail_smtp( $phpmailer ) {
2 $phpmailer->FromName = '我的网站名'; //发件人名称
3 $phpmailer->Host = 'smtp.qq.com'; //修改为你使用的邮箱SMTP服务器
4 $phpmailer->Port = 465; //SMTP端口
5 $phpmailer->Username = 'xxx@qq.com'; //邮箱账户
6 $phpmailer->Password = 'xxx'; //邮箱授权码(此处填写QQ邮箱生成的授权码)
7 $phpmailer->From = 'xxx@qq.com'; //邮箱账户
8 $phpmailer->SMTPAuth = true;
9 $phpmailer->SMTPSecure = 'ssl'; //tls or ssl (port=25时->留空,465时->ssl)
10 $phpmailer->IsSMTP();
11}
12add_action('phpmailer_init', 'mail_smtp');
示例:
数据库查询统计
1echo sprintf( '本页生成数据库 %d 次查询,耗时 %.3f 秒,使用 %.2fMB 内存',get_num_queries(),timer_stop( 0, 3 ),memory_get_peak_usage() / 1024 / 1024);
激活 WordPress 维护模式
1function wp_maintenance_mode () {
2 if ( ! current_user_can ( 'edit_themes' )
3 || ! is_user_logged_in ()) {
4 wp_die ( '<h1> Maintenance </h1> <br />
5 该站点正在定期维护中。
6 请稍后再回来查看。'
7 ) ;
8 }
9}
10add_action ( 'get_header' , 'wp_maintenance_mode' );
添加插件
在 wp-content/plugins/
创建文件夹,放入同名 php
文件。
文件头处放入以下备注:
1<?php
2/*
3Plugin Name: WP Developer
4Plugin URI: http://www.example.com
5Description: This is my awesome plugin.
6Version: 1.0
7Author: Example
8Author URI: http://www.example.com
9Text Domain: wp-fastest-Developer
10*/
自定义登陆页面
1<?php
2/**
3 * 美化Wordpress登录页 By 一为
4 * 原文地址:https://www.iowen.cn/chundaimameihuawordpressmorendengluye/
5 */
6function io_login_header(){
7 echo '<div class="login-container">
8 <div class="login-body">
9 <div class="login-img shadow-lg position-relative flex-fill">
10 <div class="img-bg position-absolute">
11 <div class="login-info">
12 <h2>'. get_bloginfo('name') .'</h2>
13 <p>'. get_bloginfo('description') .'</p>
14 </div>
15 </div>
16 </div>';
17}
18
19function io_login_footer(){
20 echo '</div><!--login-body END-->
21 </div><!--login-container END-->
22 <div class="footer-copyright position-absolute">
23 <span>Copyright © <a href="'. esc_url(home_url()) .'" class="text-white-50" title="'. get_bloginfo('name') .'" rel="home">'. get_bloginfo('name') .'</a></span>
24 </div>';
25}
26add_action('login_header', 'io_login_header');
27add_action('login_footer', 'io_login_footer');
28
29
30//登录页面的LOGO链接为首页链接
31add_filter('login_headerurl',function() {return esc_url(home_url());});
32
33function custom_login_style(){
34 $login_color =['color-l'=>'red','color-r'=>'blue'];
35 $ico='/favicon.ico';
36 $img='/wp-content/uploads/2024/01/课室低码率.jpg';
37 echo '<style type="text/css">
38 body{background:'.$login_color['color-l'].';background:-o-linear-gradient(45deg,'.$login_color['color-l'].','.$login_color['color-r'].');background:linear-gradient(45deg,'.$login_color['color-l'].','.$login_color['color-r'].');height:100vh}
39 .login h1 a{background-image:url('.$ico.');width:180px;background-position:center center;}
40 .login-container{position:relative;display:flex;align-items:center;justify-content:center;height:100vh}
41 .login-body{position:relative;display:flex;margin:0 1.5rem}
42 .login-img{display:none}
43 .img-bg{color:#fff;padding:2rem;bottom:-2rem;left:0;top:-2rem;right:0;border-radius:10px;background-image:url('.$img.');background-repeat:no-repeat;background-position:center center;background-size:cover}
44 .img-bg h2{font-size:2rem;margin-bottom:1.25rem}
45 #login{position:relative;background:#fff;border-radius:10px;padding:28px;width:280px;box-shadow:0 1rem 3rem rgba(0,0,0,.175)}
46 .flex-fill{flex:1 1 auto}
47 .position-relative{position:relative}
48 .position-absolute{position:absolute}
49 .shadow-lg{box-shadow:0 1rem 3rem rgba(0,0,0,.175)!important}
50 .footer-copyright{bottom:0;color:rgba(255,255,255,.6);text-align:center;margin:20px;left:0;right:0}
51 .footer-copyright a{color:rgba(255,255,255,.6);text-decoration:none}
52 #login form{-webkit-box-shadow:none;-moz-box-shadow:none;box-shadow:none;border-width:0;padding:0}
53 #login form .forgetmenot{float:none}
54 .login #login_error,.login .message,.login .success{border-left-color:#40b9f1;box-shadow:none;background:#d4eeff;border-radius:6px;color:#2e73b7}
55 .login #login_error{border-left-color:#f1404b;background:#ffd4d6;color:#b72e37}
56 #login form p.submit{padding:20px 0 0}
57 #login form p.submit .button-primary{float:none;background-color:#f1404b;font-weight:bold;color:#fff;width:100%;height:40px;border-width:0;text-shadow:none!important;border-color:none;transition:.5s}
58 #login form input{box-shadow:none!important;outline:none!important}
59 #login form p.submit .button-primary:hover{background-color:#444}
60 .login #backtoblog,.login #nav{padding:0}
61 @media screen and (min-width:768px){.login-body{width:1200px}
62 .login-img{display:block}
63 #login{margin-left:-60px;padding:40px}
64 }
65</style>';
66}
67add_action('login_head', 'custom_login_style');
68
69//关闭多语言切换按钮
70add_filter( 'login_display_language_dropdown', '__return_false' );
浏览量:加载中... 评论数:加载中...