Created
July 14, 2015 09:20
-
-
Save ditikos/4f44b2f03243cd13df20 to your computer and use it in GitHub Desktop.
Custom taxonomy preselection (eg prefecture) on wordpress "Add new page/post/CPT"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Στο παράδειγμα που χρησιμοποιώ, έχω συνδέσει τις pages με την category taxonomy.<br/> | |
Επίσης έχει και συνδεση με το polylang (εαν ειναι ενεργοποιημενο, ειδαλλως ολα τα taxonomy terms θεωρουνται στην default language). | |
Στο pages εχω κανει assign 3 terms: homepage-elements, home-slider, galleries, με αντίστοιχα πολυγλωσσικα. | |
1. δημιουργούμε την δομή στο χρήστη (usermeta) που θα κρατήσει το taxonomy term. | |
Για λόγους ευκολίας θα είναι textbox με key "assigned_term" και την τιμή homepage_elements. Ιδανικά θα ήταν ενα dropdown με τα terms ή μια ομαδα checkboxes. | |
Στο functions.php προσθέτουμε τα εξής: | |
<?php | |
add_action( 'show_user_profile', 'add_extra_user_fields' ); | |
add_action( 'edit_user_profile', 'add_extra_user_fields' ); | |
function add_extra_user_fields( $user ) | |
{ | |
?> | |
<h3>New User Profile Fields</h3> | |
<table class="form-table"> | |
<tr> | |
<th><label for="assigned_term">Predefined Term</label></th> | |
<td><input type="text" name="assigned_term" value="<?php echo esc_attr(get_the_author_meta( 'assigned_term', $user->ID )); ?>" class="regular-text" /></td> | |
</tr> | |
</table> | |
<?php | |
} | |
add_action( 'personal_options_update', 'save_extra_user_fields' ); | |
add_action( 'edit_user_profile_update', 'save_extra_user_fields' ); | |
function save_extra_user_fields( $user_id ) | |
{ | |
update_user_meta( $user_id,'assigned_term', sanitize_text_field( $_POST['assigned_term'] ) ); | |
} | |
add_action( 'admin_enqueue_scripts', 'my_admin_enqueue_scripts',10,1 ); | |
function my_admin_enqueue_scripts( $hook ) { | |
global $post,$current_user; | |
// Ελεγχος για την σελιδα, αν ειναι η σελιδα δημιουργιας post/page (εδω μπορει να υπαρξει διαφοροποιηση). | |
// Αυτο ισχυει για ολα τα add new xxx σε αυτη την υλοποιηση. | |
// Για συγκεκριμενο custom post type προσθετουμε και ενα check με if ($post->post_type === $CPT$) {...} | |
// οπου $CPT$ το slug του custom post type που θελουμε να βάλουμε τον έλεγχο. | |
if ( $hook == 'post-new.php' ) { | |
// Ανάκτηση του user meta απο το τρέχον χρήστη | |
$user_id = $current_user->ID; | |
$assigned_term = get_user_meta($user_id, 'assigned_term', true); | |
// Ελεγχος αν υπαρχει assigned term στο χρηστη. Ειδαλλως απελευθερωσε ολα τα πεδια. | |
if ($assigned_term!=""): | |
// Εδω μπορει να υπαρξει διαφοροποιηση αναλογα με το custom term/ custom category | |
// Το "category" εδω αντιστοιχει στο custom taxonomy. | |
$term = get_term_by('slug',$assigned_term,'category'); | |
$the_term = $term->term_id; | |
// Polylang Support | |
if (function_exists('pll_get_term')): | |
$the_term = pll_get_term($term->term_id, 'en'); | |
endif; | |
// Χρηση helper array/js-object | |
$helper = array( | |
'assigned_term' => $term->slug, | |
'term_id' => $the_term, | |
'user_id' => $user_id, | |
); | |
// Το script θα ειναι στο: [wordpress_root]/wp-content/themes/[current_theme]/js/main-admin.js | |
wp_register_script('main-admin', get_stylesheet_directory_uri() . "/js/main-admin.js", array('jquery')); | |
// Inject το array στο script | |
wp_localize_script( 'main-admin', 'helper', $helper ); | |
// Εισαγωγή του script στο queue του admin. | |
wp_enqueue_script('main-admin'); | |
endif; | |
} | |
} | |
?> | |
Αντίστοιχα στο main-admin.js αυτό που χρειαζόμαστε ειναι ο κώδικας που θα επιλεγει το συγκεκριμένο category. | |
Τα πεδία αυτά μπορεί να χρειάζεται να αλλαχθούν ανάλογα με το τι ονομασία έχει στα αντίστοιχα div / checkbox ids. | |
jQuery(function($){ | |
// Το helper object εισάχθηκε απο το wp_localize_script() στο functions.php | |
$("#in-category-"+helper.term_id).prop('checked',true); | |
// Λίγη jquery: Επιλέγω τα div που ειναι στο All Categories / Most Used Categories. | |
// Απο εκει βρίσκω ΟΛΑ τα not checked checkboxes. | |
// Και τα κανω disable. | |
$("#categorychecklist, #categorychecklist-pop").find(":checkbox:not(':checked')").prop('disabled',true); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment