{"id":530276,"date":"2021-01-15T11:59:16","date_gmt":"2021-01-15T03:59:16","guid":{"rendered":"https:\/\/loyseo.com\/tutorial\/10%e5%80%8bwordpress%e5%b8%b8%e7%94%a8%e4%bb%a3%e7%a2%bc%e6%ae%b5%ef%bc%88code-snippets%ef%bc%89\/"},"modified":"2024-02-19T17:59:16","modified_gmt":"2024-02-19T09:59:16","slug":"code-snippets-for-wordpress","status":"publish","type":"tutorial","link":"https:\/\/loyseo.com\/zh-hant\/tutorial\/code-snippets-for-wordpress\/","title":{"rendered":"10\u500bWordPress\u5e38\u7528\u4ee3\u78bc\u6bb5\uff08Code Snippets\uff09"},"content":{"rendered":"\n<p>\u904b\u7528<a href=\"https:\/\/loyseo.com\/code-snippet\/\" class=\"rank-math-link\">\u4ee3\u78bc\u6bb5\uff08Code Snippets\uff09\u5916\u639b\u7ba1\u7406\u4ee3\u78bc<\/a>\uff0c\u53ef\u4ee5\u4e0d\u7528\u984d\u5916\u5b89\u88dd\u66f4\u591a\u5916\u639b\uff0c\u4f86\u89e3\u6c7aWordPress\u67b6\u8a2d\u7db2\u7ad9\u904e\u7a0b\u4e2d\u7684\u4e00\u4e9b\u5e38\u898b\u529f\u80fd\u9700\u6c42\uff0c\u8b6c\u5982<a href=\"https:\/\/loyseo.com\/zh-hant\/tutorial\/how-to-install-google-analytics-in-wordpress\/\" class=\"rank-math-link\">\u5b89\u88ddGoogle analytics\u8ddf\u8e64\u4ee3\u78bc<\/a>\u3002 \u4e0b\u6587\u4e2d\u8a18\u9304\u4e86\u6211\u5728<a href=\"\/\" class=\"rank-math-link\">\u5916\u8cbf\u67b6\u8a2d\u7db2\u7ad9<\/a>\u548c\u500b\u4eba\u535a\u5ba2\u4e2d\u5e38\u7528\u5230\u7684\u4ee3\u78bc\u6bb5\u3002<\/p>\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u5728WordPress\u6587\u7ae0\/\u9801\u9762\u4e0a\u7981\u6b62\u8f09\u5165WooCommerce .js\uff08javascript\uff09\u548c.css\u6a94\uff1f<\/h2>\n\n<p>woocommerce\u6703\u5728\u6bcf\u500b\u9801\u9762\u90fd\u9ed8\u8a8d\u8f09\u5165\u5e7e\u500bcss\u548cjs\uff0c\u9019\u5c0d\u7db2\u7ad9\u901f\u5ea6\u662f\u6709\u5f71\u97ff\u7684\uff0c\u5c07\u4e0b\u9762\u9019\u6bb5\u4ee3\u78bc\uff0c\u5728<a href=\"https:\/\/loyseo.com\/zh-hant\/plugins\/code-snippets\/\" class=\"rank-math-link\">code snippets<\/a>\u5916\u639b\u7a0b\u5f0f\u4e2d<a href=\"https:\/\/loyseo.com\/code-snippet\/\" class=\"rank-math-link\">\u6dfb\u52a0\u4e00\u500b\u65b0snippet<\/a>\uff0c\u80fd\u5920\u5be6\u73fe\u5728\u9664\u4e86\u8cfc\u7269\u8eca\u3001\u7d50\u7b97\u3001\u5e33\u6236\u3001\u7522\u54c1\u4ee5\u5916\u7684\u9801\u9762\u4e2d\uff0c\u79fb\u9664woocommerce\u7684css\u548cjs\u3002<\/p>\n\n<pre class=\"wp-block-code\"><code>\/** Disable All WooCommerce  Styles and Scripts Except Shop Pages*\/\nadd_action( 'wp_enqueue_scripts', 'dequeue_woocommerce_styles_scripts', 99 );\nfunction dequeue_woocommerce_styles_scripts() {\nif ( function_exists( 'is_woocommerce' ) ) {\nif ( ! is_woocommerce() &amp;&amp; ! is_cart() &amp;&amp; ! is_checkout() &amp;&amp; ! is_account_page() ) {\n# Styles\nwp_dequeue_style( 'woocommerce-general' ); \nwp_dequeue_style( 'woocommerce-layout' );\nwp_dequeue_style( 'woocommerce-smallscreen' );\nwp_dequeue_style( 'woocommerce_frontend_styles' );\nwp_dequeue_style( 'woocommerce_fancybox_styles' );\nwp_dequeue_style( 'woocommerce_chosen_styles' );\nwp_dequeue_style( 'woocommerce_prettyPhoto_css' );\n# Scripts\nwp_dequeue_script( 'wc_price_slider' );\nwp_dequeue_script( 'wc-single-product' );\nwp_dequeue_script( 'wc-add-to-cart' );\nwp_dequeue_script( 'wc-cart-fragments' );\nwp_dequeue_script( 'wc-checkout' );\nwp_dequeue_script( 'wc-add-to-cart-variation' );\nwp_dequeue_script( 'wc-single-product' );\nwp_dequeue_script( 'wc-cart' );\nwp_dequeue_script( 'wc-chosen' );\nwp_dequeue_script( 'woocommerce' );\nwp_dequeue_script( 'prettyPhoto' );\nwp_dequeue_script( 'prettyPhoto-init' );\nwp_dequeue_script( 'jquery-blockui' );\nwp_dequeue_script( 'jquery-placeholder' );\nwp_dequeue_script( 'fancybox' );\nwp_dequeue_script( 'jqueryui' );\n}\n}\n}<\/code><\/pre>\n\n<p>20201202\u66f4\u65b0\uff1a\u6709\u8b80\u8005\u53cd\u994b\u63d0\u4ea4\u6642\u5831\u932f\uff1a<\/p>\n\n<pre class=\"wp-block-preformatted\">The snippet has been deactivated due to an error on line 4:\n<strong>Cannot redeclare function dequeue_woocommerce_styles_scripts.<\/strong><\/pre>\n\n<p>\u5982\u679c\u5831\u932f\uff0c\u53ef\u4ee5\u628a\u4ee3\u78bc\u4e2d\u7684dequeue_woocommerce_styles_scripts\u6539\u70badequeue_woocommerce_styles_script\u3002 \u5982\u679c\u4e0d\u5831\u932f\u5c31\u4e0d\u6539\u3002<\/p>\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u7981\u7528WordPress\u7684XML-RPC<\/h2>\n\n<p>WordPress\u7db2\u7ad9\u5f88\u5c11\u9700\u8981\u555f\u7528XML-RPC\uff0c\u4f46\u662f\u555f\u7528\u5b83\u53ef\u80fd\u6703\u5c0e\u81f4\u5927\u91cf\u7684\u5b89\u5168\u554f\u984c\u3002 \u5982\u679c\u4f60\u4f7f\u7528WordPress\u61c9\u7528\u7a0b\u5f0f\uff0c\u4f60\u624d\u9700\u8981\u555f\u7528\u5b83\u3002 \u6b64\u4ee3\u78bc\u7247\u6bb5\u5c07\u7981\u7528XML-RPC\u4ee5\u63d0\u9ad8\u7db2\u7ad9\u5b89\u5168\u6027\u3002<\/p>\n\n<pre class=\"wp-block-code\"><code>add_filter('xmlrpc_enabled', '__return_false');<\/code><\/pre>\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u7528\u4ee3\u78bc\u6bb5\u5b89\u88ddGoogle analytics\u8ddf\u8e64\u4ee3\u78bc<\/h2>\n\n<p>\u8acb\u5c07\u5f9eGoogle analytics\u4e2d\u7372\u53d6\u7684\u8ddf\u8e64\u4ee3\u78bc\u653e\u5165\u5230\u5982\u4e0b\u4ee3\u78bc\u6bb5\u4e2d<\/p>\n\n<pre class=\"wp-block-code\"><code>add_action( 'wp_head', function () { ?&gt;\n\t\n\u8fd9\u91cc\u7c98\u8d34Google Analytics\u7684\u8ddf\u8e2a\u4ee3\u7801\n&lt;?php });<\/code><\/pre>\n\n<p>\u76f8\u95dc\u6559\u7a0b\uff1a<a href=\"https:\/\/loyseo.com\/zh-hant\/tutorial\/how-to-install-google-analytics-in-wordpress\/\">\u5982\u4f55\u7d66WordPress\u7db2\u7ad9\u5b89\u88ddGoogle Analytics\u8ddf\u8e64\u4ee3\u78bc<\/a><\/p>\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u79fb\u9664\/\u96b1\u85cfWordPress\u8a55\u8ad6\u4e2d\u7684url\u6b04\u4f4d<\/h2>\n\n<p>\u4e0b\u9762\u9019\u6bb5\u4ee3\u78bc\u53ef\u4ee5\u79fb\u9664\/\u96b1\u85cf\u90e8\u5206WordPress\u4e3b\u984c\u4e2d\u8a55\u8ad6URL\u5b57\u6bb5\uff0c\u5982\u679c\u4f60\u4f7f\u7528\u540e\u767c\u73fe\u4e0d\u80fd\u79fb\u9664\uff0c\u90a3\u8aaa\u660e\u4e3b\u984c\u4e0d\u76f8\u5bb9\uff0c\u4f60\u53ef\u4ee5\u5728\u8c37\u6b4c\u641c\u7d22\uff1aRemove Website field from the Comment Form in XXX theme\uff0c\u5c07XXX\u6539\u70ba\u4f60\u7684\u4e3b\u984c\u540d\u7a31\u3002<\/p>\n\n<pre class=\"wp-block-code\"><code>function remove_comment_fields($fields) {\nunset($fields&#91;'url']);\nreturn $fields;\n}\nadd_filter('comment_form_default_fields','remove_comment_fields');<\/code><\/pre>\n\n<p>\u8b6c\u5982\uff0c\u5728astra\u4e3b\u984c\u514d\u8cbb\u7248\u4e2d\uff0c\u96b1\u85cf\u8a55\u8ad6\u7684url\u5b57\u6bb5\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u78bc<\/p>\n\n<pre class=\"wp-block-code\"><code>function wpd_remove_comment_website_field( $fields ) {\n    unset( $fields&#91;'url'] );\n    \n    return $fields;\n}\nadd_filter( 'comment_form_default_fields', 'wpd_remove_comment_website_field', 99 );<\/code><\/pre>\n\n<p>\u5728astra \u4e3b\u984c\u4ed8\u8cbb\u7248\u4e2d\uff0c\u79fb\u9664\u8a55\u8ad6\u7684url\u5b57\u6bb5\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u78bc<\/p>\n\n<pre class=\"wp-block-code\"><code>function wpd_remove_comment_website_field( $fields ) {\n    unset( $fields&#91;'url'] );\n    \n    return $fields;\n}\nadd_filter( 'astra_comment_form_default_fields_markup', 'wpd_remove_comment_website_field', 99 );<\/code><\/pre>\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u7528\u4ee3\u78bc\u6bb5\u5be6\u73fe\u8907\u88fd\u6587\u7ae0\u529f\u80fd<\/h2>\n\n<p>\u4e0b\u9762\u9019\u6bb5\u4ee3\u78bc\u53ef\u4ee5\u7d66WordPress\u6587\u7ae0\/\u81ea\u5b9a\u7fa9\u6587\u7ae0\u6dfb\u52a0\u8907\u88fd\u529f\u80fd\uff0c\u5728\u6587\u7ae0\u5217\u8868\u4e2d\u6703\u51fa\u73feduplicate\u6309\u9215\uff0c\u4f46\u4e0d\u6703\u7d66\u9801\u9762\u6dfb\u52a0\u8907\u88fd\u529f\u80fd\u3002 \u4f7f\u7528\u5b83\u4f60\u53ef\u4ee5\u7701\u4e86\u4e00\u500b<a href=\"https:\/\/loyseo.com\/zh-hant\/tutorial\/yoast-duplicate-post\/\" class=\"rank-math-link\">\u8907\u88fd\u6587\u7ae0\u529f\u80fd\u5916\u639b\u7a0b\u5f0f<\/a>\u3002<\/p>\n\n<pre class=\"wp-block-code\"><code>\/*\n * Function for post duplication. Dups appear as drafts. User is redirected to the edit screen\n *\/\nfunction rd_duplicate_post_as_draft(){\n  global $wpdb;\n  if (! ( isset( $_GET&#91;'post']) || isset( $_POST&#91;'post'])  || ( isset($_REQUEST&#91;'action']) &amp;&amp; 'rd_duplicate_post_as_draft' == $_REQUEST&#91;'action'] ) ) ) {\n    wp_die('No post to duplicate has been supplied!');\n  }\n \n  \/*\n   * Nonce verification\n   *\/\n  if ( !isset( $_GET&#91;'duplicate_nonce'] ) || !wp_verify_nonce( $_GET&#91;'duplicate_nonce'], basename( __FILE__ ) ) )\n    return;\n \n  \/*\n   * get the original post id\n   *\/\n  $post_id = (isset($_GET&#91;'post']) ? absint( $_GET&#91;'post'] ) : absint( $_POST&#91;'post'] ) );\n  \/*\n   * and all the original post data then\n   *\/\n  $post = get_post( $post_id );\n \n  \/*\n   * if you don't want current user to be the new post author,\n   * then change next couple of lines to this: $new_post_author = $post-&gt;post_author;\n   *\/\n  $current_user = wp_get_current_user();\n  $new_post_author = $current_user-&gt;ID;\n \n  \/*\n   * if post data exists, create the post duplicate\n   *\/\n  if (isset( $post ) &amp;&amp; $post != null) {\n \n    \/*\n     * new post data array\n     *\/\n    $args = array(\n      'comment_status' =&gt; $post-&gt;comment_status,\n      'ping_status'    =&gt; $post-&gt;ping_status,\n      'post_author'    =&gt; $new_post_author,\n      'post_content'   =&gt; $post-&gt;post_content,\n      'post_excerpt'   =&gt; $post-&gt;post_excerpt,\n      'post_name'      =&gt; $post-&gt;post_name,\n      'post_parent'    =&gt; $post-&gt;post_parent,\n      'post_password'  =&gt; $post-&gt;post_password,\n      'post_status'    =&gt; 'draft',\n      'post_title'     =&gt; $post-&gt;post_title,\n      'post_type'      =&gt; $post-&gt;post_type,\n      'to_ping'        =&gt; $post-&gt;to_ping,\n      'menu_order'     =&gt; $post-&gt;menu_order\n    );\n \n    \/*\n     * insert the post by wp_insert_post() function\n     *\/\n    $new_post_id = wp_insert_post( $args );\n \n    \/*\n     * get all current post terms ad set them to the new post draft\n     *\/\n    $taxonomies = get_object_taxonomies($post-&gt;post_type); \/\/ returns array of taxonomy names for post type, ex array(\"category\", \"post_tag\");\n    foreach ($taxonomies as $taxonomy) {\n      $post_terms = wp_get_object_terms($post_id, $taxonomy, array('fields' =&gt; 'slugs'));\n      wp_set_object_terms($new_post_id, $post_terms, $taxonomy, false);\n    }\n \n    \/*\n     * duplicate all post meta just in two SQL queries\n     *\/\n    $post_meta_infos = $wpdb-&gt;get_results(\"SELECT meta_key, meta_value FROM $wpdb-&gt;postmeta WHERE post_id=$post_id\");\n    if (count($post_meta_infos)!=0) {\n      $sql_query = \"INSERT INTO $wpdb-&gt;postmeta (post_id, meta_key, meta_value) \";\n      foreach ($post_meta_infos as $meta_info) {\n        $meta_key = $meta_info-&gt;meta_key;\n        if( $meta_key == '_wp_old_slug' ) continue;\n        $meta_value = addslashes($meta_info-&gt;meta_value);\n        $sql_query_sel&#91;]= \"SELECT $new_post_id, '$meta_key', '$meta_value'\";\n      }\n      $sql_query.= implode(\" UNION ALL \", $sql_query_sel);\n      $wpdb-&gt;query($sql_query);\n    }\n \n \n    \/*\n     * finally, redirect to the edit post screen for the new draft\n     *\/\n    wp_redirect( admin_url( 'post.php?action=edit&amp;post=' . $new_post_id ) );\n    exit;\n  } else {\n    wp_die('Post creation failed, could not find original post: ' . $post_id);\n  }\n}\nadd_action( 'admin_action_rd_duplicate_post_as_draft', 'rd_duplicate_post_as_draft' );\n \n\/*\n * Add the duplicate link to action list for post_row_actions\n *\/\nfunction rd_duplicate_post_link( $actions, $post ) {\n  if (current_user_can('edit_posts')) {\n    $actions&#91;'duplicate'] = '&lt;a href=\"' . wp_nonce_url('admin.php?action=rd_duplicate_post_as_draft&amp;post=' . $post-&gt;ID, basename(__FILE__), 'duplicate_nonce' ) . '\" title=\"Duplicate this item\" rel=\"permalink\"&gt;Duplicate&lt;\/a&gt;';\n  }\n  return $actions;\n}\n \nadd_filter( 'post_row_actions', 'rd_duplicate_post_link', 10, 2 );<\/code><\/pre>\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u7528\u4ee3\u78bc\u6bb5\u7981\u6b62WordPress\u81ea\u52d5\u751f\u6210\u5716\u7247<\/h2>\n\n<p>\u7531\u65bc\u6bcf\u5f35\u5716\u7247\u5728\u4e0a\u50b3\u5230WordPress\u6642\uff0c\u6703\u88abWordPress\u6216\u90e8\u5206\u5916\u639b\u7a0b\u5f0f\u81ea\u52d5\u751f\u6210\u5f88\u591a\u4e0d\u540c\u5c3a\u5bf8\u7684\u5716\u7247\uff0c\u91dd\u5c0d\u7528\u4e0d\u4e0a\u7684\u5716\u7247\u5c3a\u5bf8\uff0c\u53ef\u4ee5\u7528\u77ed\u4ee3\u78bc\u76f4\u63a5\u7d44\u7e54\u7cfb\u7d71\u751f\u6210\u5716\u7247\uff0c\u4e0d\u50c5\u80fd\u7bc0\u7701\u7a7a\u9593\uff0c\u4e5f\u80fd\u907f\u514d\u6d88\u8017\u58d3\u7e2e\u984d\u5ea6\uff0c\u4e0b\u9762\u4ecb\u7d39\u5982\u4f55\u7528\u4ee3\u78bc\u6bb5\uff08code snippets\uff09\u7981\u6b62WordPress\u81ea\u52d5\u751f\u6210\u5716\u7247\u3002<\/p>\n\n<p>\u9700\u8981\u77e5\u9053\u7684\u662f\uff0c\u9019\u4e9b\u81ea\u52d5\u751f\u6210\u7684\u5716\u7247\u4e26\u4e0d\u80fd\u5728Wordpress\u7684\u5a92\u9ad4\u5eab\u770b\u5230\uff0c\u9700\u8981\u5728\u4f3a\u670d\u5668\u7684\u8cc7\u6599\u593e\u4e2d\u67e5\u770b\u3002<\/p>\n\n<p>\u4e0b\u5716\u662f<a href=\"https:\/\/loyseo.com\/zh-hant\/tutorial\/siteground-hosting\/\" class=\"rank-math-link\">Siteground<\/a>\u5f8c\u53f0\u67e5\u770b\u5716\u7247\u6a94\u7684\u65b9\u6cd5\uff0c\u8acb\u5728\u9032\u5165\u7db2\u7ad9\u7684Sitetools\u540e\u6309\u4e0b\u5716\u6240\u793a\u9806\u5e8f\u64cd\u4f5c\u67e5\u770b\uff0c\u6211\u5011\u80fd\u770b\u5230\u7cfb\u7d71\u70ba\u540c\u4e00\u5f35\u5716\u751f\u6210\u4e86\u5f88\u591a\u4e0d\u540c\u5c3a\u5bf8\u7684\u5716\u7247\u3002<\/p>\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/loyseo.com\/wp-content\/uploads\/2020\/05\/wordpress%E8%87%AA%E5%8A%A8%E7%94%9F%E6%88%90%E5%9B%BE%E7%89%87%E5%8E%BB%E9%99%A4%E6%95%99%E7%A8%8B.png\" alt=\"10&#x500B;WordPress&#x5E38;&#x7528;&#x4EE3;&#x78BC;&#x6BB5;&#xFF08;Code Snippets&#xFF09;\" title=\"-\"><\/figure>\n\n<p>\u5728\u7db2\u7ad9\u9801\u9762\u8a2d\u8a08\u5b8c\u3001\u5167\u5bb9\u4e0a\u50b3\u524d\uff0c\u6211\u5011\u5148\u7981\u6b62\u6240\u6709\u81ea\u52d5\u751f\u6210\u7684\u5716\u7247; \u82e5\u99ac\u4e0a\u53ef\u4ee5\u4e0a\u50b3\u5167\u5bb9\u4e86\uff0c\u6211\u5011\u53ef\u4ee5\u6839\u64da\u8a2d\u8a08\u60c5\u6cc1\uff0c\u914c\u60c5\u653e\u958b\u90e8\u5206\u5716\u7247\u5c3a\u5bf8\uff0c\u8b6c\u5982\u5728\u88fd\u4f5c\u7522\u54c1\u5217\u8868\u9801\u6642\uff0c\u6211\u5011\u7528\u5230\u4e86300*300px\u7684\u5716\u7247\uff0c\u90a3\u9ebc\u5c31\u5728\u4e0b\u9762\u7684\u4ee3\u78bc\u4e2d\uff0c\u5c07\u5c0d\u61c9\u884c\u7684\u4ee3\u78bc\u524d\u52a0\/\u8a3b\u91cb\u6389\u6216\u76f4\u63a5\u522a\u9664\u8a72\u884c\u3002<\/p>\n\n<pre class=\"wp-block-code\"><code>\/\/ disable generated image sizes\nfunction shapeSpace_disable_image_sizes($sizes) {\n\t\n\tunset($sizes&#91;'thumbnail']);    \/\/ disable thumbnail size\n\tunset($sizes&#91;'medium']);       \/\/ disable medium size\n\tunset($sizes&#91;'large']);        \/\/ disable large size\n\tunset($sizes&#91;'medium_large']); \/\/ disable medium-large size\n\tunset($sizes&#91;'1536x1536']);    \/\/ disable 2x medium-large size\n\tunset($sizes&#91;'2048x2048']);    \/\/ disable 2x large size\n   unset($sizes&#91;'shop_catalog']);\n   unset($sizes&#91;'shop_single']);\n   unset($sizes&#91;'shop_thumbnail']);\n   unset($sizes&#91;'woocommerce_thumbnail']);\n   unset($sizes&#91;'woocommerce_single']);\n   unset($sizes&#91;'woocommerce_gallery_thumbnail']);\n\t\n\treturn $sizes;\n\t\n}\nadd_action('intermediate_image_sizes_advanced', 'shapeSpace_disable_image_sizes');\n\n\/\/ disable scaled image size\nadd_filter('big_image_size_threshold', '__return_false');\n\n\/\/ disable other image sizes\nfunction shapeSpace_disable_other_image_sizes() {\n\t\n\tremove_image_size('post-thumbnail'); \/\/ disable images added via set_post_thumbnail_size() \n\tremove_image_size('another-size');   \/\/ disable any other added image sizes\n\t\n}\nadd_action('init', 'shapeSpace_disable_other_image_sizes');<\/code><\/pre>\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u7528\u4ee3\u78bc\u6bb5\u53bb\u6389woocommerce\u7522\u54c1\u9996\u9801\u3001\u5217\u8868\u9801\u7684\u9eb5\u5305\u5c51<\/h2>\n\n<pre class=\"wp-block-code\"><code>\/**\n * Remove the breadcrumbs \n *\/\nadd_action( 'init', 'woo_remove_wc_breadcrumbs' );\nfunction woo_remove_wc_breadcrumbs() {\n    remove_action( 'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20, 0 );\n}<\/code><\/pre>\n\n<h2 class=\"wp-block-heading\">WooCommerce\u5982\u4f55\u5728\u4ed8\u6b3e\u5f8c\u81ea\u52d5\u5b8c\u6210\u865b\u64ec\u8a02\u55ae<\/h2>\n\n<pre class=\"wp-block-code\"><code>add_filter( 'woocommerce_payment_complete_order_status', 'wmg_auto_complete_virtual_orders', 10, 3 );\n\n\/**\n * Automatically complete orders with only virtual products\n *\n * @param  string   $payment_complete_status Order status used after an order payment is received\n * @param  int      $order_id                ID of the order being processed\n * @param  WC_Order $order                   Order object being processed\n * @return string   $payment_complete_status Updated order status\n *\/\nfunction wmg_auto_complete_virtual_orders( $payment_complete_status, $order_id, $order ) {\n  $current_status = $order-&gt;get_status();\n  \/\/ We only want to update the status to 'completed' if it's coming from one of the following statuses: \n  $allowed_current_statuses = array( 'on-hold', 'pending', 'failed' );\n\n  if ( 'processing' === $payment_complete_status &amp;&amp; in_array( $current_status, $allowed_current_statuses ) ) {\n\n    $order_items = $order-&gt;get_items();\n\n    \/\/ Create an array of products in the order\n    $order_products = array_filter( array_map( function( $item ) {\n      \/\/ Get associated product for each line item\n      return $item-&gt;get_product();\n    }, $order_items ), function( $product ) {\n      \/\/ Remove non-products\n      return !! $product;\n    } );\n\n    if ( count( $order_products &gt; 0 ) ) {\n      \/\/ Check if each product is 'virtual'\n      $is_virtual_order = array_reduce( $order_products, function( $virtual_order_so_far, $product ) {\n        return $virtual_order_so_far &amp;&amp; $product-&gt;is_virtual();\n      }, true );\n\n      if ( $is_virtual_order ) {\n        $payment_complete_status = 'completed';\n      }\n    }\n\n  }\n\n  return $payment_complete_status;\n}<\/code><\/pre>\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u5c07woocomerce\u7522\u54c1\u5217\u8868\u9801\u7684\u201cadd to cart\u201d\u6309\u9215\u66ff\u63db\u70ba\u67e5\u770b\u7522\u54c1\u8a73\u60c5\u6309\u9215<\/h2>\n\n<figure class=\"wp-block-image size-large\"><img fetchpriority=\"high\" decoding=\"async\" width=\"1376\" height=\"546\" src=\"https:\/\/loyseo.com\/wp-content\/uploads\/2021\/01\/image-11.png\" alt=\"10&#x500B;WordPress&#x5E38;&#x7528;&#x4EE3;&#x78BC;&#x6BB5;&#xFF08;Code Snippets&#xFF09;\" class=\"wp-image-508809\" title=\"-\"><\/figure>\n\n<pre class=\"wp-block-code\"><code>add_filter( 'woocommerce_loop_add_to_cart_link', 'replacing_add_to_cart_button', 10, 2 );\nfunction replacing_add_to_cart_button( $button, $product  ) {\n    $button_text = __(\"View product\", \"woocommerce\");\n    $button = '&lt;a class=\"button\" href=\"' . $product-&gt;get_permalink() . '\"&gt;' . $button_text . '&lt;\/a&gt;';\n\n    return $button;\n}<\/code><\/pre>\n\n<p>\u5982\u679c\u4f60\u60f3\u4fee\u6539\u6309\u9215\u4e0a\u7684\u6587\u6848\uff0c\u5c07\u4e0a\u8ff0\u4ee3\u78bc\u4e2d\u7684view product\u5c0d\u61c9\u6539\u6389\u5373\u53ef\u3002<\/p>\n\n<h2 class=\"wp-block-heading\">\u5982\u4f55\u7e2e\u77edWooCommerce\u7522\u54c1\u5217\u8868\u4e2d\u7684\u7522\u54c1\u6a19\u984c\u7684\u5b57\u5143\u6578<\/h2>\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1302\" height=\"661\" src=\"https:\/\/loyseo.com\/wp-content\/uploads\/2021\/06\/code-snippets-for-wordpress-13.png\" alt=\"10&#x500B;WordPress&#x5E38;&#x7528;&#x4EE3;&#x78BC;&#x6BB5;&#xFF08;Code Snippets&#xFF09;\" class=\"wp-image-511562\" title=\"-\"><\/figure>\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" width=\"1388\" height=\"677\" src=\"https:\/\/loyseo.com\/wp-content\/uploads\/2021\/06\/code-snippets-for-wordpress-12.png\" alt=\"10&#x500B;WordPress&#x5E38;&#x7528;&#x4EE3;&#x78BC;&#x6BB5;&#xFF08;Code Snippets&#xFF09;\" class=\"wp-image-511561\" title=\"-\"><\/figure>\n\n<pre class=\"wp-block-code\"><code>\/\/ Automatically shortens WooCommerce product titles on the main shop, category, and tag pages\r\n\/\/ to a set number of characters \r\nfunction short_woocommerce_product_titles_chars( $title, $id ) {\r\n  if ( ( is_shop() || is_product_tag() || is_product_category() ) &amp;&amp; get_post_type( $id ) === 'product' ) {\r\n    \/\/ Kicks in if the product title is longer than 32 characters \r\n    if ( strlen( $title ) > 32) {\r\n      \/\/ Shortens it to 32 characters and adds ellipsis at the end\r\n      return substr( $title, 0, 32 ) . '...';\r\n    }\r\n  }\r\n\r\n  return $title;\r\n}\r\nadd_filter( 'the_title', 'short_woocommerce_product_titles_chars', 10, 2 );<\/code><\/pre>\n\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u78bc\u4e2d\uff0c\u6578\u4f4d32\u8868\u793a\uff1a\u4f60\u5e0c\u671b\u6a19\u984c\u5c55\u793a\u7684\u5b57\u5143\u6578\u91cf\uff0c\u4f60\u53ef\u4ee5\u6309\u9700\u8abf\u6574\u9019\u500b\u6578\u503c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u904b\u7528\u4ee3\u78bc\u6bb5\uff08Code Snippets\uff09\u5916\u639b\u7ba1\u7406\u4ee3\u78bc\uff0c\u53ef\u4ee5\u4e0d\u7528\u984d\u5916\u5b89\u88dd\u66f4\u591a\u5916\u639b\uff0c\u4f86\u89e3\u6c7aWordPress\u67b6\u8a2d\u7db2\u7ad9\u904e\u7a0b\u4e2d\u7684\u4e00\u4e9b\u5e38\u898b\u529f\u80fd\u9700\u6c42\uff0c\u8b6c\u5982\u5b89\u88ddGoogle analytics\u8ddf\u8e64\u4ee3\u78bc\u3002 \u4e0b\u6587\u4e2d\u8a18\u9304\u4e86\u6211\u5728\u5916\u8cbf\u67b6\u8a2d\u7db2\u7ad9\u548c\u500b\u4eba\u535a\u5ba2\u4e2d\u5e38\u7528\u5230\u7684\u4ee3\u78bc\u6bb5\u3002 \u5982\u4f55\u5728WordPress\u6587\u7ae0\/\u9801\u9762\u4e0a\u7981\u6b62\u8f09\u5165WooCommerce .js\uff08javascript\uff09\u548c.css\u6a94\uff1f woocommerce\u6703\u5728\u6bcf\u500b\u9801\u9762\u90fd\u9ed8 [&hellip;]<\/p>\n","protected":false},"featured_media":0,"parent":0,"menu_order":164,"comment_status":"open","ping_status":"open","template":"","hf_cat_tutorial":[],"class_list":["post-530276","tutorial","type-tutorial","status-publish","hentry"],"acf":[],"taxonomy_info":[],"featured_image_src_large":false,"author_info":[],"comment_info":0,"_links":{"self":[{"href":"https:\/\/loyseo.com\/zh-hant\/wp-json\/wp\/v2\/tutorial\/530276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/loyseo.com\/zh-hant\/wp-json\/wp\/v2\/tutorial"}],"about":[{"href":"https:\/\/loyseo.com\/zh-hant\/wp-json\/wp\/v2\/types\/tutorial"}],"replies":[{"embeddable":true,"href":"https:\/\/loyseo.com\/zh-hant\/wp-json\/wp\/v2\/comments?post=530276"}],"version-history":[{"count":0,"href":"https:\/\/loyseo.com\/zh-hant\/wp-json\/wp\/v2\/tutorial\/530276\/revisions"}],"wp:attachment":[{"href":"https:\/\/loyseo.com\/zh-hant\/wp-json\/wp\/v2\/media?parent=530276"}],"wp:term":[{"taxonomy":"hf_cat_tutorial","embeddable":true,"href":"https:\/\/loyseo.com\/zh-hant\/wp-json\/wp\/v2\/hf_cat_tutorial?post=530276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}