v2 Documentation

Data Stores

At some point in developing a WordPress theme you’ll need to store some text or a simple array in one place and then use it in another. With Data Stores you can store data temporarily over a page load, in a user’s cookie, or permanently in the database.

Cookies

Storing data as a cookie is useful when you'd like to customize a setting to a specific user but maybe they do not have an account. As an example, you could have let a visitor add pages of the site to a "collection" and then let them take the list and print it. Here you could store the page IDs and then, when a user is ready, get the page IDs from the cookie and pull the pages. Because cookies are stored on a client's system you'll want to validate all cookie data before using it.

To store data as a cookie:

// string
wpm('store.cookie')->set('collection_name', 'My Favorite Content');

// boolean
wpm('store.cookie')->set('show_sidebar', false);

// integer
wpm('store.cookie')->set('current_page', 182);

// simple array
wpm('store.cookie')->set('collection', [155, 200, 146]);

WPM will encode your cookie into json when storing and decode when getting.

To recall any cookie store:

// echo
echo wpm('store.cookie')->get('show_sidebar');

// return
$collection = wpm('store.cookie')->get('collection');

With cookies you'll want the ability to remove a single variable or all variables from the cookie.

// remove the collection from the cookie
wpm('store.cookie')->remove('collection');

// remove all information stored in the cookie
wpm('store.cookie')->removeAll();

You can check to see if the cookie has a variable with the has method.

if(wpm('store.cookie')->has('collection')) {
    // do something because 'collection' are stored
}

Forgot what you called it? Grab everything to take a look:

dd(wpm('store.all')->all());

By default, the cookie has a name of WpmCookieStore. You can change this by adding the name before any get, set or remove call.

wpm('store.cookie')->name('my_cookie')->set('show_sidebar', false);

$show_sidebar = wpm('store.cookie')->name('my_cookie')->get('show_sidebar');

wpm('store.cookie')->removeAll();

There are a few other helpful methods to control basic cookie settings. To set all of these at once, configure each and then call the create method. This method will create an empty cookie.


// set expiration time to 1 day (default is 30 days)
wpm('store.cookie')->expires(time() + 86400)->create();

// change the cookie's domain (default is "")
wpm('store.cookie')->domain('sub.domain.com')->create();

// path change the location of the cookie (default is "/")
wpm('store.cookie')->path('/my-login/')->create();

// change secure flag (default is false)
wpm('store.cookie')->secure(true)->create();

// change the http_only flag (default is false)
wpm('store.cookie')->httpOnly(true)->create();

// set all these at once for a cookie named 'user_prefs'
wpm('store.cookie')->name('user_prefs')
    ->expires(time() + 86400)
    ->domain('sub.domain.com')
    ->path('/my-account/')
    ->secure(true)
    ->httpOnly(false)
    ->create();

Database

Variables work great over the course of a request's lifecycle but sometimes you may need to store simple data more long term. In these instances, WPM offers a database store.

To set a db store:

// string
wpm('store.db')->set('background_color', '#cecece');

// boolean
wpm('store.db')->set('display_sidebar', true);

// integer
wpm('store.db')->set('default_banner_image', 182);

// simple array
wpm('store.db')->set('social_channel_urls', [
    'twitter'  => 'http://twitter.com',
    'facebook' => 'http://facebook.com'
]);

WPM uses serialization to store information in the database.

To recall any database store:

// echo
echo wpm('store.db')->get('background_color');

// return
$social_urls = wpm('store.db')->get('social_channel_urls');

You can check to see if the database has a variable with the has method.

if(wpm('store.database')->has('social_channel_urls')) {
    // do something because 'social_channel_urls' are stored
}

Forgot what you called it? Grab everything to take a look:

dd(wpm('store.db')->all());

Variables

With variables, you can store data temporarily during a page load. As a example, you may want to set a title in you template file, like an alternate title, and display it in the header.

WordPress uses global variables heavily but they can quickly muddy your global namespace. Instead, throw them in WPM and fish them out when you need them.

To set a variable:

// string
wpm('store.var')->set('alt_title', 'About The Team');

// boolean
wpm('store.var')->set('show_sidebar', true);

// integer
wpm('store.var')->set('banner_image_id', 182);

// array
wpm('store.var')->set('alert', [
    'type'  => 'danger',
    'title' => 'Do not press the button!'
]);

Anything that can be stored as a variable can be stored as in WPM. To recall any variable:

// echo
echo wpm('store.var')->get('alt_title');

// return
$alert = wpm('store.var')->get('alert');

You can check to see if the variable exists with the has method.

if(wpm('store.var')->has('alt_title')) {
    // do something because 'alt_title' are stored
}

Forgot what you called it? Grab everything to take a look:

dd(wpm('store.var')->all());

© Copyright 2019 WPMachine.Co. All Rights Reserved.