Support

Account

Forum Replies Created

  • I found a solution to my problem by adding the custom button another way :

    In functions.php :

    // Prevent empty tags from being removed by TinyMCE
    add_filter('tiny_mce_before_init', 'add_my_options');
    function add_my_options($opt) {
        $opt['extended_valid_elements'] = '*[*]';
        return $opt;
    }
    
    add_action( 'after_setup_theme', 'arch_theme_setup' );
    if ( ! function_exists( 'arch_theme_setup' ) ) {
      function arch_theme_setup(){
        add_action( 'init', 'arch_buttons' );
      }
    }
    
    if ( ! function_exists( 'arch_buttons' ) ) {
      function arch_buttons() {
        if ( ! current_user_can( 'edit_posts' ) && ! current_user_can( 'edit_pages' ) ) {
              return;
          }
          add_filter( 'mce_external_plugins', 'arch_add_buttons' );
          add_filter( 'mce_buttons', 'arch_register_buttons' );
      }
    }
    if ( ! function_exists( 'arch_add_buttons' ) ) {
      function arch_add_buttons( $plugin_array ) {
          $plugin_array['archbtn'] = get_template_directory_uri().'/lib/blocks/blockscript/custom-button.js';
          return $plugin_array;
      }
    }
    if ( ! function_exists( 'arch_register_buttons' ) ) {
      function arch_register_buttons( $buttons ) {
          array_push( $buttons, 'archbtn' );
          return $buttons;
      }
    }

    custom-button.js :

    (function() {
        tinymce.PluginManager.add('archbtn', function( editor, url ) {
            editor.addButton( 'archbtn', {
                type: 'button',
                text: 'Ajouter un bouton',
                onclick: function() {
                    editor.insertContent('test');
                }
            });
        });
    })();

    I still plan on using acf js api for this kind of thing because i found it to be cleaner. But i still don’t understand why my database entry in postmeta table got duplicated everytime tinymce was loaded.

    So if anybody knows why it is doing this, i’m very curious and want to understand 🙂 !

    Have a nice day

Viewing 1 post (of 1 total)