{"id":10984,"date":"2026-02-13T17:03:10","date_gmt":"2026-02-13T08:03:10","guid":{"rendered":"https:\/\/code-plus.jp\/gp\/?p=10984"},"modified":"2026-02-13T17:03:21","modified_gmt":"2026-02-13T08:03:21","slug":"wordpress-10984","status":"publish","type":"post","link":"https:\/\/code-plus.jp\/gp\/wordpress-10984\/","title":{"rendered":"\u3010WordPress\u3011\u8a18\u4e8b\u5358\u4f4d\u3067\u30ab\u30b9\u30bf\u30e0CSS\/JS\u3092\u8ffd\u8a18\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b"},"content":{"rendered":"<pre><code class=\"language-php\">&lt;?php \/\/\uff08functions.php\uff09\r\n\r\n\/* \u8a18\u4e8b\u5358\u4f4d\u3067\u30ab\u30b9\u30bf\u30e0CSS\/JS\u3092\u8ffd\u8a18\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b\r\n-------------------------------------- *\/\r\n\/\/\u30e1\u30bf\u30dc\u30c3\u30af\u30b9\u767b\u9332\r\nadd_action(&#39;add_meta_boxes&#39;, function () {\r\n    foreach ([&#39;post&#39;, &#39;page&#39;] as $post_type) {\r\n        add_meta_box(&#39;custom_css&#39;, &#39;Custom CSS&#39;, &#39;render_custom_css_meta_box&#39;, $post_type, &#39;normal&#39;, &#39;high&#39;);\r\n        add_meta_box(&#39;custom_js&#39;, &#39;Custom JS&#39;, &#39;render_custom_js_meta_box&#39;, $post_type, &#39;normal&#39;, &#39;high&#39;);\r\n    }\r\n});\r\n\r\nfunction render_custom_css_meta_box($post) {\r\n    wp_nonce_field(&#39;custom_css_nonce_action&#39;, &#39;custom_css_nonce&#39;);\r\n    $value = get_post_meta($post-&gt;ID, &#39;_custom_css&#39;, true);\r\n    echo &#39;&lt;textarea name=&quot;custom_css&quot; rows=&quot;5&quot; style=&quot;width:100%;&quot;&gt;&#39; . esc_textarea($value) . &#39;&lt;\/textarea&gt;&#39;;\r\n}\r\n\r\nfunction render_custom_js_meta_box($post) {\r\n    wp_nonce_field(&#39;custom_js_nonce_action&#39;, &#39;custom_js_nonce&#39;);\r\n    $value = get_post_meta($post-&gt;ID, &#39;_custom_js&#39;, true);\r\n    echo &#39;&lt;textarea name=&quot;custom_js&quot; rows=&quot;5&quot; style=&quot;width:100%;&quot;&gt;&#39; . esc_textarea($value) . &#39;&lt;\/textarea&gt;&#39;;\r\n}\r\n\r\n\/\/\u4fdd\u5b58\u51e6\u7406\r\nadd_action(&#39;save_post&#39;, function ($post_id) {\r\n    if (defined(&#39;DOING_AUTOSAVE&#39;) &amp;&amp; DOING_AUTOSAVE) return;\r\n    if (!current_user_can(&#39;edit_post&#39;, $post_id)) return;\r\n\r\n    \/\/CSS\r\n    if (isset($_POST[&#39;custom_css_nonce&#39;]) &amp;&amp; wp_verify_nonce($_POST[&#39;custom_css_nonce&#39;], &#39;custom_css_nonce_action&#39;)) {\r\n        $css = isset($_POST[&#39;custom_css&#39;]) ? wp_strip_all_tags($_POST[&#39;custom_css&#39;]) : &#39;&#39;;\r\n        update_post_meta($post_id, &#39;_custom_css&#39;, $css);\r\n    }\r\n\r\n    \/\/JS\r\n    if (isset($_POST[&#39;custom_js_nonce&#39;]) &amp;&amp; wp_verify_nonce($_POST[&#39;custom_js_nonce&#39;], &#39;custom_js_nonce_action&#39;)) {\r\n        $js = isset($_POST[&#39;custom_js&#39;]) ? $_POST[&#39;custom_js&#39;] : &#39;&#39;;\r\n        update_post_meta($post_id, &#39;_custom_js&#39;, $js);\r\n    }\r\n});\r\n\r\n\/\/\u30d5\u30ed\u30f3\u30c8\u51fa\u529b\r\nadd_action(&#39;wp_head&#39;, function () {\r\n    if (!is_singular()) return;\r\n    $post_id = get_queried_object_id();\r\n\r\n    $css = get_post_meta($post_id, &#39;_custom_css&#39;, true);\r\n    if ($css) {\r\n        echo &#39;&lt;style&gt;&#39; . wp_strip_all_tags($css) . &#39;&lt;\/style&gt;&#39;;\r\n    }\r\n\r\n    $js = get_post_meta($post_id, &#39;_custom_js&#39;, true);\r\n    if ($js) {\r\n        echo &#39;&lt;script&gt;&#39; . $js . &#39;&lt;\/script&gt;&#39;;\r\n    }\r\n});\r\n<\/code><\/pre>","protected":false},"excerpt":{"rendered":"\u3010WordPress\u3011\u8a18\u4e8b\u5358\u4f4d\u3067\u30ab\u30b9\u30bf\u30e0CSS\/JS\u3092\u8ffd\u8a18\u3067\u304d\u308b\u3088\u3046\u306b\u3059\u308b","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"sns_share_botton_hide":"","vkExUnit_sns_title":"","_vk_print_noindex":"","footnotes":"","vk-ltc-link":"","vk-ltc-target":"0"},"categories":[13],"tags":[],"class_list":["post-10984","post","type-post","status-publish","format-standard","category-wordpress"],"veu_head_title_object":{"title":"","add_site_title":""},"_links":{"self":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts\/10984","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=10984"}],"version-history":[{"count":0,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/posts\/10984\/revisions"}],"wp:attachment":[{"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/media?parent=10984"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/categories?post=10984"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-plus.jp\/gp\/wp-json\/wp\/v2\/tags?post=10984"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}