Hi John,
Thanks for your reply! I figured out that there was something wrong with initializing the shortcodes button. Everything is working fine now and this is the optimized code:
add_action( 'after_setup_theme', 'shortcodes_button_setup' );
if ( ! function_exists( 'shortcodes_button_setup' ) ) {
function shortcodes_button_setup() {
add_action( 'init', 'shortcodes_button' );
}
}
if ( ! function_exists( 'shortcodes_button' ) ) {
function shortcodes_button() {
if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
return;
}
if ( get_user_option( 'rich_editing' ) !== 'true' ) {
return;
}
add_filter( 'mce_external_plugins', 'add_shortcodes_button' );
add_filter( 'mce_buttons', 'register_shortcodes_button' );
}
}
if ( ! function_exists( 'add_shortcodes_button' ) ) {
function add_shortcodes_button( $plugin_array ) {
$plugin_array['shortcodes_button'] = get_template_directory_uri().'/includes/shortcodes/js/shortcodes.js';
return $plugin_array;
}
}
if ( ! function_exists( 'register_shortcodes_button' ) ) {
function register_shortcodes_button( $buttons ) {
array_push( $buttons, 'shortcodes_button' );
return $buttons;
}
}
Hi John, I added the following code within functions.php and shortcodes.js:
functions.php
add_action('admin_head', 'add_shortcodes_button');
function add_shortcodes_button() {
global $typenow;
if ( !current_user_can('edit_posts') && !current_user_can('edit_pages') ) {
return;
}
if ( get_user_option('rich_editing') == 'true') {
add_filter("mce_external_plugins", "add_tinymce_plugin");
add_filter('mce_buttons', 'register_shortcodes_button');
}
}
function add_tinymce_plugin($plugin_array) {
$plugin_array['shortcodes_button'] = esc_url( get_template_directory_uri() ) . '/includes/shortcodes/js/shortcodes.js';
return $plugin_array;
}
function register_shortcodes_button($buttons) {
array_push($buttons, "shortcodes_button");
return $buttons;
}
shortcodes.js
(function() {
tinymce.PluginManager.add('shortcodes_button', function( editor, url ) {
editor.addButton( 'shortcodes_button', {
title: 'Element toevoegen',
type: 'menubutton',
icon: 'plus',
menu: [
{
text: 'Button',
onclick: function() {
editor.windowManager.open( {
title: 'Button',
icon: 'plus',
body: [{
type: 'listbox',
name: 'size',
label: 'Formaat',
'values': [
{text: 'Klein', value: 'small'},
{text: 'Middel', value: 'medium'},
{text: 'Groot', value: 'large'},
]
},
{
type: 'listbox',
name: 'color',
label: 'Kleur',
'values': [
{text: 'Blauw', value: 'default'},
{text: 'Oranje', value: 'orange'},
{text: 'Groen', value: 'green'},
]
},
{
type: 'textbox',
name: 'text',
label: 'Tekst'
},
{
type: 'textbox',
name: 'link',
label: 'Link (URL)'
},
{
type: 'listbox',
name: 'target',
label: 'Link openen in',
'values': [
{text: 'Hetzelfde scherm', value: '_self'},
{text: 'Nieuw tabblad', value: '_blank'},
]
},
{
type: 'textbox',
name: 'cssclass',
label: 'CSS class'
},
{
type: 'listbox',
name: 'ga_event',
label: 'GA gebeurtenis volgen?',
'values': [
{text: 'Nee', value: 'no'},
{text: 'Ja', value: 'yes'},
]
},
{
type: 'textbox',
name: 'ga_category',
label: 'GA gebeurteniscategorie'
},
{
type: 'textbox',
name: 'ga_action',
label: 'GA gebeurtenisactie'
},
{
type: 'textbox',
name: 'ga_label',
label: 'GA gebeurtenislabel'
}],
onsubmit: function( e ) {
editor.insertContent( '[button size="' + e.data.size + '" color="' + e.data.color + '" link="' + e.data.link + '" target="' + e.data.target + '" class="' + e.data.cssclass + '" ga_event="' + e.data.ga_event + '" ga_category="' + e.data.ga_category + '" ga_action="' + e.data.ga_action + '" ga_label="' + e.data.ga_label + '"]' + e.data.text + '[/button]');
}
});
}
}
]
});
});
})();
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!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.
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.