{"id":1275,"date":"2024-06-04T08:38:01","date_gmt":"2024-06-04T06:38:01","guid":{"rendered":"https:\/\/wypo.io\/utilisation-de-db_version-dans-wordpress\/"},"modified":"2024-07-26T20:14:37","modified_gmt":"2024-07-26T18:14:37","slug":"optimize-plugins-wordpress-database-version-management","status":"publish","type":"post","link":"https:\/\/wypo.io\/en\/optimize-plugins-wordpress-database-version-management\/","title":{"rendered":"Optimize your plugins: WordPress database version management"},"content":{"rendered":"\n<p class=\" hf_animated fade_bottom default \">Effectively managing the database version is crucial for any WordPress plugin developer. Whether it&#8217;s to add new features or improve performance, updating the underlying data structures of your plugin must be done methodically. In this article, we will explore how to use the <code>db_version<\/code> option in WordPress to check, update, and initialize the database version of your plugin. You will also learn how to automate these processes during your plugin&#8217;s activation, ensuring a smooth transition for your users.<\/p>\n\n\n\n<div class=\"wp-block-group is-style-smallbordertop   hf_animated fade_bottom default is-layout-constrained wp-block-group-is-layout-constrained\" style=\"margin-top:var(--wp--preset--spacing--50);padding-top:var(--wp--preset--spacing--50)\">            <div class=\"wp-block-habefast-advanced-layout-group is-layout-constrained wp-block-habefast-advanced-layout-group-is-layout-constrained\">\r\n                <div class=\"wp-block-habefast-advanced-group-container is-style- is-layout-constrained\"  style='--_hfal-cg-d-lg:flex;--_hfal-cg-d-md:flex;--_hfal-cg-d-sm:flex;'>\r\n                                        <div class=\"wp-block-habefast-advanced-group-inner is-layout-flow\">\r\n                        \n\n<h2 class=\"wp-block-heading   \" datalink=\"content-check-the-database-version\">Check the Database Version<\/h2>\n\n\n\n<p class=\"\">You can retrieve the current database version using the <code>get_option()<\/code> function:<\/p>\n\n\n    <div class=\"wp-block-habefast-code  \">\n      <header class=\"habefast-code-header\">\n        <div class=\"habefast-code-lang is-lang-php\">\n          PHP        <\/div>\n      <\/header>\n      <textarea\n        class=\"habefast-code-source\"\n        name=\"codemirror-94497697\"\n        id=\"codemirror-94497697\"\n      >$db_version = get_option(&#039;db_version&#039;);\necho &#039;Current DB Version: &#039; . $db_version;<\/textarea>\n      <script>\n        CodeMirror.fromTextArea( document.getElementById('codemirror-94497697'), {\n          mode: {name:'php',startOpen:true},\n          readOnly: true,\n          lineNumbers: true,\n          firstLineNumber: 1,\n          matchBrackets: true,\n          indentUnit: 4,\n          tabSize: 4,\n          lineWrapping: true,\n        } );\n      <\/script>\n    <\/div>\n    \n                    <\/div>\r\n                <\/div>\r\n            <\/div>\r\n            <\/div>\n\n\n\n<div class=\"wp-block-group is-style-smallbordertop   hf_animated fade_bottom default is-layout-constrained wp-block-group-is-layout-constrained\" style=\"margin-top:var(--wp--preset--spacing--50);padding-top:var(--wp--preset--spacing--50)\">            <div class=\"wp-block-habefast-advanced-layout-group is-layout-constrained wp-block-habefast-advanced-layout-group-is-layout-constrained\">\r\n                <div class=\"wp-block-habefast-advanced-group-container is-style- is-layout-constrained\"  style='--_hfal-cg-d-lg:flex;--_hfal-cg-d-md:flex;--_hfal-cg-d-sm:flex;'>\r\n                                        <div class=\"wp-block-habefast-advanced-group-inner is-layout-flow\">\r\n                        \n\n<h2 class=\"wp-block-heading   \" datalink=\"content-update-the-database-version\">Update the Database Version<\/h2>\n\n\n\n<p class=\"\">If you need to update your plugin&#8217;s database, you can check the current version and make the necessary changes:<\/p>\n\n\n    <div class=\"wp-block-habefast-code  \">\n      <header class=\"habefast-code-header\">\n        <div class=\"habefast-code-lang is-lang-php\">\n          PHP        <\/div>\n      <\/header>\n      <textarea\n        class=\"habefast-code-source\"\n        name=\"codemirror-1065542057\"\n        id=\"codemirror-1065542057\"\n      >function my_plugin_update_db_check() {\n    \/\/ Current database version\n    $current_db_version = get_option(&#039;my_plugin_db_version&#039;);\n\n    \/\/ Expected database version\n    $plugin_db_version = &#039;1.1&#039;;\n\n    \/\/ If the current version is different from the expected version, perform updates\n    if ($current_db_version != $plugin_db_version) {\n        \/\/ Make necessary database changes\n        my_plugin_update_db();\n\n        \/\/ Update the database version in the options\n        update_option(&#039;my_plugin_db_version&#039;, $plugin_db_version);\n    }\n}\n\n\/\/ Function to make necessary database changes\nfunction my_plugin_update_db() {\n    global $wpdb;\n\n    \/\/ Example update: adding a new table\n    $table_name = $wpdb-&gt;prefix . &#039;my_plugin_table&#039;;\n    $charset_collate = $wpdb-&gt;get_charset_collate();\n\n    $sql = &quot;CREATE TABLE $table_name (\n        id mediumint(9) NOT NULL AUTO_INCREMENT,\n        name tinytext NOT NULL,\n        value text NOT NULL,\n        PRIMARY KEY (id)\n    ) $charset_collate;&quot;;\n\n    require_once(ABSPATH . &#039;wp-admin\/includes\/upgrade.php&#039;);\n    dbDelta($sql);\n}\n\n\/\/ Add an action to check and update the database when the plugin is activated\nregister_activation_hook(__FILE__, &#039;my_plugin_update_db_check&#039;);\n<\/textarea>\n      <script>\n        CodeMirror.fromTextArea( document.getElementById('codemirror-1065542057'), {\n          mode: {name:'php',startOpen:true},\n          readOnly: true,\n          lineNumbers: true,\n          firstLineNumber: 1,\n          matchBrackets: true,\n          indentUnit: 4,\n          tabSize: 4,\n          lineWrapping: true,\n        } );\n      <\/script>\n    <\/div>\n    \n                    <\/div>\r\n                <\/div>\r\n            <\/div>\r\n            <\/div>\n\n\n\n<div class=\"wp-block-group   hf_animated fade_bottom default is-layout-constrained wp-block-group-is-layout-constrained\" style=\"margin-top:var(--wp--preset--spacing--50);padding-top:var(--wp--preset--spacing--50)\">            <div class=\"hf_animated none default wp-block-habefast-advanced-layout-group is-layout-constrained wp-block-habefast-advanced-layout-group-is-layout-constrained\">\r\n                <div class=\"wp-block-habefast-advanced-group-container is-style- is-layout-constrained\"  style='--_hfal-cg-d-lg:flex;--_hfal-cg-d-md:flex;--_hfal-cg-d-sm:flex;'>\r\n                                        <div class=\"wp-block-habefast-advanced-group-inner is-layout-flow\">\r\n                        \n\n<h2 class=\"wp-block-heading   \" datalink=\"content-initialize-the-database-version\">Initialize the Database Version<\/h2>\n\n\n\n<p class=\"\">When initially activating your plugin, you can set the database version:<\/p>\n\n\n    <div class=\"wp-block-habefast-code  \">\n      <header class=\"habefast-code-header\">\n        <div class=\"habefast-code-lang is-lang-php\">\n          PHP        <\/div>\n      <\/header>\n      <textarea\n        class=\"habefast-code-source\"\n        name=\"codemirror-382496776\"\n        id=\"codemirror-382496776\"\n      >function my_plugin_activate() {\n    \/\/ Set the database version\n    $plugin_db_version = &#039;1.0&#039;;\n    add_option(&#039;my_plugin_db_version&#039;, $plugin_db_version);\n\n    \/\/ Perform necessary database initializations\n    my_plugin_update_db_check();\n}\n\n\/\/ Add an action to initialize the database when the plugin is activated\nregister_activation_hook(__FILE__, &#039;my_plugin_activate&#039;);\n<\/textarea>\n      <script>\n        CodeMirror.fromTextArea( document.getElementById('codemirror-382496776'), {\n          mode: {name:'php',startOpen:true},\n          readOnly: true,\n          lineNumbers: true,\n          firstLineNumber: 1,\n          matchBrackets: true,\n          indentUnit: 4,\n          tabSize: 4,\n          lineWrapping: true,\n        } );\n      <\/script>\n    <\/div>\n    \n                    <\/div>\r\n                <\/div>\r\n            <\/div>\r\n            <\/div>\n\n\n\n<div class=\"wp-block-group   hf_animated fade_bottom default is-layout-constrained wp-block-group-is-layout-constrained\">            <div class=\"wp-block-habefast-advanced-layout-group is-layout-constrained wp-block-habefast-advanced-layout-group-is-layout-constrained\">\r\n                <div class=\"wp-block-habefast-advanced-group-container is-style- is-layout-constrained\"  style='--_hfal-cg-d-lg:flex;--_hfal-cg-d-md:flex;--_hfal-cg-d-sm:flex;'>\r\n                                        <div class=\"wp-block-habefast-advanced-group-inner is-layout-flow\">\r\n                        \n\n<h3 class=\"wp-block-heading   \" datalink=\"content-explanations\">Explanations<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li class=\"\"><strong><code>get_option('db_version')<\/code><\/strong>: Retrieves the current database version.<\/li>\n\n\n\n<li class=\"\"><strong><code>update_option('my_plugin_db_version', $version)<\/code><\/strong>: Updates the database version for your plugin.<\/li>\n\n\n\n<li class=\"\"><strong><code>register_activation_hook(__FILE__, 'function_name')<\/code><\/strong>: Adds an action to be performed when the plugin is activated.<\/li>\n<\/ul>\n\n\n\n<p class=\"\">By using these functions, you can effectively manage your WordPress plugin&#8217;s database updates using the <code>db_version<\/code> option or a plugin-specific option.<\/p>\n\n                    <\/div>\r\n                <\/div>\r\n            <\/div>\r\n            <\/div>\n","protected":false},"excerpt":{"rendered":"<p>Effectively managing the database version is crucial for any WordPress plugin developer. Whether it&#8217;s to add new features or improve performance, updating the underlying data structures of your plugin must be done methodically. In this article, we will explore how to use the db_version option in WordPress to check, update, and initialize the database version [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_habefastfse_class":"","hf_wpseo_meta_title":"Manage the version of your plugin options [separator] [sitetitle]","hf_wpseo_meta_description":"Learn how to check, update and initialize the database version of your WordPress plugin with the db_version option. An essential guide for developers wishing to improve performance and add new features methodically.","hf_wpseo_meta_robots_index":"","hf_wpseo_meta_robots_follow":true,"hf_wpseo_meta_robots_advanced":"{}","hf_wpseo_meta_canonical_url":"","footnotes":""},"categories":[28],"tags":[],"class_list":["post-1275","post","type-post","status-publish","format-standard","hentry","category-developer"],"_links":{"self":[{"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/posts\/1275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/comments?post=1275"}],"version-history":[{"count":4,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/posts\/1275\/revisions"}],"predecessor-version":[{"id":1477,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/posts\/1275\/revisions\/1477"}],"wp:attachment":[{"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/media?parent=1275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/categories?post=1275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wypo.io\/en\/wp-json\/wp\/v2\/tags?post=1275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}