We need to display custom taxonomy options in post quick edit page as dropdown as below screen shot: |
The very first step is to register taxonomy.
https://pritomkumar.blogspot.com/2019/02/wordpress-plugin-how-to-create-custom.html will help you. |
Now the target is to display taxonomy options in quick edit posts page. For that we have to register some actions as below: |
<?php add_action('admin_enqueue_scripts', array(self::class, 'my_add_admin_scripts')); add_filter('manage_posts_columns', array(self::class, 'add_post_fake_column'), 10, 2); add_filter('manage_edit-post_columns', array(self::class, 'remove_post_fake_column')); add_action('quick_edit_custom_box', array(self::class, 'add_post_quick_edit_box'), 10, 2); add_action('save_post', array(self::class, "my_taxonomy_post_save_callback"), 1, 2); $action_name = 'plugin_my_personal_taxonomy'; add_action ('wp_ajax_' . $action_name, array(self::class, 'ajax_call_your_function')); add_action ('wp_ajax_nopriv_' . $action_name, array(self::class, 'ajax_call_your_function')); |
<?php public static function my_add_admin_scripts() { global $pagenow; $taxonomy = MY_TAXONOMY_PLUGIN_CORE . 'color'; wp_register_script( 'MY_TAXONOMY_PLUGIN_CORE', MY_TAXONOMY_PLUGIN_URL . 'assets/script7.js', array('jquery') ); $params = array( 'BASE_URL' => MY_TAXONOMY_PLUGIN_URL, 'ADMIN_URL' => admin_url() ); wp_localize_script( 'MY_TAXONOMY_PLUGIN_CORE', 'MY_TAXONOMY_PLUGIN_CORE', $params); wp_enqueue_script('MY_TAXONOMY_PLUGIN_CORE'); wp_enqueue_style( "MY_TAXONOMY_PLUGIN_CORE", MY_TAXONOMY_PLUGIN_URL . "assets/style7.css" ); } |
<?php public static function add_post_fake_column($posts_columns, $post_type) { $posts_columns['fake_column_taxonomy_color'] = 'Fake Column (Invisible)'; return $posts_columns; } public static function remove_post_fake_column($posts_columns) { unset($posts_columns['fake_column_taxonomy_color']); return $posts_columns; } |
<?php public static function add_post_quick_edit_box($column_name, $post_type) { if (true) { $taxonomy = get_taxonomy(self::getTaxonomyName()); $taxonomy_options = get_terms(array( 'taxonomy' => self::getTaxonomyName(), 'hide_empty' => false, )); if (count($taxonomy_options) > 0) { $post_terms = []; $post_id = isset($GLOBALS['post_id']) ? $GLOBALS['post_id'] : get_the_ID(); foreach(wp_get_post_terms($post_id, self::getTaxonomyName()) as $o) { $post_terms[$o->term_id] = 1; } global $taxonomy_data_map; $taxonomy_data_map = []; $taxonomy_data_map['name'] = self::getTaxonomyName(); $taxonomy_data_map['value_field'] = $taxonomy->hierarchical ? 'term_id' : 'name'; $taxonomy_data_map['options'] = $taxonomy_options; $taxonomy_data_map['selected'] = $post_terms; include_once MY_TAXONOMY_PLUGIN_DIR . 'templates/taxonomy_post_quick_edit.php'; wp_nonce_field( MY_TAXONOMY_PLUGIN_NONCE_FIELD_NAME, MY_TAXONOMY_PLUGIN_NONCE_FIELD_NAME ); } } } |
taxonomy_post_quick_edit.php<?php global $taxonomy_data_map; $terms = $taxonomy_data_map['options']; $value_field = $taxonomy_data_map['value_field']; $selected = $taxonomy_data_map['selected']; ?> <fieldset class="inline-edit-col-left my_taxonomy_plugin_color-quick-editor"> <div class="inline-edit-col"> <span class="title"><?= __('Colors') ?></span> <select name='<?= $taxonomy_data_map['name'] ?>[]' id='<?= $taxonomy_data_map['name'] ?>' multiple> <?php foreach ($terms as $term) { $_checked = isset($selected[$term->term_id]) ? 'selected' : ''; echo "<option value='{$term->{$value_field}}' {$_checked}>{$term->name}</option>\n"; } ?> </select> </div> </fieldset> |
<?php public static function my_taxonomy_post_save_callback($id, $post) { if ((defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) || !current_user_can('edit_page', $id)) { return $id; } if (!isset($_POST[MY_TAXONOMY_PLUGIN_NONCE_FIELD_NAME])) { return $id; } if (!wp_verify_nonce($_POST[MY_TAXONOMY_PLUGIN_NONCE_FIELD_NAME], MY_TAXONOMY_PLUGIN_NONCE_FIELD_NAME)) { return $id; } if (!current_user_can( 'edit_post', $id)) { return $id; } $taxonomy = get_taxonomy(self::getTaxonomyName()); $field = $taxonomy->hierarchical ? 'term_id' : 'name'; $taxonomy_options = array(); foreach(get_terms(array('taxonomy' => self::getTaxonomyName(), 'hide_empty' => false)) as $o) { $taxonomy_options[strval($o->{$field})] = 1; } if (count($taxonomy_options)) { $options = array(); foreach((isset($_POST[self::getTaxonomyName()]) ? $_POST[self::getTaxonomyName()] : array()) as $i) { if (isset($taxonomy_options[$i])) { array_push($options, $i); } } wp_set_post_terms($id, $options, self::getTaxonomyName()); } else { wp_set_post_terms($id, array(), self::getTaxonomyName()); } return $id; } |
<?php public static function ajax_call_your_function() { $case = isset($_REQUEST['case']) ? $_REQUEST['case'] : ''; switch ($case) { case 'post-quick-edit': $id = isset($_REQUEST['id']) ? doubleval($_REQUEST['id']) : 0; if ($id > 0) { $GLOBALS['post_id'] = $id; self::add_post_quick_edit_box(null, null); wp_die(); } break; } header("HTTP/1.1 404"); echo __("Not Found"); exit; } |
script7.jsjQuery(document).ready(function(){ var $ = jQuery, body = $(document.body); if (typeof inlineEditPost !== 'undefined') { (function ($, inlineEditPost) { // inlineEditTax does not invoke any events, but does ensure to stop // propagation to all other event handlers; swap it out. inlineEditPost.realOne = inlineEditPost.edit; inlineEditPost.edit = function (link) { // Invoke original edit event handler. this.realOne(link); var id = inlineEditPost.getId(link), tr = $(link).closest("table").find("tr#edit-" + id); var qe = tr.find(".my_taxonomy_plugin_color-quick-editor"); if (qe.length) { $.ajax ({ url: MY_TAXONOMY_PLUGIN_CORE.ADMIN_URL + '/admin-ajax.php', type: 'POST', dataType: 'HTML', data: { action: 'plugin_my_personal_taxonomy', case: 'post-quick-edit', id: id }, success: function (resp) { qe.html($(resp).html()); }, error: function (xhr, ajaxOptions, thrownError) { }, }); } return false; } })($, inlineEditPost); } }); |
Download full plugin from BitBucket |
Friday, February 22, 2019
WordPress | Customizing Taxonomies as Dropdowns in Quick Edit | Display a custom taxonomy as a dropdown on the quick edit posts page
Subscribe to:
Post Comments (Atom)
Here you can download php, codeigniter and laravel example project. Learn Codeigniter from scratch. Learn Laravel From scratch.
ReplyDeletePhp Projects
Clear example
ReplyDelete