Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
jon13

Use of Local Storage in multiple plugins

My situation, I think, is a common one. I have two plugins that each use Local storage with Admin Columns Pro to automatically load columns, each of those plugins for a particular content type.

My code looks like this:

add_filter( 'acp/storage/file/directory/writable', '__return_false' ); //! CHANGE TO __return_true TO MAKE CHANGES
add_filter( 'acp/storage/file/directory', 'apartmentsync_acp_storage_file_directory' );
function apartmentsync_acp_storage_file_directory( $path ) {
	// Use a writable path, directory will be created for you
    return APARTMENTSYNC_DIR . '/acp-settings';
}

And it’s working great when either of these plugins are used. However, when BOTH plugins are used, only one of the content types loads its columns (the one that loads last).

I think what I need is your advanced setup here:
https://docs.admincolumns.com/article/58-how-to-setup-local-storage

However, the code sample applies to themes, not plugins, and unfortunately is just unscrutable to be as to what it does. It also appears to be targeting where things are *stored*, but I’m not sure if it’s allowing for multiple different load locations.

Would it be possible for someone to write a code sample that fits this sort of situation? Ive tried and failed, unfortunately. I’d wager that this is a relatively common need, as the people using ACP, I’d think, would also be likely to be developing plugins for setting up various content types (e.g. I have a staff plugin, a partners plugin, jobs plugin, etc.).

3 years, 7 months ago
jon13

Oh, and I’ll definitely still be working on this, as I have a conflict right now I need to get resolved. If I do, I’ll be happy to post sample code in this thread.

3 years, 7 months ago
jon13

Well, I actually got it!

Here’s the example that would have helped me to get to this place more easily:

In this example, we’re using Admin Columns Pro’s Local Storage feature in two separate plugins, either of which could be installed alongside other themes or plugins using ACP’s Local storage.

The first plugin registers a staff content type, with associated columns, and the second plugin registers three content types: floorplans, properties, and neighborhoods. If both plugins were using the simple version of the ACP Local storage implementation, only the fields from the staff content type (because that plugin loads later in alphabetical order) would be available, since they’d both be trying to load their fields from the same location (set by the first plugin then, unfortunately, overwritten by the second – or by a theme). Therefore, it’s best practice when writing plugins for distribution to always use the advanced version of the setup.

In the staff plugin, here’s the code we use:

https://gist.github.com/jonschr/7c2eae0708eea6ed01fc0a7e65af459f

In the floorplans plugin, here’s the code we use:

https://gist.github.com/jonschr/e3b1d3a262eb68bff64a9c0f3c0fef48

3 years, 7 months ago
Stefan van den Dungen Gronovius
Developer

You’re correct that you need to use the Advanced setup, since this only allows you to use multiple storage locations.
The example is set with rules and a variable for the write status (where you use a hook in your example). Although the example is indeed written for a theme, the only difference for a plugin will be the location, which you already have with a define. You can use the following snippet for your first plugin and write a similar hook for your other plugin.

add_filter( 'acp/storage/repositories', function ( $repositories, $factory ) {
	$repositories['plugin_x'] = $factory->create( APARTMENTSYNC_DIR . '/acp-settings', false );

	return $repositories;
}, 20, 2 );

In the example, I made the storage read-only. But if you’re developing, you probably make them writable. When you have both plugins activated on your dev environment, you could use rules as in the example on the documentation to decide where certain column sets will be stored. I hope this will gives you an idea of how to use the advanced setup to setup multiple storage location (for read only in this case)

3 years, 7 months ago

You must be logged in to reply to this topic.