Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- new my_acf_extension();
- class my_acf_extension
- {
- public function __construct()
- {
- // kalliergeia field on poikilia
- add_action('acf/load_field/key=field_579376941cecc', array($this, 'load_kalliergeia_field_choices'));
- // kalliergeia field on post
- add_action('acf/load_field/key=field_5b5e20e06334b', array($this, 'load_kalliergeia_field_choices'));
- // poikilia field on post
- add_action('acf/load_field/key=field_5b5e20cb6334a', array($this, 'load_poikilia_field_choices'));
- // ajax action for loading poikilia choices
- add_action('wp_ajax_load_poikilia_field_choices', array($this, 'ajax_load_poikilia_field_choices'));
- // enqueue js extension for acf
- // do this when ACF in enqueuing scripts
- add_action('acf/input/admin_enqueue_scripts', array($this, 'enqueue_script'));
- } // end public function __construct
- public function load_kalliergeia_field_choices($field)
- {
- // this funciton dynamically loads the kalliergeia select field choices
- // from the kalliergeia post type
- // I only want to do this on paragogos and poikilia CPT
- global $post;
- if (!$post ||
- !isset($post->ID) ||
- (get_post_type($post->ID) != 'paragogos' && get_post_type($post->ID) != 'poikilia')) {
- return $field;
- }
- // get kalliergeies
- $args = array(
- 'post_type' => 'kalliergeia',
- 'posts_per_page' => -1,
- 'orderby' => 'title',
- 'order' => 'ASC'
- );
- $query = new WP_Query($args);
- if (get_post_type($post->ID) == 'paragogos') {
- $choices = array('' => '-- Καλλιέργεια --');
- } // Default value on paragogos CPT
- if (get_post_type($post->ID) == 'poikilia') {
- $choices = array();
- } // Empty default value on poikilia CPT
- if (count($query->posts)) {
- // populate choices
- foreach ($query->posts as $kalliergeia) {
- $choices[$kalliergeia->ID] = $kalliergeia->post_title;
- }
- }
- $field['choices'] = $choices;
- //debug_to_console($field);
- return $field;
- } // end public function load_kalliergeia_field_choices
- public function load_poikilia_field_choices($field)
- {
- // this function dynamically loads poikilia field choices
- // based on the currently saved kalliergeia
- // I only want to do this on paragogos CPT
- global $post;
- if (!$post ||
- !isset($post->ID) ||
- get_post_type($post->ID) != 'paragogos') {
- return $field;
- }
- if (have_rows('kalliergeies')){
- debug_to_console('Have rows');
- }
- return $field;
- } // end public funciton load_poikilia_field_choices
- public function enqueue_script()
- {
- // enqueue acf extenstion
- // only enqueue the script on the post page where it needs to run
- /* *** THIS IS IMPORTANT
- ACF uses the same scripts as well as the same field identification
- markup (the data-key attribute) if the ACF field group editor
- because of this, if you load and run your custom javascript on
- the field group editor page it can have unintended side effects
- on this page. It is important to alway make sure you're only
- loading scripts where you need them.
- */
- global $post;
- if (!$post ||
- !isset($post->ID) ||
- get_post_type($post->ID) != 'paragogos') {
- return;
- }
- $handle = 'acf-kalliergeies';
- // I'm using this method to set the src because
- // I don't know where this file will be located
- // you should alter this to use the correct fundtions
- // to set the src value to point to the javascript file
- $src = '/'.str_replace(ABSPATH, '', dirname(__FILE__)).'/acf-kalliergeies.js';
- // make this script dependent on acf-input
- $depends = array('acf-input');
- wp_enqueue_script($handle, $src, $depends);
- } // end public function enqueue_script
- public function ajax_load_poikilia_field_choices()
- {
- // this funtion is called by AJAX to load poikilies
- // based on kalliergeia selecteion
- // we can use the acf nonce to verify
- if (!wp_verify_nonce($_POST['nonce'], 'acf_nonce')) {
- die();
- }
- $kalliergeia = 0;
- if (isset($_POST['kalliergeia'])) {
- $kalliergeia = intval($_POST['kalliergeia']);
- }
- $poikilies = $this->get_poikilies($kalliergeia);
- $choices = array();
- foreach ($poikilies as $value => $label) {
- $choices[] = array('value' => $value, 'label' => $label);
- }
- echo json_encode($choices);
- exit;
- } // end public function ajax_load_poikilia_field_choices
- private function get_poikilies($post_id)
- {
- // $post_id is post ID of kalliergeia post
- // get all poikilies related to the kalliergeia
- $args = array(
- 'post_type' => 'poikilia',
- 'posts_per_page' => -1,
- 'orderby' => 'title',
- 'order' => 'ASC',
- 'meta_query' => array(
- array(
- 'key' => 'kalliergeia',
- //'value' => $post_id, // [if acf supports only one value]
- 'value' => '"'.$post_id.'"', // [if acf supports multiple values]
- 'compare' => 'LIKE' // [if acf supports multiple values]
- )
- )
- );
- $query = new WP_Query($args);
- //$choices = array('' => '-- Ποικιλία --');
- $choices = array();
- if (count($query->posts)) {
- // populate choices
- foreach ($query->posts as $post) {
- $choices[$post->ID] = $post->post_title;
- }
- }
- return $choices;
- } // end private function get_poikilies
- } // end class my_acf_extension
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement