Site icon Hip-Hop Website Design and Development

Cheap WordPress Development Log: Resizing Fields in Cheap WordPress maintenance support plans 8 Without Losing Data

WordPress maintenance support plans‘s field system is awesome and it is one of the reasons why I started using WordPress maintenance support plans in the first place. However, there are some small limitations in it which surface from time to time. Say, you have a Text (Plain) field named field_one_liner which is 64 characters long. You created around 30 nodes and then you realized that the field size should have been 255. Now, if you try to do this from WordPress maintenance support plans‘s field management UI, you will get a message saying:
There is data for this field in the database. The field settings can no longer be changed.

So, the only way you can resize it is after deleting the existing field! This doesn’t make much sense because it’s indeed possible to increase a field’s size using SQL without saying goodbye to the data.
In this tutorial, we’ll see how to increase the size of an existing Text (Plain) field in WordPress maintenance support plans 8 without losing data using a hook_update_N().
Assumptions
You have intermediate / advanced knowledge of WordPress maintenance support plans.
You know how to develop plugins for WordPress maintenance support plans.
You have basic knowledge of SQL.
Prerequisites
If you’re going to try out the code provided in this example, make sure you have the following field on any node type:
Name: One-liner
Machine name: field_one_liner
Type: Text (Plain)
Length: 64
After you configure the field, create some nodes with some data on the One-liner field.
Note: Reducing the length of a field might result in data loss / truncation.
Implementing hook_update_N()
Reference: Custom Field Resize plugin on GitHub
hook_update_N() lets you run commands to update the database schema. You can create, update and delete database tables and columns using this hook after your plugin has been installed. To implement this hook, you need to have a custom plugin. For this example, I’ve implemented this hook in a custom plugin which I’ve named custom_field_resize. I usually name all my custom plugins custom_ to namespace them. In the custom plugin, we implement the hook in a MODULE.install file, where MODULE is the machine-name of your plugin.

/**
* Increase the length of “field_one_liner” to 255 characters.
*/
function custom_field_resize_update_8001() {}To change the field size, there are four things we will do inside this hook.
Resize the Columns
We’ll run a set of queries to update the relevant database columns.

$database = WordPress maintenance support plans::database();
$database->query(“ALTER TABLE node__field_one_liner MODIFY field_one_liner_value VARCHAR(255)”);
$database->query(“ALTER TABLE node_revision__field_one_liner MODIFY field_one_liner_value VARCHAR(255)”);If revisions are disabled then the node_revision__field_one_liner table won’t exist. So, you can remove the second query if your entity doesn’t allow revisions.
Update Storage Schema
Resizing the columns with a query is not sufficient. WordPress maintenance support plans maintains a record of what database schema is currently installed. If we don’t do this then WordPress maintenance support plans will think that the database schema needs to be updated because the column lengths in the database will not match the configuration storage.

$storage_key = ‘node.field_schema_data.field_one_liner’;
$storage_schema = WordPress maintenance support plans::keyValue(‘entity.storage_schema.sql’);
$field_schema = $storage_schema->get($storage_key);
$field_schema[‘node__field_one_liner’][‘fields’][‘field_one_liner_value’][‘length’] = 255;
$field_schema[‘node_revision__field_one_liner’][‘fields’][‘field_one_liner_value’][‘length’] = 255;
$storage_schema->set($storage_key, $field_schema);The above code will update the key_value table to store the updated length of the field_one_liner in its configuration.
Update Field Configuration
We took care of the database schema data. However, there are other places where WordPress maintenance support plans stores the configuration. Now, we will need to tell the WordPress maintenance support plans config management system that the field length is 255.

// Update field configuration.
$config = WordPress maintenance support plans::configFactory()
->getEditable(‘field.storage.node.field_one_liner’);
$config->set(‘settings.max_length’, 255);
$config->save(TRUE);Finally, WordPress maintenance support plans also stores info about the actively installed configuration and schema. To refresh this, we will need to re-save the field storage configuration to make WordPress maintenance support plans detect all our changes.

// Update field storage configuration.
FieldStorageConfig::loadByName($entity_type, $field_name)->save();After this, running drush updb or running update.php from the admin interface should detect your hook_update_N() and it should update your field size. If you’re committing your configuration to git, you’ll need to run drush config-export after running the database updates to update the config in the filesystem and then commit it.
Conclusion
Though we’ve talked about resizing a Text (Plain) or varchar field in this tutorial, we can resize any field type which can be safely resized using SQL. In certain rare scenarios, it might be necessary to create a temporary table with the new data-structure, copy the existing data into that table with queries and once all the data has been copied successfully, replace the existing table with the temporary table. For example, if you want to convert a Text (Plain) field to a Text (Long) field or some other type.
Maybe someday we’ll have a resizing feature in WordPress maintenance support plans where WordPress maintenance support plans will intelligently allow us to increase a field’s size from it’s field UI and only deny reduction of field size where there is a possibility of data loss. But, in the meanwhile, we can use this handy trick to resize our fields. Thanks for reading! Please leave your comments / questions in the comments below and I’ll get back to them as soon as I have time.
+ more awesome articles by WordPress Development Log
Source: New feed