<?php
/*
Plugin Name: 广告管理面板
Plugin URI:
Description: wordpress下广告图片自定义管理。
Author: 安馨软服科技
Version: 0.0.1
Author URI:
Requires PHP: 7.0.0
*/
function ad_management_panel($wp_customize) {
$wp_customize->add_panel('ad_management_panel', array(
'priority' => 200,
'theme_supports' => '',
'title' => '广告面板',
'discription' => 'wordpress下广告图片自定义管理'
));
$num = get_theme_mod('ad_num');
$num = intval($num) > 0 ? $num : 5;
for($i = 0; $i < $num; $i++) {
$wp_customize->add_section('ad_list'.$i, array(
'title' => '广告 '.$i,
'panel' => 'ad_management_panel',
'priority' => 130
));
$wp_customize->add_setting('ad_upload_'.$i, array(
'default' => '',
'transport' => 'refresh',
'sanitize_callback' => 'absint',
'type' => 'theme_mod'
));
$wp_customize->add_control(new WP_Customize_Media_Control($wp_customize, 'ad_upload_'.$i,
array(
'label' => __('文章广告投放'),
'description' => __('位置'.$i),
'section' => 'ad_list'.$i,
'settings' => 'ad_upload_'.$i,
'priority' => 1,
'mime_type' => 'video',
'button_labels' => array(
'select' => '选择图片',
'change' => '更换图片',
'default' => '默认',
'remove' => '删除图片',
'placeholder' => '请选择图片',
'frame_title' => '请选择文件',
'frame_button' => '请选择文件'
)
))
);
$wp_customize->add_setting('ad_poster'.$i, array(
'default' => plugin_dir_url(__FILE__).'images/default.jpg',
'transport' => 'refresh',
'type' => 'theme_mod'
));
// $wp_customize->add_control(new WP_Customize_Cropped_Image_Control('ad_poster'.$i, array(
// 'label' => '请选择图片',
// 'height' => 40,
// 'width' => 70,
// 'flex_height' => true,
// 'flex_width' => true,
// 'section' => 'ad_list'.$i,
// 'settings' => 'ad_poster'.$i,
// 'priority' => 1
// )));
$wp_customize->add_setting('ad_title'.$i, array(
'default' => '',
'type' => 'theme_mod',
));
$wp_customize->add_control(new WP_Customize_Control($wp_customize, 'ad_title'.$i, array(
'label' => '标题',
'section' => 'ad_list'.$i,
'settings' => 'ad_title'.$i,
'priority' => 1,
'type' => 'text',
'placeholder' => '请输入标题'
)));
$wp_customize->add_setting('ad_url'.$i, array(
'default' => '',
'type' => 'theme_mod',
));
$wp_customize->add_control(new WP_Customize_Control($wp_customize, 'ad_url'.$i, array(
'label' => '跳转地址',
'section' => 'ad_list'.$i,
'settings' => 'ad_url'.$i,
'priority' => 1,
'type' => 'text',
'placeholder' => '请输入跳转链接'
)));
}
$wp_customize->add_section('ad_set', array(
'title' => '广告位设置',
'panel' => 'ad_management_panel',
'priority' => 130
));
$wp_customize->add_setting('ad_num', array(
'default' => '',
'type' => 'theme_mod',
));
$wp_customize->add_control(new WP_Customize_Control($wp_customize, 'ad_num', array(
'label' => '广告位数量',
'section' => 'ad_set',
'settings' => 'ad_num',
'priority' => 1,
'type' => 'text',
'placeholder' => '请输入广告位数量'
)));
}
add_action('customize_register', 'ad_management_panel');
add_shortcode('ad_show', 'adSHowList');
function adSHowList() {
$num = get_theme_mod('ad_num');
$ad_data = [];
if(is_numeric($num)) {
for($i = 0; $i < $num; $i++) {
$id = get_theme_mod('ad_upload_'.$i);
if($id) {
$ad_data[] = [
'img' => wp_get_attachment_url($id) ?? '',
'title' => get_theme_mod('ad_title'.$i) ?? '',
'jump' => get_theme_mod('ad_url'.$i) ?? ''
];
}
}
}
return $ad_data;
}
//$id = get_theme_mod('ad_poster'.$i);获取id
//wp_get_attachment_url($id); 获取附件地址