Home › Forums › Feature Requests › Shortcodes Plugin › Reply To: Shortcodes Plugin
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
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.