定制 aurooba/cpt 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

aurooba/cpt

Composer 安装命令:

composer require aurooba/cpt

包简介

A small helper class for registering Custom Post Types and their taxonomies in WordPress

关键字:

README 文档

README

Packagist Downloads

Packagist PHP Version Support Packagist License

Custom Post Type and Taxonomies Generator

Quickly register custom post types and associated taxonomies with intelligent defaults in your WordPress projects.

For complete documentation of Custom Post Type registration, see https://developer.wordpress.org/reference/functions/register_post_type/

For complete documentation of Taxonomy registration, see https://developer.wordpress.org/reference/functions/register_taxonomy/

Minimum Requirements

  • PHP: 7.0
    • PHP 7.4+ is recommended
  • WordPress: 5.0
    • Tested up to WP 5.6.2

Installation

The easiest way is to install this through Composer with:

composer require aurooba/cpt

Make sure you have composer autoload set up where you are adding the package.

For themes, in your functions.php file:

require get_template_directory() . '/vendor/autoload.php';

For plugins, in your main plugin file, near the top:

require plugin_dir_path( __FILE__ ) . 'vendor/autoload.php';

Usage

Don't see an option you use often and would love for it to be easier too? Open an issue or pop it into the Discussion area, let's make it happen. :)

Have a question about your specific use case? Ask in a support discussion.

Basic Usage

// Yep feel free to use spaces in your name
$custom_post_type = new Aurooba\CPT( 'custom post type' );

The class, by default, takes in just one parameter: a singular name, like resource. It's smart enough to convert resource into resources, country into countries, potato into potatoes, goose into geese, and handles some exceptions as well.

At this time, it is not smart enough to handle uncountable nouns such as rice, tea, knowledge, etc. This is a planned addition, coming later.

Customized Usage

$custom_post_type = new Aurooba\CPT(
	'singular name', // singular taxonomy name, human readable
	$args, // an array of custom parameters for the custom post type
	$labels // an array of custom labels for the custom post type
);

$custom_post_type->add_taxonomy(
	'Taxonomy', // singular taxonomy name
	// array of custom parameters for the taxonomy
	array(
		'hierarchical' => false,
	),
	$labels, // array of custom labels for the taxonomy
)

Icons

The default icon, set up in the class is the screenoptions Dashicon, purely because I like it.

Feel free to pass a different Dashicon to your class as shown in this different icon example.

You can also pass custom SVG icons, as shown in this custom svg icon example.

Taxonomies

The class can also attach existing taxonomies to your CPT or generate a new taxonomy to attach to the CPT.

Examples

Register a basic Custom Post Type:

/**
 * Initialize a Resource Custom Post Type
 * @return void
 */
function initialize_cpts() {

	$resource = new Aurooba\CPT( 'resource');

}

add_action( 'after_setup_theme', 'initialize_cpts' );

Register and associate a basic taxonomy with the Custom Post Type:

/**
 * Initialize a Resource Custom Post Type
 * @return void
 */
function initialize_cpts_and_taxonomies() {

	// initialize cpt
	$resource = new Aurooba\CPT( 'resource' );

	// add Resource Type taxonomy
	$resource->add_taxonomy( 'Resource Type' );

}

add_action( 'after_setup_theme', 'initialize_cpts_and_taxonomies' );

Register a Custom Post Type with a different icon

$resource = new Aurooba\CPT(
		'resource',
		array( 'menu_icon' => 'dashicons-share-alt' ),
	);

Register a Custom Post Type with a custom icon

// Your icon SVG Code. Alternatively, you can pass the path to an SVG file.
$resource_icon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 640 512"><path fill="#ffffff" d="M608 0H160c-17.67 0-32 13.13-32 29.33V112h48V48h48v64h48V48h224v304h112c17.67 0 32-13.13 32-29.33V29.33C640 13.13 625.67 0 608 0zm-16 304h-48v-56h48zm0-104h-48v-48h48zm0-96h-48V48h48zM128 320a32 32 0 1 0-32-32 32 32 0 0 0 32 32zm288-160H32a32 32 0 0 0-32 32v288a32 32 0 0 0 32 32h384a32 32 0 0 0 32-32V192a32 32 0 0 0-32-32zm-16 240L299.31 299.31a16 16 0 0 0-22.62 0L176 400l-36.69-36.69a16 16 0 0 0-22.62 0L48 432V208h352z"/></svg>';

$resource = new Aurooba\CPT(
		'resource',
		array(
			'menu_icon' => 'data:image/svg+xml;base64,' . base64_encode( $resource_icon ) ),
	);

Create a customized Custom Post Type:

/**
 * Initialize a Resource Custom Post Type
 * @return void
 */
function initialize_cpts() {

	// create a block template to include in the custom post type
	$block_template = array(
		array(
			'core/heading',
			array(
				'level'       => 2,
				'placeholder' => __( 'Resource Type', 'textdomain' ),
			),
		),
		array(
			'core/paragraph',
			array(
				'content' => __( 'Places where this resource can be helpful are:>', 'textdomain' ),
			),
		),
	);

	$resource = new Aurooba\CPT(
		'resource',
		array(
			'menu_position' => 26,
			'menu_icon'     => 'dashicons-plus-alt',
			'template'      => $block_template,
			'rewrite'       => true,
			'capability_type' => 'resource',
			'map_meta_cap' => true,
			'supports'      => array(
				'title',
				'editor',
				'thumbnail',
				'author',
				'custom-fields',
			),
		),
	);

}

add_action( 'after_setup_theme', 'initialize_cpts' );

Create a customized taxonomy:

	$resource->add_taxonomy(
		'Resource Type',
		array(
			'capabilities' => array(
				'manage_terms' => 'manage_resource_type',
				'edit_terms'   => 'edit_resource_type',
				'delete_terms' => 'delete_resource_type',
				'assign_terms' => 'assign_resource_type',
			),
		),
	);

Attach an existing taxonomy:

$resource->add_taxonomy( 'Post Tag' );
$resource->add_taxonomy( 'Category' );

Contributing

Contributors are welcome! Come help extend, test, and improve this package so it becomes an insanely helpful library. Check out CONTRIBUTING.md for more information.

License

Created by Aurooba Ahmed. Licensed under the terms of the GPL v2 or later.

统计信息

  • 总下载量: 26
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 10
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 9
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-2.0-or-later
  • 更新时间: 2021-03-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固