slimline_add_term_tinymce() - slimline/tinymce GitHub Wiki

Adds TinyMCE hooks for taxonomy pages.

Developers can control when TinyMCE is added to term descriptions using the slimline_add_term_tinymce filter. By default TinyMCE is added for any user who can manage categories.

Source

/**
 * Init TinyMCE hooks for taxonomy pages.
 *
 * Adds the action hooks for replacing the term description textarea with a TinyMCE
 * editor. Using a separate function allows us to dynamically add the hooks to all
 * taxonomies.
 *
 * @link  https://github.com/slimline/tinymce/wiki/slimline_add_term_tinymce()
 * @since 0.1.0
 */
function slimline_add_term_tinymce() {

	/**
	 * @global string $taxnow The taxonomy slug for the current term
	 */
	global $taxnow;

	/**
	 * Exit function if taxonomy not set
	 */
	if ( ! $taxnow ) {
		return;
	} // if ( ! $taxnow )

	/**
	 * Filter which users are allowed to use TinyMCE for term descriptions
	 *
	 * By default, any user who can edit categories can also use TinyMCE in term
	 * descriptions. This is redundant since those users are also the only ones who
	 * can access the edit-tags.php page, but use of the filter allows developers to
	 * create more stringent or varied rules if they choose.
	 *
	 * @param bool Whether or not the current user is allowed to use the TinyMCE
	 *             Editor for term descriptions. Defaults to TRUE if the user can
	 *             manage categories, FALSE if they cannot.
	 * @link  https://codex.wordpress.org/Roles_and_Capabilities#manage_categories
	 *        Description of `manage_categories` capability
	 * @since 0.3.0
	 */
	$add_term_tinymce = apply_filters( 'slimline_add_term_tinymce', current_user_can( 'manage_categories' ) );

	if ( $add_term_tinymce ) {

		/**
		 * Remove default term description filter.
		 *
		 * The default wp_filter_kses for term descriptions strips most HTML content,
		 * rendering the TinyMCE editor useless. We will add the same HTML filter as
		 * is used with posts later.
		 *
		 * @link https://developer.wordpress.org/reference/hooks/pre_term_description/
		 *       Description of `pre_term_description` filter
		 */
		remove_filter( 'pre_term_description', 'wp_filter_kses' );

		/**
		 * Start output buffering for add term form
		 *
		 * We force passing 0 parameters to prevent setting ob_start's callback
		 * parameter.
		 *
		 * @link https://developer.wordpress.org/reference/hooks/taxonomy_pre_add_form/
		 *       Documentation of `{$taxnow}_pre_add_form` hook
		 */
		add_action( "{$taxnow}_pre_add_form", 'ob_start', 1000, 0 );

		/**
		 * Complete buffering and output content for add term form
		 *
		 * @link https://developer.wordpress.org/reference/hooks/taxonomy_pre_add_form/
		 *       Documentation of `{$taxnow}_add_form_fields` hook
		 */
		add_action( "{$taxnow}_add_form_fields", 'slimline_tinymce_output_wp_editor', 0 );

		/**
		 * Start output buffering for edit term form
		 *
		 * We force passing 0 parameters to prevent setting ob_start's callback
		 * parameter.
		 *
		 * @link https://developer.wordpress.org/reference/hooks/taxonomy_pre_add_form/
		 *       Documentation of `{$taxnow}_pre_edit_form` hook
		 */
		add_action( "{$taxnow}_pre_edit_form", 'ob_start', 1000, 0 );

		/**
		 * Complete buffering and output content for edit term form
		 *
		 * @link https://developer.wordpress.org/reference/hooks/taxonomy_pre_add_form/
		 *       Documentation of `{$taxnow}_edit_form_fields` hook
		 */
		add_action( "{$taxnow}_edit_form_fields", 'slimline_tinymce_output_wp_editor', 0 );

		/**
		 * Add posts HTML filter to term descriptions.
		 *
		 * @link https://developer.wordpress.org/reference/hooks/pre_term_description/
		 *       Documentation of `pre_term_description` filter
		 */
		add_filter( 'pre_term_description', 'slimline_wp_filter_kses' );

	} // if ( $add_term_tinymce )

}

Hook

Fires on load-edit-tags.php at priority 10

Changelog

  • Since 0.1.0