Search
Close this search box.

wordpress 上传图片,删除图片,修改图片,后台列表自定义,自定义分类法和字段

<?php

/*
Plugin Name: se-frame-activity
Plugin URI: Http://shopeo.cn
Description: Smoant
Author: SHOPEO
Version: 1.0
Author URI: http://www.cnblogs.com/fxmbz
*/


/*****************创建新的文章类型 */
//注册自定义文章类型-activity
/**
 * Register a custom post type called "activity".
 *
 * @see get_post_type_labels() for label keys.
 */
add_action('init', 'my_custom_post_activity');

function my_custom_post_activity()
{
	$labels = array(
		'name' => _x('Activity', 'post type 名称'),
		'singular_name' => _x('Activity', 'post type 单个 item 时的名称,因为英文有复数'),
		'add_new' => _x('新建Activity', '添加新内容的链接名称'),
		'add_new_item' => __('新建Activity'),
		'edit_item' => __('编辑Activity'),
		'new_item' => __('新Activity'),
		'all_items' => __('所有Activity'),
		'view_item' => __('查看Activity'),
		'search_items' => __('搜索Activity'),
		'not_found' => __('没有找到有关Activity'),
		'not_found_in_trash' => __('回收站里面没有相关Activity'),
		'parent_item_colon' => '',
		'menu_name' => 'Activity'
	);
	$args = array(
		'labels' => $labels,
		'description' => '我们网站的Activity信息',
		'public' => true,
		'menu_position' => 5,
		'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'),
		'has_archive' => true
	);
	register_post_type('activity', $args);
}



//注册自定义文章类型-添加分类
function my_taxonomies_activity()
{
	$labels = array(
		'name' => _x('Activity分类', 'taxonomy 名称'),
		'singular_name' => _x('Activity分类', 'taxonomy 单数名称'),
		'search_items' => __('搜索Activity分类'),
		'all_items' => __('所有Activity分类'),
		'parent_item' => __('该Activity分类的上级分类'),
		'parent_item_colon' => __('该Activity分类的上级分类:'),
		'edit_item' => __('编辑Activity分类'),
		'update_item' => __('更新Activity分类'),
		'add_new_item' => __('添加新的Activity分类'),
		'new_item_name' => __('新Activity分类'),
		'menu_name' => __('Activity分类'),
	);
	$args = array(
		'labels' => $labels,
		'hierarchical' => true,
		'show_ui' => true,
		'show_in_menu' => true,
		'show_in_nav_menus' => true
	);
	register_taxonomy('activity_category', 'activity', $args);
}
add_action('init', 'my_taxonomies_activity', 0);

//注册自定义文章类型-joinactivity
/**
 * Register a custom post type called "joinactivity".
 *
 * @see get_post_type_labels() for label keys.
 */
add_action('init', 'my_custom_post_joinactivity');

function my_custom_post_joinactivity()
{
	$labels = array(
		'name' => _x('JoinActivity', 'post type 名称'),
		'singular_name' => _x('JoinActivity', 'post type 单个 item 时的名称,因为英文有复数'),
		'add_new' => _x('新建JoinActivity', '添加新内容的链接名称'),
		'add_new_item' => __('新建JoinActivity'),
		'edit_item' => __('编辑JoinActivity'),
		'new_item' => __('新JoinActivity'),
		'all_items' => __('所有JoinActivity'),
		'view_item' => __('查看JoinActivity'),
		'search_items' => __('搜索JoinActivity'),
		'not_found' => __('没有找到有关JoinActivity'),
		'not_found_in_trash' => __('回收站里面没有相关JoinActivity'),
		'parent_item_colon' => '',
		'menu_name' => 'JoinActivity'
	);
	$args = array(
		'labels' => $labels,
		'description' => '我们网站的JoinActivity信息',
		'public' => true,
		'menu_position' => 5,
		'supports' => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'),
		'has_archive' => true
	);
	register_post_type('joinactivity', $args);
}



//注册自定义文章类型-添加分类
function my_taxonomies_joinactivity()
{
	$labels = array(
		'name' => _x('JoinActivity分类', 'taxonomy 名称'),
		'singular_name' => _x('JoinActivity分类', 'taxonomy 单数名称'),
		'search_items' => __('搜索JoinActivity分类'),
		'all_items' => __('所有JoinActivity分类'),
		'parent_item' => __('该JoinActivity分类的上级分类'),
		'parent_item_colon' => __('该JoinActivity分类的上级分类:'),
		'edit_item' => __('编辑JoinActivity分类'),
		'update_item' => __('更新JoinActivity分类'),
		'add_new_item' => __('添加新的JoinActivity分类'),
		'new_item_name' => __('新JoinActivity分类'),
		'menu_name' => __('JoinActivity分类'),
	);
	$args = array(
		'labels' => $labels,
		'hierarchical' => true,
		'show_ui' => true,
		'show_in_menu' => true,
		'show_in_nav_menus' => true
	);
	register_taxonomy('joinactivity_category', 'joinactivity', $args);
}
add_action('init', 'my_taxonomies_joinactivity', 0);

/**
 * 关联活动
 */
$new_meta_boxes = array(
	'activity' => array(
		"name" => "_activity",
		"std" => "关联活动",
		"title" => "关联活动"
	)
);
/**
 * 添加元数据
 */
function new_meta_boxes()
{
	global $post, $new_meta_boxes;
	//获取自定义分类文章
	$args = array(
		'post_type' => 'activity'
	);
	$postslist = get_posts($args);
	foreach ($new_meta_boxes as $mate_box) {
		$meta_box_value = get_post_meta($post->ID, $mate_box['name'], true);
		if ($mate_box['name'] == '_activity') {
			$strs = '<select name="' . $mate_box['name'] . '"><option value="">请选择</option>';
			foreach ($postslist as $postess) {
				$gr_seleted = "";
				if ($postess->ID == (int) $meta_box_value) {
					$gr_seleted = "selected";
				}
				$strs .= '<option value="' . $postess->ID . '"  ' . $gr_seleted . '>' . $postess->post_title . '</option>';
			}
			$strs .= '</select>';
			// 自定义字段标题
			echo '<h3>' . $mate_box['title'] . '</h3>';
			echo $strs;
		}
	}
	echo '<input type="hidden" name="ludou_metaboxes_nonce" id="ludou_metaboxes_nonce" value="' . wp_create_nonce(plugin_basename(__FILE__)) . '" />';

}

/**
 * 创建自定义字段模块
 */
function create_meta_box()
{
	if (function_exists('add_meta_box')) {
		add_meta_box('new_meta_boxes', '自定义模块', 'new_meta_boxes', 'joinactivity', 'normal', 'high');
	}
}



//将函数连接到指定action(动作)
add_action('admin_menu', 'create_meta_box');
add_action('save_post', 'save_postdata');



//保存文章数据
function save_postdata($post_id)
{
	global $new_meta_boxes;
	if (!wp_verify_nonce($_POST['ludou_metaboxes_nonce'], plugin_basename(__FILE__)))
		return;
	if (!current_user_can('edit_posts', $post_id))
		return;
	foreach ($new_meta_boxes as $meta_box) {
		$data = $_POST[$meta_box['name'] . '_value'];
		if ($data == "")
			delete_post_meta($post_id, $meta_box['name'] . '_value', get_post_meta($post_id, $meta_box['name'] . '_value', true));
		else
			update_post_meta($post_id, $meta_box['name'] . '_value', $data);
	}
}
function load_assets($hook)
{
	$ver = '0.0.1';
	wp_enqueue_style('vite', plugin_dir_url(__FILE__) . 'dist/assets/index.css', array(), $ver, false);
	wp_enqueue_script('vite', plugin_dir_url(__FILE__) . 'dist/assets/wp-vite-main.js', array(), $ver, true);
}

add_action('admin_enqueue_scripts', 'load_assets');
add_action('wp_enqueue_scripts', 'load_assets');

function to_script_module($tag, $handle)
{
	if (strpos($tag, 'vite-main.js') !== false) {
		$tag = str_replace('<script', '<script type="module"', $tag);
	}
	return $tag;
}
add_filter('script_loader_tag', 'to_script_module', 10, 2);

/** 会员登录页面短码 */
add_shortcode('MenberPages', 'my_shortcode_menbers');


function my_shortcode_menbers($attr)
{
	$UserInfo = wp_get_current_user();
	$id = $attr['id'];
	$datas = get_posts($id);
	$title = $datas->post_title;
	$text = $datas->post_content;
	$url = wp_get_attachment_url(get_post_thumbnail_id($id));
	if (is_user_logged_in()) {
		?>
		<script id="shopeo-frame-json" type="application/json">
						{
							"activity_id": "<?php echo $id; ?>",
							"title":"<?php echo $title; ?>",
							"frame_img": "<?php echo $url; ?>",
							"activity_text": "<?php echo $text; ?>",
							"user_id":"<?php echo get_current_user_id(); ?>", 
							"user_email":"<?php echo $UserInfo->user_email; ?>",
							"user_name":"<?php echo $UserInfo->user_firstname; ?>"
						}
					</script>
		<div class="shopeo-frame-app" id="shopeo-frame-app"></div>
		<?php
	} else {
		?>
			<div class="loginOut" style="text-align:center;margin:50px auto;">
				<a href="/my-account/" style="padding:15px 40px; border:1px solid #333; background-color: #f1f1f1;">点击登录参与活动</a> 
			</div>
		<?php
	}

}





