{"id":57,"date":"2024-05-10T22:23:20","date_gmt":"2024-05-10T14:23:20","guid":{"rendered":"https:\/\/www.weigot.com\/?p=57"},"modified":"2024-05-10T22:23:20","modified_gmt":"2024-05-10T14:23:20","slug":"wordpress%e4%b8%ad%e5%ae%9e%e7%8e%b0%e6%8f%92%e4%bb%b6%e6%95%b0%e6%8d%ae%e5%90%8c%e6%ad%a5","status":"publish","type":"post","link":"http:\/\/www.weigot.com\/index.php\/2024\/05\/10\/wordpress%e4%b8%ad%e5%ae%9e%e7%8e%b0%e6%8f%92%e4%bb%b6%e6%95%b0%e6%8d%ae%e5%90%8c%e6%ad%a5\/","title":{"rendered":"WordPress\u4e2d\u5b9e\u73b0\u63d2\u4ef6\u6570\u636e\u540c\u6b65"},"content":{"rendered":"\n<p>\u60a8\u63d0\u4f9b\u7684\u4ee3\u7801\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u5728WordPress\u4e2d\u5b9e\u73b0\u63d2\u4ef6\u6570\u636e\u540c\u6b65\u7684\u529f\u80fd\u3002\u8be5\u529f\u80fd\u5305\u62ec\u63d2\u4ef6\u6fc0\u6d3b\u65f6\u5c06\u72b6\u6001\u548c\u6570\u636e\u4fdd\u5b58\u5728WordPress\u7684options\u8868\u4e2d\uff0c\u4ee5\u53ca\u5b9a\u671f\u540c\u6b65\u6570\u636e\u5230\u8fdc\u7a0b\u7ad9\u70b9\u548c\u4ece\u8fdc\u7a0b\u7ad9\u70b9\u83b7\u53d6\u6570\u636e\u7684\u529f\u80fd\u3002<\/p>\n\n\n\n<p>\u6839\u636e\u60a8\u63d0\u4f9b\u7684\u4ee3\u7801\uff0c\u4ee5\u4e0b\u662f\u4ee3\u7801\u793a\u4f8b\u7684\u5b8c\u6574\u7248\u672c\uff1a<\/p>\n\n\n\n<p>\u5728\u63d2\u4ef6\u6fc0\u6d3b\u65f6\u4fdd\u5b58\u63d2\u4ef6\u7684\u72b6\u6001\u548c\u6570\u636e\u5230WordPress\u7684options\u8868\u4e2d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>register_activation_hook(__FILE__, 'wp_sync_activate_my_plugin');\n\nfunction wp_sync_activate_my_plugin() {\n    if (!get_option('my_plugin_status')) {\n        add_option('my_plugin_status', 1);\n        add_option('my_plugin_data', array('title' =&gt; 'Default Title', 'content' =&gt; 'Default Content'));\n    } else {\n        update_option('my_plugin_status', 1);\n    }\n}<\/code><\/pre>\n\n\n\n<p>\u540c\u6b65\u63d2\u4ef6\u6570\u636e\u7684\u529f\u80fd\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function wp_sync_sync_my_plugin_data() {\n    \/\/ \u83b7\u53d6\u672c\u5730\u6570\u636e\n    $local_title = get_option('my_plugin_data')&#91;'title'];\n    $local_content = get_option('my_plugin_data')&#91;'content'];\n\n    \/\/ \u5c06\u672c\u5730\u6570\u636e\u53d1\u9001\u5230\u8fdc\u7a0b\u7ad9\u70b9\n    $remote_url = 'http:\/\/example.com\/wp-admin\/admin-ajax.php';\n    $remote_data = array('title' =&gt; $local_title, 'content' =&gt; $local_content, 'action' =&gt; 'wp_sync_sync_my_plugin_data');\n    wp_remote_post($remote_url, array('body' =&gt; $remote_data));\n\n    \/\/ \u83b7\u53d6\u8fdc\u7a0b\u7ad9\u70b9\u7684\u6570\u636e\n    $remote_url = 'http:\/\/remote-site.com\/wp-admin\/admin-ajax.php';\n    $remote_data = array('action' =&gt; 'wp_sync_get_my_plugin_data');\n    $response = wp_remote_post($remote_url, array('body' =&gt; $remote_data));\n\n    if (!is_wp_error($response)) {\n        \/\/ \u5982\u679c\u83b7\u53d6\u6210\u529f\uff0c\u66f4\u65b0\u672c\u5730\u6570\u636e\n        $remote_data = json_decode($response&#91;'body'], true);\n        update_option('my_plugin_data', $remote_data);\n    }\n}\n\n\/\/ \u6ce8\u518cWordPress Cron Job\uff0c\u5b9a\u671f\u540c\u6b65\u63d2\u4ef6\u6570\u636e\nadd_action('wp_sync_sync_my_plugin', 'wp_sync_sync_my_plugin_data');\n\nif (!wp_next_scheduled('wp_sync_sync_my_plugin')) {\n    wp_schedule_event(time(), 'daily', 'wp_sync_sync_my_plugin');\n}<\/code><\/pre>\n\n\n\n<p>\u5728\u8fdc\u7a0b\u7ad9\u70b9\u4e0a\u63a5\u6536\u548c\u8fd4\u56de\u6570\u636e\u7684\u529f\u80fd\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u63a5\u6536\u5e76\u66f4\u65b0\u63d2\u4ef6\u6570\u636e\nadd_action('wp_ajax_wp_sync_sync_my_plugin_data', 'wp_sync_sync_my_plugin_data');\n\nfunction wp_sync_sync_my_plugin_data() {\n    \/\/ \u83b7\u53d6\u53d1\u9001\u8fc7\u6765\u7684\u6570\u636e\n    $remote_title = $_POST&#91;'title'];\n    $remote_content = $_POST&#91;'content'];\n\n    \/\/ \u66f4\u65b0\u672c\u5730\u6570\u636e\n    update_option('my_plugin_data', array('title' =&gt; $remote_title, 'content' =&gt; $remote_content));\n\n    \/\/ \u8fd4\u56de\u6210\u529f\u72b6\u6001\n    echo 'success';\n    wp_die();\n}\n\n\/\/ \u8fd4\u56de\u63d2\u4ef6\u6570\u636e\nadd_action('wp_ajax_wp_sync_get_my_plugin_data', 'wp_sync_get_my_plugin_data');\n\nfunction wp_sync_get_my_plugin_data() {\n    \/\/ \u83b7\u53d6\u672c\u5730\u6570\u636e\n    $local_title = get_option('my_plugin_data')&#91;'title'];\n    $local_content = get_option('my_plugin_data')&#91;'content'];\n\n    \/\/ \u8fd4\u56de\u6570\u636e\n    echo json_encode(array('title' =&gt; $local_title, 'content' =&gt; $local_content));\n    wp_die();\n}<\/code><\/pre>\n\n\n\n<p>\u8bf7\u6ce8\u610f\uff0c\u4e0a\u8ff0\u4ee3\u7801\u4ec5\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u60a8\u9700\u8981\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u9002\u5f53\u7684\u4fee\u6539\u548c\u8c03\u6574\uff0c\u4ee5\u786e\u4fdd\u5176\u4e0e\u60a8\u7684\u63d2\u4ef6\u548c\u73af\u5883\u76f8\u5339\u914d\u3002\u6b64\u5916\uff0c\u60a8\u8fd8\u9700\u8981\u786e\u4fdd\u8fdc\u7a0b\u7ad9\u70b9\u7684\u63a5\u6536\u548c\u8fd4\u56de\u6570\u636e\u7684\u529f\u80fd\u80fd\u591f\u6b63\u786e\u5904\u7406\u8bf7\u6c42\u548c\u54cd\u5e94\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u60a8\u63d0\u4f9b\u7684\u4ee3\u7801\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u5728WordPress\u4e2d\u5b9e\u73b0\u63d2\u4ef6\u6570\u636e\u540c\u6b65\u7684\u529f\u80fd\u3002\u8be5\u529f\u80fd\u5305\u62ec\u63d2\u4ef6\u6fc0\u6d3b\u65f6\u5c06\u72b6\u6001\u548c\u6570\u636e\u4fdd\u5b58\u5728 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-57","post","type-post","status-publish","format-standard","hentry","category-suibianjilu"],"_links":{"self":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/57","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/comments?post=57"}],"version-history":[{"count":1,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/57\/revisions"}],"predecessor-version":[{"id":58,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/posts\/57\/revisions\/58"}],"wp:attachment":[{"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/media?parent=57"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/categories?post=57"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weigot.com\/index.php\/wp-json\/wp\/v2\/tags?post=57"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}