Support

Account

Home Forums Backend Issues (wp-admin) WordPress TinyMCE + ACF Field

Unread

WordPress TinyMCE + ACF Field

  • Hi!
    The task – insert field(Post Object) to popup tinymce button and save post id’s on shortcode. How can I do this?
    For example [objects ids=’31,43,65,78′].

    function custom_button_add() {
      if ( !current_user_can( 'edit_posts' ) && !current_user_can( 'edit_pages' ) ) {
        return;
      }
      if ( 'true' == get_user_option( 'rich_editing' ) ) {
        add_filter( 'mce_external_plugins', 'custom_button_add_script' );
        add_filter( 'mce_buttons', 'c_register_mce_button' );
      }
    }
    add_action('admin_head', 'custom_button_add');
    function custom_button_add_script( $plugin_array ) {
      $plugin_array['c_mce_button'] = get_stylesheet_directory_uri() .'/button.js';
      return $plugin_array;
    }
    function c_register_mce_button( $buttons ) {
      array_push( $buttons, 'c_mce_button' );
      return $buttons;
    }
    function get_objects( $object_ids ) {
      $args = array(
      'post_type' => 'objects',
      'post__in'  => explode(',',$objects_ids )
      );
    $objects_list = new WP_Query( $args ); ?>
    
    <?php if ( $objects_list->have_posts() ) : while ( $objects_list->have_posts() ) : $objects_list->the_post();
    ?>
    <?php the_title(); ?>
    <?php endwhile; else: echo 'Не найдено'; endif; ?>
    <?php }
    
    function objects_shortcode( $atts ) {
      ob_start();
      $params = shortcode_atts( array(
      'ids' => '',
      ), $atts );
      $objects_ids = $params['ids'];
      return get_objects( $objects_ids );
      return ob_get_clean();
    }
    add_shortcode ('objects', 'objects_shortcode');
    (function() {
      tinymce.PluginManager.add('c_mce_button', function( editor, url ) {
        editor.addButton( 'c_mce_button', {
          text: 'Objects',
          onclick: function() {
                    editor.windowManager.open( {
                      title: 'Выберите посты',
                      body: [										
                        {
                          type: 'textbox',
                          name: 'ids',
                          label: 'IDS',
                          multiline: true,
                          minWidth: 300,
                          minHeight: 100
                        }
                      ],
                      onsubmit: function( e ) {
                        editor.insertContent( '[objects ids="' + e.data.ids + '"]');
                      }
                    });					
          }
        });
      });
    })();
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.