{"id":6679,"date":"2024-10-19T00:59:41","date_gmt":"2024-10-18T15:59:41","guid":{"rendered":"https:\/\/code-plus.jp\/gp\/?p=6679"},"modified":"2026-02-25T11:12:36","modified_gmt":"2026-02-25T02:12:36","slug":"post-6679","status":"publish","type":"post","link":"https:\/\/code-plus.jp\/gp\/post-6679\/","title":{"rendered":"WordPress\u306e\u30d3\u30b8\u30e5\u30a2\u30eb\u30a8\u30c7\u30a3\u30bf\u30fc(TinyMCE)\u306b\u8907\u6570\u306e\u81ea\u4f5c\u30ab\u30b9\u30bf\u30e0\u30dc\u30bf\u30f3\u3092\u8ffd\u52a0\u3059\u308b\u65b9\u6cd5"},"content":{"rendered":"<p>WordPress\u306e\u30d3\u30b8\u30e5\u30a2\u30eb\u30a8\u30c7\u30a3\u30bf\u30fc\u306b\u30ab\u30b9\u30bf\u30e0\u30dc\u30bf\u30f3\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u3001\u30b3\u30f3\u30c6\u30f3\u30c4\u4f5c\u6210\u306e\u52b9\u7387\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001\u8907\u6570\u306e\u30ab\u30b9\u30bf\u30e0\u30dc\u30bf\u30f3\u3092\u8ffd\u52a0\u3059\u308b\u65b9\u6cd5\u3068\u3001\u30ea\u30f3\u30af(a)\u30bf\u30b0\u306e\u5c5e\u6027\u3092\u4fdd\u6301\u3059\u308b\u65b9\u6cd5\u306b\u3064\u3044\u3066\u3001\u521d\u5fc3\u8005\u306b\u3082\u308f\u304b\u308a\u3084\u3059\u304f\u89e3\u8aac\u3057\u307e\u3059\u3002<\/p>\r\n<p>&nbsp;<\/p>\r\n<div>\r\n<div><b class=\"fw-600\">WordPress\u306e\u30d3\u30b8\u30e5\u30a2\u30eb\u30a8\u30c7\u30a3\u30bf\u30fc(TinyMCE)\u306b\u8907\u6570\u306e\u81ea\u4f5c\u30ab\u30b9\u30bf\u30e0\u30dc\u30bf\u30f3\u3092\u8ffd\u52a0\u3059\u308b<\/b><\/div>\r\n<cite><a href=\"https:\/\/gist.github.com\/sarap422\/b2b2b13580b092c610a0d929d50ba288\" target=\"_blank\" rel=\"noopener\">https:\/\/gist.github.com\/sarap422\/b2b2b13580b092c610a0d929d50ba288<\/a><\/cite>\r\n<\/div>\r\n<h2>1. \u5fc5\u8981\u306a\u30d5\u30a1\u30a4\u30eb<\/h2>\r\n<p>\u30ab\u30b9\u30bf\u30e0\u30dc\u30bf\u30f3\u3092\u8ffd\u52a0\u3059\u308b\u306b\u306f\u3001\u4ee5\u4e0b\u306e\u30d5\u30a1\u30a4\u30eb\u304c\u5fc5\u8981\u3067\u3059\uff1a<\/p>\r\n<ol style=\"list-style-type: decimal;\">\r\n<li>\u30dc\u30bf\u30f3\u30a2\u30a4\u30b3\u30f3\u7528\u306ePNG\u30d5\u30a1\u30a4\u30eb\uff08\u30b5\u30a4\u30ba\uff1a20x20\u30d4\u30af\u30bb\u30eb\uff09<\/li>\r\n<li>JavaScript\u30d5\u30a1\u30a4\u30eb\uff08\u4f8b\uff1a<code>swpbtn-shortcode.js<\/code>\uff09<\/li>\r\n<li>PHP\u30d5\u30a1\u30a4\u30eb\uff08\u4f8b\uff1a<code>swpbtn-shortcode.php<\/code>\uff09<\/li>\r\n<\/ol>\r\n<h2>2. JavaScript\u30d5\u30a1\u30a4\u30eb\u306e\u4f5c\u6210<\/h2>\r\n<p><code>swpbtn-shortcode.js<\/code>\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u69cb\u9020\u3067\u8a18\u8ff0\u3057\u307e\u3059\uff1a<\/p>\r\n<pre><code class=\"language-javascript\">(function () {\r\n  const enclosePlugins = [\r\n    \/\/ \u3053\u3053\u306b\u8907\u6570\u306e\u30dc\u30bf\u30f3\u8a2d\u5b9a\u3092\u8ffd\u52a0\r\n  ];\r\n\r\n  const componentPlugins = [\r\n    \/\/ \u3053\u3053\u306b\u8907\u6570\u306e\u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u30dc\u30bf\u30f3\u8a2d\u5b9a\u3092\u8ffd\u52a0\r\n  ];\r\n\r\n  function createEnclosePlugin(plugin, index) {\r\n    \/\/ \u30dc\u30bf\u30f3\u4f5c\u6210\u306e\u30ed\u30b8\u30c3\u30af\r\n  }\r\n\r\n  function createComponentPlugin(plugin, index) {\r\n    \/\/ \u30b3\u30f3\u30dd\u30fc\u30cd\u30f3\u30c8\u30dc\u30bf\u30f3\u4f5c\u6210\u306e\u30ed\u30b8\u30c3\u30af\r\n  }\r\n\r\n  enclosePlugins.forEach(createEnclosePlugin);\r\n  componentPlugins.forEach(createComponentPlugin);\r\n})();\r\n<\/code><\/pre>\r\n<h3>\u91cd\u8981\u30dd\u30a4\u30f3\u30c8\uff1a\u8907\u6570\u30dc\u30bf\u30f3\u306e\u8ffd\u52a0<\/h3>\r\n<p>\u8907\u6570\u306e\u30dc\u30bf\u30f3\u3092\u8ffd\u52a0\u3059\u308b\u306b\u306f\u3001<code>enclosePlugins<\/code>\u3068<code>componentPlugins<\/code>\u914d\u5217\u306b\u5404\u30dc\u30bf\u30f3\u306e\u8a2d\u5b9a\u3092\u8ffd\u52a0\u3057\u307e\u3059\u3002\u4f8b\u3048\u3070\uff1a<\/p>\r\n<pre><code class=\"language-javascript\">const enclosePlugins = [\r\n  {\r\n    name: 'bold',\r\n    title: '\u592a\u5b57',\r\n    icon: 'format-bold.svg',\r\n    tag: 'b',\r\n    class: 'bold'\r\n  },\r\n  {\r\n    name: 'italic',\r\n    title: '\u659c\u4f53',\r\n    icon: 'format-italic.svg',\r\n    tag: 'i',\r\n    class: 'italic'\r\n  },\r\n  \/\/ \u4ed6\u306e\u30dc\u30bf\u30f3\u3082\u540c\u69d8\u306b\u8ffd\u52a0\r\n];\r\n<\/code><\/pre>\r\n<h3>\u91cd\u8981\u30dd\u30a4\u30f3\u30c8\uff1a\u30ea\u30f3\u30af\u5c5e\u6027\u306e\u4fdd\u6301<\/h3>\r\n<p>\u30ea\u30f3\u30af(a)\u30bf\u30b0\u306ehref\u3001target\u3001rel\u5c5e\u6027\u3092\u4fdd\u6301\u3059\u308b\u306b\u306f\u3001<code>createEnclosePlugin<\/code>\u95a2\u6570\u5185\u3067\u7279\u5225\u306a\u51e6\u7406\u3092\u884c\u3044\u307e\u3059\uff1a<\/p>\r\n<pre><code class=\"language-javascript\">function createEnclosePlugin(plugin, index) {\r\n  tinymce.PluginManager.add(`swpquote${index + 1}`, function (editor, url) {\r\n    editor.addButton(`swpquote${index + 1}`, {\r\n      title: plugin.title,\r\n      cmd: `swpquote${index + 1}`,\r\n      image: url + '\/icons\/' + plugin.icon,\r\n    });\r\n\r\n    editor.addCommand(`swpquote${index + 1}`, function () {\r\n      var selected_text = editor.selection.getContent({ 'format': 'html' });\r\n      if (selected_text.length === 0) {\r\n        alert('\u30c6\u30ad\u30b9\u30c8\u3092\u9078\u629e\u3057\u3066\u304f\u3060\u3055\u3044');\r\n        return;\r\n      }\r\n\r\n      var selectedNode = editor.selection.getNode();\r\n      var isLink = selectedNode.nodeName.toLowerCase() === 'a';\r\n\r\n      if (isLink &amp;&amp; (plugin.tag === 'a' || plugin.class.includes('link'))) {\r\n        \/\/ \u30ea\u30f3\u30af\u306e\u5834\u5408\u3001\u65e2\u5b58\u306e\u5c5e\u6027\u3092\u4fdd\u6301\u3057\u3064\u3064\u30af\u30e9\u30b9\u3092\u8ffd\u52a0\r\n        var existingClass = selectedNode.getAttribute('class') || '';\r\n        var newClass = existingClass ? existingClass + ' ' + plugin.class : plugin.class;\r\n        \r\n        editor.dom.setAttribs(selectedNode, {\r\n          class: newClass\r\n        });\r\n      } else {\r\n        \/\/ \u901a\u5e38\u306e\u51e6\u7406\r\n        var open_column = `&lt;${plugin.tag}${plugin.class ? ` class=\"${plugin.class}\"` : ''}${plugin.style ? ` style=\"${plugin.style}\"` : ''}&gt;`;\r\n        var close_column = `&lt;\/${plugin.tag}&gt;`;\r\n\r\n        if (plugin.innerTag) {\r\n          open_column += `&lt;${plugin.innerTag}${plugin.innerClass ? ` class=\"${plugin.innerClass}\"` : ''}&gt;`;\r\n          close_column = `&lt;\/${plugin.innerTag}&gt;` + close_column;\r\n        }\r\n\r\n        var return_text = open_column + selected_text + close_column;\r\n        editor.execCommand('mceReplaceContent', false, return_text);\r\n      }\r\n    });\r\n  });\r\n}\r\n<\/code><\/pre>\r\n<p>\u3053\u306e\u65b9\u6cd5\u306b\u3088\u308a\u3001\u30ea\u30f3\u30af\u306e\u65e2\u5b58\u306e\u5c5e\u6027\u3092\u4fdd\u6301\u3057\u306a\u304c\u3089\u65b0\u3057\u3044\u30af\u30e9\u30b9\u3092\u8ffd\u52a0\u3067\u304d\u307e\u3059\u3002<\/p>\r\n<h2>3. PHP\u30d5\u30a1\u30a4\u30eb\u306e\u4f5c\u6210<\/h2>\r\n<p><code>swpbtn-shortcode.php<\/code>\u30d5\u30a1\u30a4\u30eb\u3092\u4f5c\u6210\u3057\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u3092\u8a18\u8ff0\u3057\u307e\u3059\uff1a<\/p>\r\n<pre><code class=\"language-php\">&lt;?php\r\nadd_action('after_setup_theme', 'swp1_theme_setup');\r\nif (!function_exists('swp1_theme_setup')) {\r\n  function swp1_theme_setup() {\r\n    add_action('init', 'swp1_buttons');\r\n  }\r\n}\r\n\r\nif (!function_exists('swp1_buttons')) {\r\n  function swp1_buttons() {\r\n    if (!current_user_can('edit_posts') &amp;&amp; !current_user_can('edit_pages')) {\r\n      return;\r\n    }\r\n    if (get_user_option('rich_editing') !== 'true') {\r\n      return;\r\n    }\r\n    add_filter('mce_external_plugins', 'swp1_add_buttons');\r\n    add_filter('mce_buttons_2', 'swp1_register_buttons'); \/\/ 2\u6bb5\u76ee\u306b\u8868\u793a\r\n  }\r\n}\r\n\r\nif (!function_exists('swp1_add_buttons')) {\r\n  function swp1_add_buttons($plugin_array) {\r\n    $plugin_array['swp_custom_buttons'] = get_theme_file_uri() . '\/path\/to\/swpbtn-shortcode.js';\r\n    return $plugin_array;\r\n  }\r\n}\r\n\r\nif (!function_exists('swp1_register_buttons')) {\r\n  function swp1_register_buttons($buttons) {\r\n    $custom_buttons = array('swp_custom_buttons');\r\n    return array_merge($buttons, $custom_buttons);\r\n  }\r\n}\r\n<\/code><\/pre>\r\n<h2>4. functions.php\u306e\u7de8\u96c6<\/h2>\r\n<p>\u30c6\u30fc\u30de\u306efunctions.php\u306b\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\u3057\u307e\u3059\uff1a<\/p>\r\n<pre><code class=\"language-php\">include \"path\/to\/swpbtn-shortcode.php\";\r\n<\/code><\/pre>\r\n<h2>5. \u30d5\u30a1\u30a4\u30eb\u306e\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9<\/h2>\r\n<ol style=\"list-style-type: decimal;\">\r\n<li><code>swpbtn-shortcode.php<\/code>\u3068\u7de8\u96c6\u3057\u305f<code>functions.php<\/code>\u3092\u30c6\u30fc\u30de\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9<\/li>\r\n<li><code>swpbtn-shortcode.js<\/code>\u3068\u30a2\u30a4\u30b3\u30f3\u7528\u306ePNG\u30d5\u30a1\u30a4\u30eb\u3092\u9069\u5207\u306a\u30c7\u30a3\u30ec\u30af\u30c8\u30ea\uff08\u4f8b\uff1a<code>\/assets\/js\/<\/code>\uff09\u306b\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9<\/li>\r\n<\/ol>\r\n<h2>\u307e\u3068\u3081<\/h2>\r\n<p>\u3053\u306e\u65b9\u6cd5\u3092\u4f7f\u7528\u3059\u308b\u3053\u3068\u3067\u3001WordPress\u306e\u30d3\u30b8\u30e5\u30a2\u30eb\u30a8\u30c7\u30a3\u30bf\u30fc\u306b\u8907\u6570\u306e\u30ab\u30b9\u30bf\u30e0\u30dc\u30bf\u30f3\u3092\u8ffd\u52a0\u3057\u3001\u30ea\u30f3\u30af\u30bf\u30b0\u306e\u5c5e\u6027\u3092\u4fdd\u6301\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u521d\u5fc3\u8005\u306e\u65b9\u3067\u3082\u3001step by step\u3067\u9032\u3081\u3066\u3044\u3051\u3070\u3001\u7c21\u5358\u306b\u30ab\u30b9\u30bf\u30e0\u30dc\u30bf\u30f3\u3092\u5b9f\u88c5\u3067\u304d\u308b\u306f\u305a\u3067\u3059\u3002<\/p>\r\n<p>\u30ab\u30b9\u30bf\u30e0\u30dc\u30bf\u30f3\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u3001\u30b3\u30f3\u30c6\u30f3\u30c4\u4f5c\u6210\u306e\u52b9\u7387\u304c\u5927\u5e45\u306b\u5411\u4e0a\u3057\u3001\u4e00\u8cab\u6027\u306e\u3042\u308b\u30b9\u30bf\u30a4\u30eb\u3092\u7c21\u5358\u306b\u9069\u7528\u3067\u304d\u308b\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002\u305c\u3072\u3001\u81ea\u5206\u306e\u30cb\u30fc\u30ba\u306b\u5408\u308f\u305b\u3066\u30ab\u30b9\u30bf\u30de\u30a4\u30ba\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>","protected":false},"excerpt":{"rendered":"WordPress\u306e\u30d3\u30b8\u30e5\u30a2\u30eb\u30a8\u30c7\u30a3\u30bf\u30fc\u306b\u30ab\u30b9\u30bf\u30e0\u30dc\u30bf\u30f3\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u3067\u3001\u30b3\u30f3\u30c6\u30f3\u30c4\u4f5c\u6210\u306e\u52b9\u7387\u3092\u5927\u5e45\u306b\u5411\u4e0a\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\u3053\u306e\u8a18\u4e8b\u3067\u306f\u3001\u8907\u6570\u306e\u30ab\u30b9\u30bf\u30e0\u30dc\u30bf\u30f3\u3092\u8ffd\u52a0\u3059\u308b\u65b9\u6cd5\u3068\u3001\u30ea\u30f3\u30af(a)\u30bf\u30b0\u306e\u5c5e\u6027\u3092\u4fdd\u6301\u3059\u308b\u65b9\u6cd5\u306b\u3064[...]","protected":false},"author":1,"featured_media":7295,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","footnotes":"","vk-ltc-link":"","vk-ltc-target":"0"},"categories":[13],"tags":[],"group":[],"class_list":["post-6679","post","type-post","status-publish","format-standard","has-post-thumbnail","category-wordpress"],"acf":[],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts\/6679","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/comments?post=6679"}],"version-history":[{"count":0,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts\/6679\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/media\/7295"}],"wp:attachment":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/media?parent=6679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/categories?post=6679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/tags?post=6679"},{"taxonomy":"group","embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/group?post=6679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}