如何将源代码从IDE直接粘贴到帖子中?

时间:2016-08-18 作者:dan9vu

我试过了preformatted 按钮Paste as text 按钮或Text tab元素粘贴源代码之前,WordPress总是剥离缩进选项卡并中断代码格式。

例如,源代码:

final class HtmlAttr
{
  /**
   * Constructor
   *
   * @param  string  $filter  A filter hook to modify the attribute(s).
   */
  function __construct(array $attrs, $filter = \'\')
  {
    // Something...
  }

  /**
   * Render attribute(s)
   */
  function __toString()
  {
    // Something...
  }
}
将成为:

final class HtmlAttr
{
 /**
 * Constructor
 *
 * @param string $filter A filter hook to modify the attribute(s).
 */
 function __construct(array $attrs, $filter = \'\')
 {
 // Something...
 }

 /**
 * Render attribute(s)
 */
 function __toString()
 {
 // Something...
 }
}
我使用的是Atom IDE,无法找到将IDE中的源代码直接粘贴到WordPress TinyMCE编辑器中的正确方法。非常感谢您的帮助!

2 个回复
最合适的回答,由SO网友:dan9vu 整理而成

在努力添加后pre 元素,我决定在TinyMCE编辑器上创建一个快捷按钮:

这是我的父主题中的按钮类和挂钩:

/**
 * EmbedSourceCodeButton
 *
 * @see  https://codex.wordpress.org/TinyMCE_Custom_Buttons
 */
final class EmbedSourceCodeButton
{
  /**
   * Name
   */
  const NAME = \'embedSourceCodeButton\';

  /**
   * Constructor
   */
  function init()
  {
    if (\'post\' !== $GLOBALS[\'current_screen\']->post_type) return;

    add_action(\'mce_buttons\', [$this, \'addButton\']);
    add_action(\'mce_external_plugins\', [$this, \'addPlugin\']);
    add_action(\'admin_enqueue_scripts\', [$this, \'localizeLabels\'], 10, 0);
  }

  /**
   * Localize labels
   */
  function localizeLabels()
  {
    wp_localize_script(\'jquery-core\', self::NAME, [
      \'title\'        => __(\'Embed Source Code\', \'textdomain\'),
      \'listboxLabel\' => __(\'Language\', \'textdomain\'),
      \'textboxLabel\' => __(\'Source code\', \'textdomain\')
    ]);
  }

  /**
   * Add this button to TinyMCE
   *
   * @see  https://core.trac.wordpress.org/browser/tags/4.6/src/wp-includes/class-wp-editor.php#L624
   *
   * @return  array  $buttons
   */
  function addButton(array $buttons)
  {
    array_push($buttons, self::NAME);

    return $buttons;
  }

  /**
   * Add this plugin
   *
   * @see  https://core.trac.wordpress.org/browser/tags/4.6/src/wp-includes/class-wp-editor.php#L392
   *
   * @return  array  $plugins
   */
  function addPlugin(array $plugins)
  {
    $x = SCRIPT_DEBUG ? \'\' : \'.min\';

    if ( !isset($plugins[self::NAME]) )
      $plugins[self::NAME] = get_template_directory_uri() . \'/assets/js/button\' . $x . \'.js\';

    return $plugins;
  }
}
add_action(\'load-post.php\', [new EmbedSourceCodeButton, \'init\']);
add_action(\'load-post-new.php\', [new EmbedSourceCodeButton, \'init\']);
这是button.js 在里面asset/js 我的主题文件夹:

(function() {
  tinymce.PluginManager.add(\'embedSourceCodeButton\', function(editor, url) {
    editor.addButton(\'embedSourceCodeButton\', {
      text: false,
      icon: \'wp_code\',
      onclick: function() {
        editor.windowManager.open({
          title: embedSourceCodeButton.title,
          body: [
            { // Unnecessary yet but may be helpful in the future.
              name: \'language\',
              type: \'listbox\',
              label: embedSourceCodeButton.listboxLabel,
              values: [
                {
                  text: \'PHP\',
                  value: \'php\'
                },
                {
                  text: \'CSS\',
                  value: \'css\'
                },
                {
                  text: \'HTML\',
                  value: \'html\'
                },
                {
                  text: \'Javascript\',
                  value: \'javascript\'
                }
              ]
            },
            {
              name: \'code_block\',
              type: \'textbox\',
              multiline: true,
              label: embedSourceCodeButton.textboxLabel
            }
          ],
          onsubmit: function(e) {
            e.stopPropagation();
            var entities = {
              \'&\': \'&\',
              \'<\': \'&lt;\',
              ">": \'&gt;\',
              \'"\': \'&quot;\',
              "\'": \'&#39;\',
              \'/\': \'&#x2F;\'
            };
            // Encode HTML special chars before inserting into editor.
            var codeBlock = e.data.code_block.replace(/&|<|>|"|\'|\\//gm, function(s) {
              return entities[s];
            });
            editor.insertContent(\'<pre data-lang="\' + e.data.language + \'"><code>\' + codeBlock + \'</code></pre>\');
          }
        });
      }
    });
  });
})();
希望它能帮助像我这样懒惰的人

SO网友:Rik Dekyvere

你试过其他编辑器吗?

您可以尝试使用CKEeditor,根据一些人的说法,它不会影响代码缩进。

https://wordpress.org/plugins/ckeditor-for-wordpress/

相关推荐

编辑主题中的formatting.php,这样它就不会被覆盖

我想删除wpautop的某些部分,但保留其余部分。我在wp中找到了包含/格式。函数wpautop中的php:$pee = preg_replace(\'|<p>\\s*</p>|\', \'\', $pee); // under certain strange conditions it could create a P of entirely whitespace &; $pee = preg_replace(\'|(?<!<br />)\\s*