PHP code example of digital-nature / wordpress-utilities
1. Go to this page and download the library: Download digital-nature/wordpress-utilities library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
digital-nature / wordpress-utilities example snippets
// The capability
class MyNewCapability extends \DigitalNature\WordPressUtilities\Common\Users\Capabilities\BaseCapability {
/**
* @return string
*/
public static function get_capability_name(): string
{
return 'my_new_capability';
}
}
// The role
class MyNewRole extends \DigitalNature\WordPressUtilities\Common\Users\Roles\BaseRole
{
/**
* @return string
*/
public static function get_role_slug(): string
{
return 'my-new-role';
}
/**
* @return string
*/
public static function get_role_name(): string
{
return 'My New Role'
}
/**
* @return string[]
*/
public static function get_capabilities(): array
{
return [
MyNewCapability::get_capability_name()
]
}
}
// The code below shows example usage of the roles/capabilities
// Include the role
MyNewRole::add_role();
// Adding capability for an admin menu item
add_submenu_page(
'parent_slug',
'Your Page Title',
'Your Menu Title',
MyNewCapability::get_capability_name(),
'submenu_slug',
'your callback'
);
// checking capability for the logged in user
$canAccess = current_user_can( MyNewCapability::get_capability_name() );
// Define the setting class
class MySettingHelper extends \DigitalNature\WordPressUtilities\Helpers\Settings\UserSettingHelper
{
public static function get_meta_key(): string
{
return 'my_setting_metadata_key';
}
}
// switch the setting on/off
MySettingHelper::enable($user);
MySettingHelper::disable($user);
// There are aliases, as some setting names lend themselves to a different terminology
MySettingHelper::turn_on($user);
MySettingHelper::turn_off($user);
// You can check whether the setting is turned on (or not) for the given user
MySettingHelper::is_enabled($user);
MySettingHelper::is_turned_on($user);
// There is a toggle method, should you need to simply switch the setting
MySettingHelper::toggle($user);
// check if we're on the live site. This is dependent on WP_ENVIRONMENT_TYPE being defined and set to 'live'
ConfigHelper::is_live_site()
// check if we're running a script. This is dependent on DN_IS_SCRIPT being defined and set to true
ConfigHelper::is_script();
// Register your post type
CustomPostTypeHelper::register_post_type(MyModel::class, $args);
// Returns the correct model for the given post (providing we registered the post type using the helper)
$myModel = ModelFactory::from_id(123);
LogHelper::get_logs(); // all logs
LogHelper::get_last_log(); // just the last thing logged
// If we know the model class then we can directly pull through that
$myModel = MyModel::from_id(123);
// If we don't know the correct class, just the ID then we have a factory method to retrieve.
// Note that the factory method will only work for custom post types we have registered using the `CustomPostTypeHelper`
$myModel = ModelFactory::from_id(123);
$myModel->my_metadata = 'abc123';
$myModel->save_attribute('my_metadata'); // a single metadata value
$myModel->save(); // all metadata values
// Create a model note for $myModel, authored by $user
ModelNoteRepository::create($myModel, 'The content of my new note', $user);
class MyModelRepository
{
public static function create(WP_User $owner): MyModel
{
// ...
}
public static function flush_caches(MyModel $myModel): void
{
// ...
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.