Giter Club home page Giter Club logo

taxonomy_metadata's Introduction

Taxonomy_MetaData

WordPress Helper Class for saving pseudo-metadata for taxonomy terms. This is not recommend if using on a very large number of terms, UNLESS using a plugin like wp-large-options. The third parameter is used to override the get/set/delete options callbacks so that you can use wp-large-options (or something like it).

Includes an extended class for using CMB to generate the actual form fields. This is handy if you need more advanced fields, or do not want to create your own field render_cbs.

Originally inspired by @williamsba. Read his article here: How To: Save Taxonomy Meta Data as an Options Array in WordPress.

Available methods for retrieving data:

  • Get all meta for a taxonomy term:

     Taxonomy_MetaData::get( 'category', 3 );
  • Get specific term meta data:

     Taxonomy_MetaData::get( 'category', 3, 'about_text' );

To initate Taxonomy_MetaData and add fields to a taxonomy:

<?php

function taxonomy_meta_initiate() {

	require_once( 'Taxonomy_MetaData/Taxonomy_MetaData.php' );

	/**
	 * Instantiate our taxonomy meta class
	 */
	$cats = new Taxonomy_MetaData( 'category', array(
		// Term option key
		'about_text' => array(
			// Field label
			'label'    => __( 'About section for this term.', 'taxonomy-metadata' ),
			// Sanitization callback
			'sanitize_cb' => 'wp_kses_post',
			// Render callback
			'render_cb' => 'taxonomy_metadata_textarea',
		),
	), __( 'Category Settings', 'taxonomy-metadata' ) /* Settings heading */ );

	$tags = new Taxonomy_MetaData( 'post_tag', array(
		'arbitrary_text' => array(
			'label'       => __( 'Arbitrary text for tags', 'taxonomy-metadata' ),
			// Optional input description
			'desc'        => __( 'A description for this field', 'taxonomy-metadata' ),
			// Optional placeholder text if no value is saved
			'placeholder' => __( 'Placeholder text', 'taxonomy-metadata' ),
		),
	) );

}
taxonomy_meta_initiate();

/**
 * Textarea callback for Taxonomy_MetaData
 * @param  array  $field Field config
 * @param  mixed  $value Field value
 */
function taxonomy_metadata_textarea( $field, $value ) {
	echo '<textarea class="taxonomy-metadata-textarea" name="'. $field->id .'" id="'. $field->id .'">'. esc_textarea( $value ) .'</textarea>';
	if ( isset( $field->desc ) && $field->desc )
		echo "\n<p class=\"description\">{$field->desc}</p>\n";

}

To use wp_large_options (recommended):

<?php

function taxonomy_meta_initiate() {

	require_once( 'Taxonomy_MetaData/Taxonomy_MetaData.php' );
	require_once( 'wp-large-options/wp-large-options.php' );

	// Form title
	$title = __( 'Category Archive Options', 'taxonomy-metadata' );

	// Category fields
	$fields = array(
		'arbitrary_text' => array(
			'label'       => __( 'Arbitrary text for tags', 'taxonomy-metadata' ),
			'desc'        => __( 'A description for this field', 'taxonomy-metadata' ),
			'placeholder' => __( 'Placeholder text', 'taxonomy-metadata' ),
		),
	);

	// get/set/delete overrides using wp-large-options
	$overrides = array(
		'get_option'    => 'wlo_get_option',
		'update_option' => 'wlo_update_option',
		'delete_option' => 'wlo_delete_option',
	);

	/**
	 * Instantiate our taxonomy meta object
	 */
	new Taxonomy_MetaData( 'category', $fields, $title, $overrides );

}
taxonomy_meta_initiate();

To use Taxonomy_MetaData with Custom Metaboxes and Fields:

<?php

function cmb_taxonomy_meta_initiate() {

	require_once( 'Taxonomy_MetaData/Taxonomy_MetaData_CMB.php' );

	/**
	 * Semi-standard CMB metabox/fields array
	 */
	$meta_box = array(
		'id'         => 'cat_options',
		'show_on'    => array( 'key' => 'options-page', 'value' => array( 'unknown', ), ),
		'show_names' => true, // Show field names on the left
		'fields'     => array(
			array(
				'name' => __( 'Category Archive Featured Post', 'taxonomy-metadata' ),
				'desc' => __( 'Enter Post ID', 'taxonomy-metadata' ),
				'id'   => 'feat_post', // no prefix needed since the options are one option array.
				'type' => 'text',
			),
			array(
				'name' => __( 'Test Checkbox', 'taxonomy-metadata' ),
				'desc' => __( 'field description (optional)', 'taxonomy-metadata' ),
				'id'   => 'test_checkbox',
				'type' => 'checkbox',
			),
 			array(
				'name' => __( 'Test Text Small', 'taxonomy-metadata' ),
				'desc' => __( 'field description (optional)', 'taxonomy-metadata' ),
				'id'   => 'test_textsmall',
				'type' => 'text_small',
				// 'repeatable' => true,
			),
			array(
				'name'    => __( 'Test wysiwyg', 'taxonomy-metadata' ),
				'desc'    => __( 'field description (optional)', 'taxonomy-metadata' ),
				'id'      => 'test_wysiwyg',
				'type'    => 'wysiwyg',
				'options' => array( 'textarea_rows' => 5, ),
			),
		)
	);

	// (Recommneded) Use wp-large-options
	require_once( 'wp-large-options/wp-large-options.php' );
	$overrides = array(
		'get_option'    => 'wlo_get_option',
		'update_option' => 'wlo_update_option',
		'delete_option' => 'wlo_delete_option',
	);

	/**
	 * Instantiate our taxonomy meta class
	 */
	$cats = new Taxonomy_MetaData_CMB( 'category', $meta_box, __( 'Category Settings', 'taxonomy-metadata' ), $overrides );
}
cmb_taxonomy_meta_initiate();

taxonomy_metadata's People

Contributors

jtsternberg avatar mweimerskirch avatar

Watchers

Travis Smith avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.