PHP code example of brightnucleus / custom-content

1. Go to this page and download the library: Download brightnucleus/custom-content library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

brightnucleus / custom-content example snippets


 namespace CPT\Example;

use BrightNucleus\Config\ConfigFactory;
use BrightNucleus\CustomContent\CustomPostType;

// You can of course load your Config from a file. We create one directly here
// to make the example clearer.
$config = ConfigFactory::create( [

 	// This configuration key represents the slug of the CPT.
	'example' => [

		// For most localization needs, it should be sufficient to only provide
		// these four name variants. The Custom Content component will figure
		// out the rest.
		Argument::NAMES => [
			Name::SINGULAR_NAME_UC => _x('Example', 'post type uc singular name', 'cpt-example'),
			Name::SINGULAR_NAME_LC => _x('example', 'post type lc singular name', 'cpt-example'),
			Name::PLURAL_NAME_UC   => _x('Examples', 'post type uc plural name', 'cpt-example'),
			Name::PLURAL_NAME_LC   => _x('examples', 'post type lc plural name', 'cpt-example'),
		],

		// Here, we register the taxonomy we'll later create with our new custom
		// post type.
		Argument::TAXONOMIES => [ 'taxexample' ],

		// We also add some supported features to the custom post type.
		Argument::SUPPORTS => [
			Feature::TITLE,
			Feature::AUTHOR,
			Feature::REVISIONS,
			Feature::COMMENTS,
			Feature::THUMBNAIL,
		],
	],
] );

// Create a new `CustomPostType` instance configured by our new Config file.
$example_cpt = new CustomPostType( $config );

// Register this new custom post type with WordPress.
// Note that CPTs should always be registered within the `init` hook.
add_action( 'init', [ $example_cpt, 'register' ] );

 namespace Tax\Example;

use BrightNucleus\Config\ConfigFactory;
use BrightNucleus\CustomContent\CustomTaxonomy;

// You can of course load your Config from a file. We create one directly here
// to make the example clearer.
$config = ConfigFactory::create( [

 	// This configuration key represents the slug of the CPT.
 	'taxexample' => [

		// For most localization needs, it should be sufficient to only provide
		// these four name variants. The Custom Content component will figure
		// out the rest.
		Argument::NAMES => [
			Name::SINGULAR_NAME_UC => _x('TaxExample', 'taxonomy uc singular name', 'tax-example'),
			Name::SINGULAR_NAME_LC => _x('taxexample', 'taxonomy lc singular name', 'tax-example'),
			Name::PLURAL_NAME_UC   => _x('TaxExamples', 'taxonomy uc plural name', 'tax-example'),
			Name::PLURAL_NAME_LC   => _x('taxexamples', 'taxonomy lc plural name', 'tax-example'),
		],

		// Here, we register the taxonomy with our previously created custom
		// post type.
		Argument::POST_TYPES => [ 'example' ],
	],
] );

// Create a new `CustomTaxonomy` instance configured by our new Config file.
$example_tax = new CustomTaxonomy( $config );

// Register this new custom taxonomy with WordPress.
// Note that Taxonomies should always be registered within the `init` hook.
add_action( 'init', [ $example_tax, 'register' ] );

 namespace CustomContent\Example;

use BrightNucleus\Config\ConfigFactory;
use BrightNucleus\CustomContent\CustomContent;
use BrightNucleus\CustomContent\CustomPostType\Argument as CPTArgument;
use BrightNucleus\CustomContent\CustomTaxonomy\Argument as TaxArgument;

// You can of course load your Config from a file. We create one directly here
// to make the example clearer.
$config = ConfigFactory::create( [

	// In this example, we want to register two custom post types (`book` &
	// `magazine`) as well as two custom taxonomies related to these
	// (`publisher`, `shelf`).

	'CustomPostType' => [
		'book'       => [
			// Arguments to define a book.
			// [...]
			CPTArgument::TAXONOMIES => [ 'publisher', 'shelf' ],
		],
		'magazine'   => [
			// Arguments to define a magazine.
			// [...]
			CPTArgument::TAXONOMIES => [ 'publisher', 'shelf' ],
		],
	],

	'CustomTaxonomy' => [
		'publisher'  => [
			// Arguments to define a publisher.
			// [...]
			TaxArgument::POST_TYPES => [ 'book', 'magazine' ],
		],
		'shelf'      => [
			// Arguments to define a publisher.
			// [...]
			TaxArgument::POST_TYPES => [ 'book', 'magazine' ],
		],
	],
] );

// Create a new `CustomContent` instance configured by our new Config file.
$custom_content = new CustomContent( $config );

// Register this new custom content with WordPress.
add_action( 'init', [ $custom_content, 'register' ] );

 namespace CustomContent\Example;

use BrightNucleus\Config\ConfigFactory;
use BrightNucleus\CustomContent\CustomContent;

function register_custom_content() {
    static $custom_content;

    if ( null === $custom_content ) {
		$config = ConfigFactory::create( __DIR__ . '/config/custom_content.php' );
		$custom_content = new CustomContent( $config );
	}

    $custom_content->register();
}
add_action( 'init', __NAMESPACE__ . '\\register_custom_content' );

function flush_rewrite_rules() {
	cc_example_register_custom_content();
	flush_rewrite_rules();
}
register_activation_hook( __FILE__, __NAMESPACE__ . '\\flush_rewrite_rules' );