Support

Account

Forum Replies Created

  • I found this tutorial today and figured I would take a shot at writing what I needed. “http://www.wpactions.com/1028/how-to-create-dropdown-list-button-in-tinymce-toolbar/”

    I was successful in creating a simple dropdown that would allow you to pick from ACF from your options page. I have only tested it with the text field since that’s all I am using it for but I believe it should work for quite a few fields.

    Plugin File (acf-dropdown-shortcode.php)

    <?php
    /*
    Plugin Name: Advanced Custom Fields: Dropdown Shortcode
    Plugin URI: http://www.andresthegiant.com
    Description: Create a dropdown list of Advanced Custom Fields in the tinymce toolbar
    Author: Andres The Gaint
    Version: 0.1
    Author URI: http://www.andresthegiant.com
    */
    
    include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    if ( is_plugin_active( 'advanced-custom-fields/acf.php' ) ) {
    
    	add_action('admin_head', 'my_print_shortcodes_in_js');
    	add_action('admin_head', 'my_add_tinymce');
    	function my_print_shortcodes_in_js(){
    			
    		?>		
    		<script type="text/javascript">			
    			var my_shortcodes = [
    				
    				<?php 
    					$fields = get_fields('option');
    					if( $fields )
    					{
    						foreach( $fields as $field_name => $value )
    						{
    							// get_field_object( $field_name, $post_id, $options )
    							// - $value has already been loaded for us, no point to load it again in the get_field_object function
    							$field = get_field_object($field_name, false, array('load_value' => false));
    					 
    								echo "'" . $field_name . "' , ";
    						}
    					}
    				?>
    			
    			];
    		</script>
    		<?php
    	}
    	
    	
    	function my_add_tinymce() {
    		// global $typenow;
    		// if(!in_array($typenow,array('post','page')))
    		// 	return ;
    		add_filter('mce_external_plugins', 'my_add_tinymce_plugin');
    		add_filter('mce_buttons', 'my_add_tinymce_button');
    	}
    	
    	function my_add_tinymce_plugin($plugin_array) {
    		$plugin_array['acf_drop'] =	plugins_url( 'dropdown-shortcode.js',__FILE__ );
    	
    		return $plugin_array;
    	}
    	
    	function my_add_tinymce_button($buttons) {
    		array_push($buttons, 'acf_drop');
    		return $buttons;
    	}
    }
    
    ?>

    Plugin JS File (dropdown-shortcode.js)

    (function() {
    	
    // Makes ACF names readable 
    function unSlugify(Text)
    {
    	return Text
    		.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();})
    		.replace(/_/g, " ")
    		;
    }	
    	
    tinymce.create('tinymce.plugins.acf_drop', {
     
    init : function(ed, url) {
    },
    createControl : function(n, cm) {
     
    if(n=='acf_drop'){
    var mlb = cm.createListBox('acf_drop', {
    title : 'ACF Options',
    onselect : function(v) {
    if(tinyMCE.activeEditor.selection.getContent() == ''){
    tinyMCE.activeEditor.selection.setContent( v )
    }
    }
    });
     
    for(var i in my_shortcodes)
    mlb.add(unSlugify(my_shortcodes[i]), '[acf field="' + my_shortcodes[i] + '" post_id="option"]');
     
    return mlb;
    }
    return null;
    }
     
     
    });
    tinymce.PluginManager.add('acf_drop', tinymce.plugins.acf_drop);
    })();

    Hope this can help someone else

  • I am actually thinking of working on something similar. A button in the wysiwyg editor that would show you all the custom fields under your options page.

    So you could easily enter global values without having to know your how to write the shortcode.

  • Thanks for the tip @elliot. I was able to successfully clone the User field and change the value output for each choice with the company custom field added to each user. This will make it easier for the website users to develop the foodchain.

    What are your thoughts on adding users to the “Relationship” field? I think it would be nicer then my drop down in a repeater solution. I tried to work it out on my end but the Relationship field was pretty heavy. Just a thought, I don’t need it but it would be neat to be able to use in the future.

  • Hey @elliot you are right on with my first problem. I think I meant to use a true of false field there. Not sure how I missed that :(.

    Thanks again for your help. I hope bringing the second issue to your attention was somewhat helpful :D.

  • Hey @elliot, just want to confirm that you received my credentials. I completely understand your busy, just hope when you get a chance you can solve this issue :D.

  • This reply has been marked as private.
  • @elliot thanks for getting back to me. Unfortunately this is not the case for either of these problems. I do not have another field with the same value, and I do ot have “Save / Load terms to post” selected. I am happy to share my login credentials if you want to take a peak. Do you have a way of doing that?

Viewing 7 posts - 1 through 7 (of 7 total)