php MU插件|示例文件

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php MU插件|示例文件相关的知识,希望对你有一定的参考价值。

<?php
/**
 * Must-Use Functions
 * 
 * A class filled with functions that will never go away upon theme deactivation.
 * 
 * @package WordPress
 * @subpackage GRD
 */
class MU_Functions {


	public function __construct() {

		add_action( 'init', array( $this, 'add_post_type' ) );
		add_action( 'init', array( $this, 'add_taxonomy' ) );
		

	}

	public function add_post_type() {

		$this->setup_post_type( array( 'Projects', 'Projects', 'projects', 'projects','dashicons-welcome-widgets-menus' ), array( 'menu_position' => '1' ) );
		//$this->setup_post_type( array( 'The Team', 'The Team', 'the-team', 'the-team','dashicons-universal-access-alt' ), array( 'menu_position' => '2' ) );
	}

		public function setup_post_type( $type, $args = array() ) {

			if ( is_array( $type ) ) {
				$types = isset( $type[0] ) ? $type[0] : $type . 's';
				$singular = isset( $type[1] ) ? $type[1] : $type . 's';
				$key = isset( $type[2] ) ? $type[2] : strtolower( str_ireplace( ' ', '_', $type[1] ) );
				$slug = isset( $type[3] ) ? $type[3] : str_ireplace( '_', '-', $key );
				$dashicon = $type[4];
				$type = $type[1];
			} else {
				$types = $type . 's';
				$key = strtolower( str_ireplace( ' ', '_', $type ) );
				$slug = str_ireplace( '_', '-', $key );
			}

			$labels = array(
				'name'                => $types,
				'singular_name'       => $singular,
				'add_new'             => 'Add New',
				'add_new_item'        => 'Add New ' . $singular,
				'edit_item'           => 'Edit ' . $singular,
				'new_item'            => 'New ' . $singular,
				'view_item'           => 'View ' . $singular,
				'search_items'        => 'Search ' . $types,
				'not_found'           => 'No ' . $types . ' found',
				'not_found_in_trash'  => 'No ' . $types . ' found in Trash',
				'parent_item_colon'   => '',
				'menu_name'           => $types,
				'all_items'			  => 'All ' . $types
			);

			$rewrite = array(
				'slug'                => $slug,
				'with_front'          => true,
				'pages'               => true,
				'feeds'               => true,
			);

			$args = wp_parse_args( $args, array(
				'labels'              => $labels,
				'public'              => true,
				'publicly_queryable'  => true,
				'show_ui'             => true,
				'query_var'           => true,
				'rewrite'             => $rewrite,
				'capability_type'     => 'post',
				'hierarchical'        => true,
				'menu_position'       => '5',
				'has_archive'         => true,
				'exclude_from_search' => false,
				'supports'            => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments','genesis-cpt-archives-settings' ),
				'taxonomies'          => array(),
				'menu_icon'			  => $dashicon
			));

			register_post_type( $key, $args );

		}




	public function add_taxonomy() {

		/* $this->setup_taxonomy( 'Department', 'Department', 'department', 'department', array( 'the-team' ) );
		$this->setup_taxonomy( 'Plugin Category', 'Plugin Categories', 'plugin-category', 'plugin-category', array( 'wp-plugins' ) );
		$this->setup_taxonomy( 'Skills', 'Skill', 'skills', 'skills', array( 'portfolio' ) );
		$this->setup_taxonomy( 'Tag', 'Photo Tags', 'photos', 'photos-tags', array( 'photos' ) );
		$this->setup_taxonomy( 'Tag', 'Recipe Tags', 'recipes', 'recipes-tags', array( 'recipes' ) ); */

	}

		public function setup_taxonomy( $type, $types, $key, $url_slug, $post_type_keys ) {

			$labels = array(
				'name'                       => $types,
				'singular_name'              => $type,
				'search_items'               => 'Search ' . $types,
				'popular_items'              => 'Common ' . $types,
				'all_items'                  => 'All ' . $types,
				'parent_item'                => null,
				'parent_item_colon'          => null,
				'edit_item'                  => 'Edit ' . $type,
				'update_item'                => 'Update ' . $type,
				'add_new_item'               => 'Add New ' . $type,
				'new_item_name'              => 'New ' . $type . ' Name',
				'separate_items_with_commas' => 'Separate ' . $types . ' with commas',
				'add_or_remove_items'        => 'Add or remove ' . $types,
				'choose_from_most_used'      => 'Choose from the most used ' . $types
			);

			$rewrite = array(
				'slug'                       => $url_slug,
				'with_front'                 => true,
				'hierarchical'               => true,

			);

			$args = array(
				'labels'                     => $labels,
				'hierarchical'               => true,
				'public'                     => true,
				'show_ui'                    => true,
				'show_admin_column'          => true,
				'show_in_nav_menus'          => true,
				'show_tagcloud'              => true,
				'query_var'                  => true,
				'rewrite'                    => $rewrite
			);

			register_taxonomy( $key, $post_type_keys, $args );

		}




} 

// Instantiate the class
$MU_Functions = new MU_Functions();

以上是关于php MU插件|示例文件的主要内容,如果未能解决你的问题,请参考以下文章

php MU插件|新的自定义帖子类型

php 相对WP URL替换MU插件

php mu-plugin carga condicional插件

php 此MU插件允许从Checkout页面删除字段

php [WordPress] [WP Rocket]自定义功能(MU)插件的Boilerplate。

php 这个MU插件允许使用格式Y-m-d按日期查找证书