{"id":32564,"date":"2024-11-25T14:56:49","date_gmt":"2024-11-25T06:56:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/32564\/"},"modified":"2024-11-25T14:56:49","modified_gmt":"2024-11-25T06:56:49","slug":"%e5%9c%a8%e6%82%a8%e7%9a%84%e7%bd%91%e7%ab%99%e4%b8%8a%e5%b1%95%e7%a4%ba-wordpress-org-%e6%8f%92%e4%bb%b6%e7%9a%84%e4%bf%a1%e6%81%af","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/32564\/","title":{"rendered":"\u5728\u60a8\u7684\u7f51\u7ad9\u4e0a\u5c55\u793a WordPress.org \u63d2\u4ef6\u7684\u4fe1\u606f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/164\/169372110793764.jpg\" class=\"aligncenter\" title=\"\u5728\u60a8\u7684\u7f51\u7ad9\u4e0a\u5c55\u793a WordPress.org \u63d2\u4ef6\u7684\u4fe1\u606f\u63d2\u56fe\" alt=\"\u5728\u60a8\u7684\u7f51\u7ad9\u4e0a\u5c55\u793a WordPress.org \u63d2\u4ef6\u7684\u4fe1\u606f\u63d2\u56fe\" \/><\/p>\n<\/p>\n<p>\u5728\u672c\u6587\u7684\u7b2c\u4e00\u90e8\u5206\u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u5982\u4f55\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u4e0e WordPress.org \u8fdb\u884c\u901a\u4fe1\u5e76\u68c0\u7d22\u63d2\u4ef6\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<p>\u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u628a\u7406\u8bba\u4ed8\u8bf8\u5b9e\u8df5\uff0c\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u63d2\u4ef6\uff0c\u8be5\u63d2\u4ef6\u5c06\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u7b80\u7801\u5728\u6211\u4eec\u7684 WordPress \u7f51\u7ad9\u4e0a\u663e\u793a WordPress.org \u4e0a\u6258\u7ba1\u7684\u4efb\u4f55\u63d2\u4ef6\u7684\u8be6\u7ec6\u4fe1\u606f\u3002  p&gt; <\/p>\n<hr>\n<h2>\u5f00\u59cb\u4f7f\u7528<\/h2>\n<p>\u6211\u5047\u8bbe\u60a8\u662f\u63d2\u4ef6\u5f00\u53d1\u4eba\u5458\u5e76\u4e14\u4e86\u89e3\u57fa\u7840\u77e5\u8bc6\uff0c\u4f46\u5982\u679c\u6709\u7591\u95ee\uff0c\u6211\u5efa\u8bae\u9605\u8bfb\u4ee5\u4e0b\u4e24\u7bc7\u6587\u7ae0\uff1a<\/p>\n<ul>\n<li>\u5f00\u53d1 WordPress \u63d2\u4ef6\u7684\u4e24\u79cd\u65b9\u6cd5\uff1a\u51fd\u6570\u5f0f\u7f16\u7a0b<\/li>\n<li>\u5f00\u53d1 WordPress \u63d2\u4ef6\u7684\u4e24\u79cd\u65b9\u6cd5\uff1a\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b<\/li>\n<\/ul>\n<h3>\u6211\u4eec\u5728\u505a\u4ec0\u4e48\uff1f<\/h3>\n<p>\u901a\u8fc7\u8fd9\u4e2a\u63d2\u4ef6\uff0c\u6211\u4eec\u60f3\u8981\u521b\u5efa\u4e00\u4e2a\u77ed\u4ee3\u7801\uff0c\u4f8b\u5982 [mpi slug=&#8217;my-plugin-information&#8217; field=&#8217;version&#8217;] \uff0c\u5b83\u53ef\u4ee5\u63a5\u53d7\u4e24\u4e2a\u5c5e\u6027\uff1a\u201cslug\u201d\u548c\u201cfield\u201d\uff0c\u7136\u540e\u57fa\u4e8e\u7136\u540e\uff0c\u6211\u4eec\u68c0\u7d22\u5e76\u663e\u793a WordPress.org \u5b58\u50a8\u5e93\u4e2d\u6258\u7ba1\u7684\u4efb\u4f55\u63d2\u4ef6\u7684\u4fe1\u606f\u3002<\/p>\n<h3>\u521b\u5efa\u63d2\u4ef6\u5e93<\/h3>\n<p>\u8ba9\u6211\u4eec\u9996\u5148\u5728 <strong>wp-content\/plugins<\/strong> \u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <strong>my-plugin-information<\/strong> \u7684\u6587\u4ef6\u5939\u3002\u5728\u5176\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a <strong>my-plugin-info.php<\/strong> \u7684\u6587\u4ef6\uff0c\u5e76\u5c06\u4ee5\u4e0b\u4ee3\u7801\u7c98\u8d34\u5230\u5176\u4e2d\uff1a<\/p>\n<pre>\n&lt;?php \/*\nPlugin Name: My Plugin Info\nPlugin URI: https:\/\/myplugininfo.com\nDescription: Communicate with WordPress.org Plugins API to retrive your Plugin Information\nVersion: 0.1\nAuthor: Harish\nAuthor Email: mye@email.com\nLicense: GPL3\n*\/\nif ( ! defined( 'ABSPATH' ) ) exit; \/\/ Exit if accessed directly\n\nif ( ! class_exists( 'DOT_MyPluginInfo' ) )\n{\n\n\tclass DOT_MyPluginInfo {\n\n\t\t\/**\n\t\t * Constructor\n\t\t *\/\n\t\tfunction __construct() {\n\n\t\t\t\/\/Hook up to the init action\n\t\t\tadd_action( 'init', array( &amp;$this, 'init_my_plugin_info' ) );\n\n\t\t}\n\n\t\t\/**\n\t\t * Runs when the plugin is initialized\n\t\t *\/\n\t\tfunction init_my_plugin_info() {\n\n\t\t\t\/\/ Register the shortcode [mpi slug='my-plugin-info' field='version']\n\t\t\tadd_shortcode( 'mpi', array( &amp;$this, 'render_mpi' ) );\n\n\t\t}\n\n\t\tfunction render_mpi($atts) {\n\n\t\t}\n\n\t} \/\/ end class\n\n\tnew DOT_MyPluginInfo();\n}\n?&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>\u6211\u4eec\u505a\u4e86\u4ec0\u4e48\uff1f<\/h3>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u5e76\u521d\u59cb\u5316\u4e86\u63d2\u4ef6\u7c7b DOT_MyPluginInfo\u3002\u5176\u4e2d\u5305\u542b\u4efb\u4f55\u63d2\u4ef6\u7684\u901a\u7528\u5757\uff0c\u4f8b\u5982 __construct() \u65b9\u6cd5\u3002<\/p>\n<p>\u51fd\u6570 init_my_plugin_info \u4e0e init \u64cd\u4f5c\u6302\u94a9\uff0c\u4ee5\u4fbf\u5b83\u5728\u52a0\u8f7d WordPress \u4e4b\u540e\u4f46\u5728\u53d1\u9001\u4efb\u4f55\u6807\u5934\u4e4b\u524d\u8fd0\u884c\u3002\u5728\u51fd\u6570 init_my_plugin_info \u4e2d\uff0c\u6211\u4eec\u4f7f\u7528 add_shortcode \u51fd\u6570\u6ce8\u518c\u4e86\u6211\u4eec\u7684\u77ed\u4ee3\u7801\u3002<\/p>\n<p><em>\u6ce8\u610f\uff1a\u8981\u4e86\u89e3\u6709\u5173 add_shortcode \u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u67e5\u770b Codex\u3002<\/em><\/p>\n<p>\u4e0a\u9762\u7684\u63d2\u4ef6\u73b0\u5728\u6709\u8db3\u591f\u7684\u4ee3\u7801\u53ef\u4ee5\u88ab WordPress \u4ece\u63d2\u4ef6\u4eea\u8868\u677f\u8bc6\u522b\u3002\u5982\u679c\u60a8\u5df2\u7ecf\u6309\u7167\u8bf4\u660e\u521b\u5efa\u4e86\u6587\u4ef6\uff0c\u60a8\u73b0\u5728\u53ef\u4ee5\u8bbf\u95ee<strong>\u63d2\u4ef6<\/strong>\u9875\u9762\u5e76\u6fc0\u6d3b\u6b64\u63d2\u4ef6\u3002<\/p>\n<hr>\n<h3>\u8bbe\u7f6e\u7b80\u7801<\/h3>\n<p>\u7531\u4e8e\u6211\u4eec\u5e0c\u671b\u7075\u6d3b\u5730\u9009\u62e9\u8981\u663e\u793a\u6709\u5173\u63d2\u4ef6\u7684\u4fe1\u606f\uff0c\u56e0\u6b64\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u5177\u6709\u4e24\u4e2a\u5c5e\u6027\u7684\u77ed\u4ee3\u7801\u3002\u7b2c\u4e00\u4e2a\u79f0\u4e3a\u201cslug\u201d\u5c06\u7528\u4e8e\u6307\u5b9a\u9700\u8981\u68c0\u7d22\u54ea\u4e2a\u63d2\u4ef6\u7684\u6570\u636e\u3002\u7b2c\u4e8c\u4e2a\u5c5e\u6027\u201cfield\u201d\u5c06\u7528\u4e8e\u6307\u5b9a\u6211\u4eec\u9700\u8981\u663e\u793a\u7684\u63d2\u4ef6\u7684\u5177\u4f53\u4fe1\u606f\u3002<\/p>\n<p>\u4f8b\u5982\uff0c\u5982\u679c\u6211\u4eec\u60f3\u663e\u793a\u8be5\u63d2\u4ef6\u7684\u4e0b\u8f7d\u6b21\u6570\uff0c\u6211\u4eec\u53ea\u9700\u5728\u5e16\u5b50\u7f16\u8f91\u5668\u4e0b\u65b9\u6dfb\u52a0\u6587\u672c\uff0c\u6700\u7ec8\u7ed3\u679c\u5e94\u8be5\u662f\u201c\u4e0b\u8f7d\u4e86 100 \u6b21\u201d\u4e4b\u7c7b\u7684\u5185\u5bb9\u3002 p&gt; <\/p>\n<pre>\nDownloaded [mpi slug='my-plugin-information' field='downloaded'] times.\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4f7f\u7528 add_shortcode \u6211\u4eec\u6ce8\u518c\u4e86\u6211\u4eec\u7684\u77ed\u4ee3\u7801\uff0c\u4ee5\u4fbf\u6bcf\u5f53\u5728\u5e16\u5b50\u5185\u5bb9\u4e2d\u627e\u5230\u77ed\u4ee3\u7801\u65f6\uff0c\u90fd\u4f1a\u8c03\u7528\u51fd\u6570 render_mpi() \u6765\u5904\u7406\u5b83\u3002\u4ece\u73b0\u5728\u5f00\u59cb\uff0c\u5176\u4f59\u4ee3\u7801\u5c06\u653e\u7f6e\u5728\u8be5\u51fd\u6570\u5185\u6765\u5904\u7406\u6211\u4eec\u7684\u77ed\u4ee3\u7801\u3002<\/p>\n<h3>\u4f7f\u7528 render_mpi() \u5904\u7406\u77ed\u4ee3\u7801 <\/h3>\n<p>\u8981\u663e\u793a\u63d2\u4ef6\u4fe1\u606f\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u5904\u7406\u77ed\u4ee3\u7801\u4ee5\u83b7\u53d6\u5c5e\u6027\u3002\u5728 render_api \u51fd\u6570\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre>\n\/\/ get our variable from $atts\nextract( shortcode_atts( array(\n\t'slug' =&amp;gt; '', \/\/foo is a default value\n\t'field' =&amp;gt; ''\n), $atts ) );\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u4f1a\u63d0\u53d6\u4e24\u4e2a\u5c5e\u6027\u201cslug\u201d\u548c\u201cfield\u201d\uff08\u5982\u679c\u63d0\u4f9b\uff09\u3002\u5728\u7ee7\u7eed\u4e4b\u524d\uff0c\u6211\u4eec\u9996\u5148\u68c0\u67e5\u201cslug\u201d\u548c\u201cfield\u201d\u7684\u503c\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u505c\u6b62\u8fdb\u4e00\u6b65\u5904\u7406\u3002<\/p>\n<pre>\n\/**\n * Check if slug exists\n *\/\nif ( ! $slug ) {\n\treturn false;\n}\n\n\/**\n * Check if field exists\n * Return value based on the field attribute\n *\/\nif ( ! $field ) {\n\treturn false;\n} else {\n\n} \/\/ $field check\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u4e0a\u9762\u7684\u4ee3\u7801\u5c06\u68c0\u67e5\u201cslug\u201d\u662f\u5426\u5b58\u5728\uff0c\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de false\u3002\u5982\u679c\u201cslug\u201d\u786e\u5b9e\u5b58\u5728\uff0c\u5b83\u5c06\u7ee7\u7eed\u68c0\u67e5\u201cfield\u201d\u5c5e\u6027\u3002\u7531\u4e8e\u6211\u4eec\u53ea\u662f\u521b\u5efa\u4e00\u4e2a\u77ed\u4ee3\u7801\u6765\u663e\u793a\u6709\u5173\u63d2\u4ef6\u7684\u7279\u5b9a\u4fe1\u606f\uff0c\u56e0\u6b64\u5728\u8fdb\u4e00\u6b65\u5904\u7406\u4e4b\u524d\u68c0\u67e5\u8fd9\u4e24\u4e2a\u5c5e\u6027\u662f\u5426\u5b58\u5728\u5c06\u8282\u7701\u5bf9 WordPress.org \u63d2\u4ef6 API \u7684\u4e0d\u5fc5\u8981\u7684\u8c03\u7528\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u5982\u679c\u77ed\u4ee3\u7801\u4e2d\u63d0\u4f9b\u4e86\u201cslug\u201d\u548c\u201cfield\u201d\u5c5e\u6027\u7684\u503c\uff0c\u6211\u4eec\u5c06\u9996\u5148\u6e05\u7406\u8fd9\u4e24\u4e2a\u503c\u3002<\/p>\n<pre>\n\/\/ Sanitize attributes\n$slug = sanitize_title( $slug );\n$field = sanitize_title( $field );\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>\u5728\u77ac\u6001\u4e2d\u5b58\u50a8\u63d2\u4ef6\u6570\u636e<\/h3>\n<p>\u4e3a\u4e86\u907f\u514d\u6bcf\u6b21\u52a0\u8f7d\u5305\u542b\u6b64\u77ed\u4ee3\u7801\u7684\u9875\u9762\u65f6\u90fd\u5411 WordPress.org \u53d1\u9001\u8bf7\u6c42\uff0c\u6211\u4eec\u9700\u8981\u5728\u672c\u5730\u4fdd\u5b58\u63d2\u4ef6\u4fe1\u606f\u3002\u8fd9\u6837\uff0c\u5982\u679c\u60a8\u653e\u7f6e\u4e86\u591a\u4e2a\u77ed\u4ee3\u7801\u6765\u663e\u793a\u540c\u4e00\u63d2\u4ef6\u7684\u4e0d\u540c\u8be6\u7ec6\u4fe1\u606f\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u901a\u8fc7\u663e\u793a\u60a8\u7f51\u7ad9\u4e0a\u672c\u5730\u4fdd\u5b58\u7684\u4fe1\u606f\u4e2d\u7684\u6570\u636e\u6765\u52a0\u5feb\u8fd9\u4e00\u8fc7\u7a0b\u3002<\/p>\n<p><em>\u4f46\u662f\u5982\u679c\u63d2\u4ef6\u66f4\u65b0\u5e76\u4e14\u6211\u4eec\u7ee7\u7eed\u663e\u793a\u65e7\u6570\u636e\u600e\u4e48\u529e\uff1f<\/em>\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6700\u5feb\u7684\u9009\u62e9\u662f\u4f7f\u7528 Transients API \u4fdd\u5b58\u6211\u4eec\u7684\u4e2a\u4eba\u63d2\u4ef6\u6570\u636e\u5e76\u8bbe\u7f6e\u5230\u671f\u65e5\u671f\u6570\u636e\u3002<\/p>\n<p>\u53e6\u4e00\u4e2a\u95ee\u9898\u662f\uff0c\u5982\u679c\u60a8\u6709\u6b63\u5728\u68c0\u7d22\u6709\u5173\u4e0d\u540c\u63d2\u4ef6\u7684\u6570\u636e\u7684\u77ed\u4ee3\u7801\u3002\u5982\u679c\u6211\u4eec\u4f7f\u7528\u5355\u4e2a\u4e34\u65f6\u540d\u79f0\u5b58\u50a8\u5b83\u4eec\uff0c\u7ed3\u679c\u53ef\u80fd\u4f1a\u51fa\u4e4e\u610f\u6599\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u4f7f\u7528\u201cslug\u201d\u5c5e\u6027\u4e3a\u4fdd\u5b58\u7684\u77ac\u6001\u63d0\u4f9b\u4e00\u4e2a\u552f\u4e00\u7684\u540d\u79f0\u3002<\/p>\n<h3>\u4e3a\u4ec0\u4e48\u8981\u7ecf\u5386\u8fd9\u4e00\u5207\uff1f<\/h3>\n<ul>\n<li>\u5355\u72ec\u4fdd\u5b58\u6bcf\u4e2a\u63d2\u4ef6\u7684\u4fe1\u606f<\/li>\n<li>\u51cf\u5c11\u5411 WordPress.org \u53d1\u51fa\u7684\u8bf7\u6c42<\/li>\n<li>\u901a\u8fc7\u76f4\u63a5\u4ece\u60a8\u81ea\u5df1\u7684\u7f51\u7ad9\u63d0\u4f9b\u6570\u636e\u6765\u66f4\u5feb\u5730\u52a0\u8f7d\u6570\u636e<\/li>\n<\/ul>\n<p>\u8ba9\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf $mpi_transient_name \u6765\u4fdd\u5b58\u57fa\u4e8e\u201cslug\u201d\u5c5e\u6027\u7684\u552f\u4e00\u77ac\u6001\u540d\u79f0\u3002<\/p>\n<pre>\n\/\/ Create a empty array with variable name different based on plugin slug\n$mpi_transient_name = 'mpi-' . $slug;\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u68c0\u67e5\u77ac\u6001\u662f\u5426\u5df2\u7ecf\u5b58\u5728\uff1a<\/p>\n<pre>\n\/**\n * Check if transient with the plugin data exists\n *\/\n $mpi_info = get_transient( $mpi_transient_name );\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5982\u679c\u77ac\u6001\u5b58\u5728\uff0c\u6211\u4eec\u5c06\u7ee7\u7eed\u6839\u636e\u201cfield\u201d\u5c5e\u6027\u663e\u793a\u6570\u636e\uff0c\u5426\u5219\u6211\u4eec\u4f7f\u7528 plugins_api \u8fde\u63a5\u5230 WordPress.org \u5e76\u8bf7\u6c42\u63d2\u4ef6\u4fe1\u606f\u3002<\/p>\n<pre>\nif ( empty( $mpi_info ) ) {\n\n\t\/**\n\t * Connect to WordPress.org using plugins_api\n\t * About plugins_api -\n\t * https:\/\/code.tutsplus.com\/tutorials\/communicating-with-the-wordpress-org-plugin-api--wp-33069\n\t *\/\n\trequire_once( ABSPATH . 'wp-admin\/includes\/plugin-install.php' );\n\t$mpi_info = plugins_api( 'plugin_information', array( 'slug' =&amp;gt; $slug ) );\n\n\t\/\/ Check for errors with the data returned from WordPress.org\n\tif ( ! $mpi_info or is_wp_error( $mpi_info ) ) {\n\t\treturn false;\n\t}\n\n\t\/\/ Set a transient with the plugin data\n\t\/\/ Use Options API with auto update cron job in next version.\n\tset_transient( $mpi_transient_name, $mpi_info, 1 * HOUR_IN_SECONDS );\n\n}\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u505a\u4e86\u4e09\u4ef6\u4e8b\uff1a<\/p>\n<ol>\n<li>\u6211\u4eec\u8fde\u63a5\u5230 WordPress.org \u5e76\u8bf7\u6c42\u63d2\u4ef6\u4fe1\u606f\u3002\u7136\u540e\u8be5\u8bf7\u6c42\u88ab\u4fdd\u5b58\u5728\u540d\u4e3a $mpi_info \u7684\u53d8\u91cf\u4e2d <\/li>\n<li>\u7136\u540e\u6211\u4eec\u8fdb\u884c\u9519\u8bef\u68c0\u67e5\uff0c\u4ee5\u786e\u4fdd\u8fd4\u56de\u7684\u6570\u636e\u662f\u5426\u6ca1\u6709\u9519\u8bef<\/li>\n<li>\u6700\u540e\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u8fc7\u671f\u65e5\u671f\u4e3a\u4e00\u5c0f\u65f6\u7684\u65b0\u77ac\u6001<\/li>\n<\/ol>\n<p>\u73b0\u5728\uff0c\u5982\u679c slug \u5c5e\u6027\u7684\u503c\u4e3a\u201cmy-plugin-information\u201d\uff0c\u90a3\u4e48\u5b58\u50a8\u63d2\u4ef6\u4fe1\u606f\u7684\u77ac\u6001\u540d\u79f0\u5c06\u4e3a\u201cmpi-my-plugin-information\u201d\u3002<\/p>\n<p><em>\u6ce8\u610f\uff1a\u8981\u4e86\u89e3\u6709\u5173 plugins_api \u7684\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u53c2\u9605\u672c\u7cfb\u5217\u7684\u7b2c\u4e00\u7bc7\u6587\u7ae0\uff0c\u5982\u672c\u6587\u9876\u90e8\u6240\u793a\u3002<\/em><\/p>\n<h3>\u663e\u793a\u63d2\u4ef6\u4fe1\u606f<\/h3>\n<p>\u6700\u540e\u4e00\u6b65\u6d89\u53ca\u6839\u636e\u201cfield\u201d\u5c5e\u6027\u7684\u503c\u8fd4\u56de\u7279\u5b9a\u4fe1\u606f\u3002\u4e3a\u6b64\uff0c\u6211\u4eec\u53ea\u9700\u4f7f\u7528\u5355\u72ec\u7684\u68c0\u67e5\u5373\u53ef\u3002<\/p>\n<pre>\nif ( $field == \"downloaded\" ) {\n\treturn $mpi_info-&amp;gt;downloaded;\n}\n\nif ( $field == \"name\" ) {\n\treturn $mpi_info-&amp;gt;name;\n}\n\nif ( $field == \"slug\" ) {\n\treturn $mpi_info-&amp;gt;slug;\n}\n\nif ( $field == \"version\" ) {\n\treturn $mpi_info-&amp;gt;version;\n}\n\nif ( $field == \"author\" ) {\n\treturn $mpi_info-&amp;gt;author;\n}\n\nif ( $field == \"author_profile\" ) {\n\treturn $mpi_info-&amp;gt;author_profile;\n}\n\nif ( $field == \"last_updated\" ) {\n\treturn $mpi_info-&amp;gt;last_updated;\n}\n\nif ( $field == \"download_link\" ) {\n\treturn $mpi_info-&amp;gt;download_link;\n}\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<hr>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u5b8c\u6574\u7684\u63d2\u4ef6\u4ee3\u7801\uff1a<\/p>\n<pre>\n&lt;?php \/*\nPlugin Name: My Plugin Information\nPlugin URI: https:\/\/code.tutsplus.com\nDescription: Communicate with WordPress.org Plugins API to retrive your Plugin Information\nVersion: 0.1.1\nAuthor: Harish\nAuthor Email: me@email.com\nLicense:\n\n  Copyright 2013 Harish\n\n  This program is free software; you can redistribute it and\/or modify\n  it under the terms of the GNU General Public License, version 3, as\n  published by the Free Software Foundation.\n\n  This program is distributed in the hope that it will be useful,\n  but WITHOUT ANY WARRANTY; without even the implied warranty of\n  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n  GNU General Public License for more details.\n\n  You should have received a copy of the GNU General Public License\n  along with this program; if not, write to the Free Software\n  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA\n\n*\/\nif ( ! defined( 'ABSPATH' ) ) exit; \/\/ Exit if accessed directly\n\nif ( ! class_exists( 'DOT_MyPluginInfo' ) )\n{\n\n\tclass DOT_MyPluginInfo {\n\n\t\t\/**\n\t\t * Constructor\n\t\t *\/\n\t\tfunction __construct() {\n\n\t\t\t\/\/Hook up to the init action\n\t\t\tadd_action( 'init', array( &amp;$this, 'init_my_plugin_info' ) );\n\n\t\t}\n\n\t\t\/**\n\t\t * Runs when the plugin is initialized\n\t\t *\/\n\t\tfunction init_my_plugin_info() {\n\n\t\t\t\/\/ Register the shortcode [mpi slug='my-plugin-info' field='version']\n\t\t\tadd_shortcode( 'mpi', array( &amp;$this, 'render_mpi' ) );\n\n\t\t}\n\n\t\tfunction render_mpi($atts) {\n\n\t\t\t\/\/ get our variable from $atts\n\t\t\textract(shortcode_atts(array(\n\t\t\t\t'slug' =&gt; '', \/\/foo is a default value\n\t\t\t\t'field' =&amp;gt; ''\n\t\t\t), $atts));\n\n\t\t\t\/**\n\t\t\t * Check if slug exists\n\t\t\t *\/\n\t\t\tif ( ! $slug ) {\n\t\t\t\treturn false;\n\t\t\t}\n\n\t\t\t\/**\n\t\t\t * Check if field exists\n\t\t\t * Return value based on the field attribute\n\t\t\t *\/\n\t\t\tif ( ! $field ) {\n\n\t\t\t\treturn false;\n\n\t\t\t} else {\n\n\t\t\t\t\/\/ Sanitize attributes\n\t\t\t\t$slug = sanitize_title( $slug );\n\t\t\t\t$field = sanitize_title( $field );\n\n\t\t\t\t\/\/ Create a empty array with variable name different based on plugin slug\n\t\t\t\t$mpi_transient_name = 'mpi' . $slug;\n\n\t\t\t\t\/**\n\t\t\t\t * Check if transient with the plugin data exists\n\t\t\t\t *\/\n\t\t\t\t$mpi_info = get_transient( $mpi_transient_name );\n\n\t\t\t\tif ( empty( $mpi_info ) ) {\n\n\t\t\t\t\t\/**\n\t\t\t\t\t * Connect to WordPress.org using plugins_api\n\t\t\t\t\t * About plugins_api -\n\t\t\t\t\t * https:\/\/code.tutsplus.com\/tutorials\/communicating-with-the-wordpress-org-plugin-api--wp-33069\n\t\t\t\t\t *\/\n\t\t\t\t\trequire_once( ABSPATH . 'wp-admin\/includes\/plugin-install.php' );\n\t\t\t\t\t$mpi_info = plugins_api( 'plugin_information', array( 'slug' =&amp;gt; $slug ) );\n\n\t\t\t\t\t\/\/ Check for errors with the data returned from WordPress.org\n\t\t\t\t\tif ( ! $mpi_info or is_wp_error( $mpi_info ) ) {\n\t\t\t\t\t\treturn false;\n\t\t\t\t\t}\n\n\t\t\t\t\t\/\/ Set a transient with the plugin data\n\t\t\t\t\t\/\/ Use Options API with auto update cron job in next version.\n\t\t\t\t\tset_transient( $mpi_transient_name, $mpi_info, 1 * HOUR_IN_SECONDS );\n\n\t\t\t\t}\n\n\t\t\t\tif ( $field == \"downloaded\" ) {\n\t\t\t\t\treturn $mpi_info-&amp;gt;downloaded;\n\t\t\t\t}\n\n\t\t\t\tif ( $field == \"name\" ) {\n\t\t\t\t\treturn $mpi_info-&amp;gt;name;\n\t\t\t\t}\n\n\t\t\t\tif ( $field == \"slug\" ) {\n\t\t\t\t\treturn $mpi_info-&amp;gt;slug;\n\t\t\t\t}\n\n\t\t\t\tif ( $field == \"version\" ) {\n\t\t\t\t\treturn $mpi_info-&amp;gt;version;\n\t\t\t\t}\n\n\t\t\t\tif ( $field == \"author\" ) {\n\t\t\t\t\treturn $mpi_info-&amp;gt;author;\n\t\t\t\t}\n\n\t\t\t\tif ( $field == \"author_profile\" ) {\n\t\t\t\t\treturn $mpi_info-&amp;gt;author_profile;\n\t\t\t\t}\n\n\t\t\t\tif ( $field == \"last_updated\" ) {\n\t\t\t\t\treturn $mpi_info-&amp;gt;last_updated;\n\t\t\t\t}\n\n\t\t\t\tif ( $field == \"download_link\" ) {\n\t\t\t\t\treturn $mpi_info-&amp;gt;download_link;\n\t\t\t\t}\n\n\t\t\t} \/\/ $field check\n\n\t\t} \/\/ render_mpi()\n\n\t} \/\/ end class\n\tnew DOT_MyPluginInfo();\n\n}\n\n?&amp;gt;\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u63d2\u4ef6\u4ee3\u7801\u53ef\u5728 GitHub \u4e0a\u627e\u5230\uff0c\u60a8\u4e5f\u53ef\u4ee5\u4ece WordPress.org \u4e0b\u8f7d<\/p>\n<hr>\n<h2>\u4ed8\u8bf8\u884c\u52a8<\/h2>\n<p>\u73b0\u5728\u60a8\u53ea\u9700\u8f6c\u5230\u5e16\u5b50\u7f16\u8f91\u5668\u5e76\u6dfb\u52a0\u4e00\u4e2a\u77ed\u4ee3\u7801\uff0c\u4f8b\u5982\uff1a<\/p>\n<pre>\nDownloaded [mpi slug='my-plugin-information' field='downloaded'] times.\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236    \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5b83\u4f1a\u663e\u793a\uff1a<\/p>\n<pre>\nDownloaded 10 times.\n<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<h3>\u663e\u793a\u6709\u5173\u63d2\u4ef6\u7684\u5176\u4ed6\u4fe1\u606f\u7684\u793a\u4f8b\u7b80\u7801<\/h3>\n<p>\u901a\u8fc7\u66ff\u6362\u201cfield\u201d\u5c5e\u6027\u7684\u503c\uff0c\u60a8\u53ef\u4ee5\u663e\u793a\u4e0d\u540c\u7684\u4fe1\u606f\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u63d2\u4ef6\u540d\u79f0\uff1a[mpi slug=&#8217;my-plugin-information&#8217; field=&#8217;name&#8217;] <\/li>\n<li>\u63d2\u4ef6\u7248\u672c\uff1a[mpi slug=&#8217;my-plugin-information&#8217; field=&#8217;version&#8217;] <\/li>\n<li>\u63d2\u4ef6 Slug\uff1a[mpi slug=&#8217;my-plugin-information&#8217; field=&#8217;slug&#8217;] <\/li>\n<li>\u63d2\u4ef6\u4f5c\u8005\uff08\u8fd4\u56de\u540d\u79f0\u548c\u94fe\u63a5\uff09\uff1a[mpi slug=&#8217;my-plugin-information&#8217; field=&#8217;author&#8217;] <\/li>\n<li>\u4f5c\u8005\u7b80\u4ecb\uff08\u8fd4\u56de\u4e2a\u4eba\u8d44\u6599\u5730\u5740\uff09\uff1a[mpi slug=&#8217;my-plugin-information&#8217; field=&#8217;author_profile&#8217;] <\/li>\n<li>\u6700\u540e\u66f4\u65b0\uff1a[mpi slug=&#8217;my-plugin-information&#8217; field=&#8217;last_updated&#8217;] <\/li>\n<li>\u4e0b\u8f7d\u94fe\u63a5\uff1a[mpi slug=&#8217;my-plugin-information&#8217; field=&#8217;download_link&#8217;] <\/li>\n<\/ul>\n<hr>\n<h2>\u6539\u8fdb<\/h2>\n<p>\u4e3a\u4e86\u7b80\u5355\u8d77\u89c1\uff0c\u6211\u4f7f\u7528\u77ac\u6001\u6765\u4fdd\u5b58\u63d2\u4ef6\u4fe1\u606f\u3002\u7136\u800c\uff0c\u77ac\u6001\u4ece\u6765\u5c31\u4e0d\u662f\u7528\u6765\u4fdd\u5b58\u91cd\u8981\u6570\u636e\u7684\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u4f7f\u7528\u9009\u9879 API\u3001add_options() \u6216\u4f5c\u4e3a post meta \u4fdd\u5b58\u63d2\u4ef6\u6570\u636e\uff0c\u7136\u540e\u5b89\u6392\u4e00\u4e2a cron \u4efb\u52a1\u6bcf\u5c0f\u65f6\u66f4\u65b0\u4e00\u6b21\u6570\u636e\u3002<\/p>\n<hr>\n<h2>\u63a5\u4e0b\u6765\u505a\u4ec0\u4e48\uff1f<\/h2>\n<p>\u4f7f\u7528 plugins_api\uff0c\u6211\u4eec\u5df2\u7ecf\u6f14\u793a\u4e86\u901a\u4fe1\u548c\u68c0\u7d22 WordPress.org \u4e0a\u6258\u7ba1\u7684\u4efb\u4f55\u63d2\u4ef6\u7684\u4fe1\u606f\u662f\u591a\u4e48\u5bb9\u6613\u3002<\/p>\n<p>\u60a8\u53ef\u80fd\u8fd8\u60f3\u67e5\u770b\u5176\u4ed6\u63d2\u4ef6\uff0c\u4f8b\u5982 Plugin Info\uff08\u4e5f\u4f7f\u7528 plugins_api \u548c I Make Plugins\uff0c\u770b\u770b\u5b83\u4eec\u5982\u4f55\u5b8c\u6210\u76f8\u540c\u7684\u4efb\u52a1\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5728\u60a8\u7684\u7f51\u7ad9\u4e0a\u5c55\u793a WordPress.org \u63d2\u4ef6\u7684\u4fe1\u606f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u672c\u6587\u7684\u7b2c\u4e00\u90e8\u5206\u4e2d\uff0c\u6211\u4eec\u8ba8\u8bba\u4e86\u5982\u4f55\u4f7f\u7528\u5185\u7f6e\u51fd\u6570\u4e0e WordPress.org \u8fdb\u884c\u901a\u4fe1\u5e76\u68c0\u7d22\u63d2\u4ef6\u8be6\u7ec6\u4fe1\u606f\u3002 \u5728\u672c\u6559\u7a0b\u4e2d\uff0c\u6211\u4eec\u5c06\u628a\u7406\u8bba\u4ed8\u8bf8\u5b9e\u8df5\uff0c\u521b\u5efa\u4e00\u4e2a\u7b80\u5355\u7684\u63d2\u4ef6\uff0c\u8be5\u63d2\u4ef6\u5c06\u5141\u8bb8\u6211\u4eec\u4f7f\u7528\u7b80\u7801\u5728\u6211\u4eec\u7684 WordPress \u7f51\u7ad9\u4e0a\u663e\u793a WordPress.org \u4e0a\u6258\u7ba1\u7684\u4efb\u4f55\u63d2\u4ef6\u7684\u8be6\u7ec6\u4fe1\u606f\u3002 p&gt; \u5f00\u59cb\u4f7f\u7528 \u6211\u5047\u8bbe\u60a8\u662f\u63d2\u4ef6\u5f00\u53d1\u4eba\u5458\u5e76\u4e14\u4e86\u89e3\u57fa\u7840\u77e5\u8bc6\uff0c\u4f46\u5982\u679c\u6709\u7591\u95ee\uff0c\u6211\u5efa\u8bae\u9605\u8bfb\u4ee5\u4e0b\u4e24\u7bc7\u6587\u7ae0\uff1a \u5f00\u53d1 WordPress \u63d2\u4ef6\u7684\u4e24\u79cd\u65b9\u6cd5\uff1a\u51fd\u6570\u5f0f\u7f16\u7a0b \u5f00\u53d1 WordPress \u63d2\u4ef6\u7684\u4e24\u79cd\u65b9\u6cd5\uff1a\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b \u6211\u4eec\u5728\u505a\u4ec0\u4e48\uff1f \u901a\u8fc7\u8fd9\u4e2a\u63d2\u4ef6\uff0c\u6211\u4eec\u60f3\u8981\u521b\u5efa\u4e00\u4e2a\u77ed\u4ee3\u7801\uff0c\u4f8b\u5982 [mpi slug=&#8217;my-plugin-information&#8217; field=&#8217;version&#8217;] \uff0c\u5b83\u53ef\u4ee5\u63a5\u53d7\u4e24\u4e2a\u5c5e\u6027\uff1a\u201cslug\u201d\u548c\u201cfield\u201d\uff0c\u7136\u540e\u57fa\u4e8e\u7136\u540e\uff0c\u6211\u4eec\u68c0\u7d22\u5e76\u663e\u793a WordPress.org \u5b58\u50a8\u5e93\u4e2d\u6258\u7ba1\u7684\u4efb\u4f55\u63d2\u4ef6\u7684\u4fe1\u606f\u3002 \u521b\u5efa\u63d2\u4ef6\u5e93 \u8ba9\u6211\u4eec\u9996\u5148\u5728 wp-content\/plugins \u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a my-plugin-information \u7684\u6587\u4ef6\u5939\u3002\u5728\u5176\u4e2d\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a my-plugin-info.php \u7684\u6587\u4ef6\uff0c\u5e76\u5c06\u4ee5\u4e0b\u4ee3\u7801\u7c98\u8d34\u5230\u5176\u4e2d\uff1a &lt;?php \/* Plugin Name: My Plugin Info Plugin URI: https:\/\/myplugininfo.com Description: Communicate with WordPress.org Plugins API to retrive your Plugin Information Version: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-32564","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32564","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=32564"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/32564\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=32564"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=32564"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=32564"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}