You don’t, or at least not in the way you expect. If the code in the question worked as expected, it would still be committing a mortal performance sin, database writes on the frontend.
So lets take the relevant code:
$cat_id = $_GET['tag_ID'];
if (metadata_exists('term', $cat_id, 'cat_settings' )) {
$catSettings = get_term_meta($cat_id, 'cat_settings', false);
} else {
$defaultdata = array( 'cat_settings' =>
array(
'everyoneCompose' => true,
'restrictedGroup' => true
)
);
add_term_meta($cat_id, 'cat_settings', $defaultdata, false);
}
Remove the unnecessary metadata_exists
call:
$cat_id = $_GET['tag_ID'];
$catSettings = get_term_meta($cat_id, 'cat_settings', false);
And make use of the error value that gets returned if nothing was found:
$cat_id = $_GET['tag_ID'];
$catSettings = get_term_meta($cat_id, 'cat_settings', false);
if ( empty( $catSettings ) ) {
$catSettings = array( 'cat_settings' =>
array(
'everyoneCompose' => true,
'restrictedGroup' => true
)
);
}
This still leaves the code vulnerable to times when the settings are defined but keys are missing though, you should check if values exist before you use them.
However it also points to a deeper issue
Storing Serialised Data
This is bad, instead of storing an array with 2 values in term meta, just store 2 term meta, there’s no reason you couldn’t have done this:
$everyoneCompose = get_term_meta( $cat_id, 'everyoneComposer', true );
$restrictedGroup = get_term_meta( $cat_id, 'restrictedGroup', true );