//获取列表
add_action("wp_ajax_vote_post_lists", "add_votes_list");
add_action("wp_ajax_nopriv_vote_post_lists", "add_votes_list");
//上传图片

add_action("wp_ajax_vote_upload_lists", "add_votes_opload_img");
add_action("wp_ajax_nopriv_vote_upload_lists", "add_votes_opload_img");
function add_votes_list()
{
	$userid = get_current_user_id();
	$activeid = $_POST['activity_id'];
	$arg = array(
		'post_type' => 'joinactivity',
		'post_author' => $userid,
		'post_status' => array('publish'),
		'meta_query' => array(
			array(
				'key' => '_activity',
				'value' => $activeid,
			)
		)
	);
	$arrList = new WP_Query($arg);
	wp_send_json($arrList->posts);
}
function add_votes_opload_img()
{
	require_once(ABSPATH . 'wp-admin/includes/image.php');
	require_once(ABSPATH . 'wp-admin/includes/file.php');
	require_once(ABSPATH . 'wp-admin/includes/media.php');
	$id = $_POST['activity_id'];
	$datas = get_post($id);
	$title = $datas->post_title;
	$content = $datas->post_content;
	if (isset($_POST['action']) && $_POST['action'] == 'vote_upload_lists') {
		$my_post = array(
			'post_status' => 'publish',
			'post_author' => get_current_user_id(),
			'post_type' => 'joinactivity',
			'post_title' => $title,
			'post_content' => $content
		);
		$post_id = wp_insert_post($my_post);
		$permalink = get_permalink($post_id);
		$attachment_id = media_handle_upload('file', $post_id);
		set_post_thumbnail($post_id, $attachment_id);
		update_post_meta($post_id, '_activity', $id);
		$url = wp_get_attachment_url(get_post_thumbnail_id($post_id));
		wp_send_json(array("share_link" => $permalink, "download_link" => $url));
	} else {

	}
}




add_filter('manage_joinactivity_posts_columns', 'add_new_joinactivity_columns');

function add_new_joinactivity_columns($book_columns)
{
	$new_columns['cb'] = '<input type="checkbox" />'; //这个是前面那个选框,不要丢了   
	$new_columns['id'] = __('ID');
	$new_columns['title'] = '标题';
	$new_columns['images'] = __('Images');
	$new_columns['author'] = __('Author');
	$new_columns['categories'] = __('Categories');
	$new_columns['tags'] = __('Tags');
	$new_columns['date'] = _x('Date', 'column name');
	//直接返回一个新的数组   
	return $new_columns;
}
add_action('manage_posts_custom_column', 'manage_joinactivity_columns', 10, 2);
function manage_joinactivity_columns($column_name, $id)
{
	global $wpdb;
	switch ($column_name) {
		case 'id':
			echo $id;
			break;

		case 'images':
			//$num_images = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts WHERE post_parent = {$id};"));   
			$num_images = wp_get_attachment_url(get_post_thumbnail_id($id));
			echo '<a href="'.$num_images.'"><img src="' . wp_get_attachment_url(get_post_thumbnail_id($id)) . '"></a>';
			break;
		default:
			break;
	}
}





add_filter('manage_activity_posts_columns', 'add_new_activity_columns');

function add_new_activity_columns($book_columns)
{
	$new_columns['cb'] = '<input type="checkbox" />'; //这个是前面那个选框,不要丢了   
	$new_columns['id'] = __('ID');
	$new_columns['title'] = '标题';
	$new_columns['shortCode'] = __('shortCode');
	$new_columns['date'] = _x('Date', 'column name');
	//直接返回一个新的数组   
	return $new_columns;
}
add_action('manage_posts_custom_column', 'manage_activity_columns', 10, 2);
function manage_activity_columns($column_name, $id)
{
	global $wpdb;
	switch ($column_name) {
		case 'shortCode':
			$shortCode = "[MenberPages id='$id']";
			echo $shortCode;
			break;
		default:
			break;
	}
}

/**
 * Filter slugs
 * @since 1.1.0
 * @return void
 */
add_action('restrict_manage_posts','author_select');
function author_select($post_type){
	if(post_type_supports($post_type,'author')){
		wp_dropdown_users([
			'name' => 'author',
			'who' => 'authors',
			'show_option_all' => '所有作者',
			'hide_if_only_one_author' => true,
			'selected' => $_REQUEST['author'] ?? 0
		]);
	}
}
?>

发表评论

Optimized by WPJAM Basic