This is a theme based on the client's requirements.
In this project, Vanilla JS and Gutenberg Blocks were implemented.
This code manages the declaration of multiple Custom Post Types (CPTs) in WordPress, allowing the site to handle custom content effectively.
This modular approach is ideal for maintaining scalability and organization in complex projects, separating the logic of Custom Post Types into individual files for better maintenance.
/**
*
* Custom Post Types Declaration
*
*/
$post_type_includes = [
'inc/post-types/locations.php',
'inc/post-types/reservations.php',
'inc/post-types/vehicle.php',
'inc/post-types/discounts.php',
];
foreach ($post_type_includes as $file) {
if (!$filepath = locate_template($file)) {
trigger_error(sprintf(__('Error locating %s for inclusion', 'ws'), $file), E_USER_ERROR);
}
require_once $filepath;
}
This code defines a Custom Post Type (CPT) called "location" in WordPress. This content type allows for structured and customized management of locations within the site's admin area.
function create_location_post_type()
{
/**
* Post Type: Location.
*/
register_post_type(
'location',
array(
'labels' => array(
'name' => 'Locations',
'menu_name' => 'Locations',
'all_items' => 'Locations',
'singular_name' => 'Location',
'add_new' => 'New Location',
'add_new_item' => 'Add New Location'
),
'public' => false,
'publicly_queryable' => true,
'show_ui' => true,
'exclude_from_search' => true,
'show_in_nav_menus' => false,
'has_archive' => false,
'rewrite' => array('slug' => 'location'),
'taxonomies' => array(''),
'menu_icon' => 'dashicons-location',
'hiercarchical' => true,
'supports' => array('title', 'author')
)
);
}
add_action('init', 'create_location_post_type');
This code extends the functionality of the Gutenberg editor in WordPress by creating custom blocks and configuring their visibility.
This approach is ideal for projects that require custom blocks and stricter control over the available options in the editor, ensuring an optimized user experience tailored to the site's needs.
/**
*
* Gutenberg Editor - Custom Blocks
*
*/
// Create Custom Blocks Category
function custom_block_category( $categories, $post ) {
return array_merge(
$categories,
array(
array(
'slug' => THEME_FOLDER . '-blocks',
'title' => __( APP_NAME .' Blocks', THEME_FOLDER . '-blocks' ),
),
)
);
}
add_filter( 'block_categories', 'custom_block_category', 10, 2);
// Register Blocks
function register_acf_block_types(){
if ( function_exists('acf_register_block') ) {
// Hero Block
acf_register_block_type(
array(
'name' => 'hero-block',
'title' => 'Hero',
'description' => 'Hero Block',
'category' => THEME_FOLDER . '-blocks',
'icon' => 'format-image',
'keywords' => array('block', 'custom', 'hero'),
'render_template' => '/resourses/blocks/hero-block.php',
'mode' => 'edit', // Force Edit Mode
)
);
// Subscribe Block
acf_register_block_type(
array(
'name' => 'subscribe-block',
'title' => 'Subscribe',
'description' => 'Subscribe Block',
'category' => THEME_FOLDER . '-blocks',
'icon' => 'share',
'keywords' => array('block', 'custom', 'subscribe' ),
'render_template' => '/resourses/blocks/subscribe-block.php',
'mode' => 'edit', // Force Edit Mode
)
);
// Campaign Block
acf_register_block_type(
array(
'name' => 'campaign-block',
'title' => 'Campaign',
'description' => 'Campaign Block',
'category' => THEME_FOLDER . '-blocks',
'icon' => 'embed-post',
'keywords' => array('block', 'custom', 'campaign', 'posts'),
'render_template' => '/resourses/blocks/campaign-block.php',
'mode' => 'edit', // Force Edit Mode
)
);
}
}
add_action('init', 'register_acf_block_types');
// Hide most of the default blocks
function filter_allowed_block_types($allowed_blocks)
{
$custom_blocks_category = THEME_FOLDER . '-blocks';
$allowed_blocks = [
$custom_blocks_category,
"acf/campaign-block",
'acf/hero-block',
'acf/subscribe-block',
];
return $allowed_blocks;
}
add_filter('allowed_block_types', 'filter_allowed_block_types');
This code defines a custom template for a Hero Block in WordPress, designed to integrate with the Gutenberg editor and manage dynamic content.
/**
* Hero Block Template.
*/
$section_name = get_field('section_name');
$hero_image = get_field('hero_image');
This code defines a custom template for a Subscribe Block in WordPress, designed to capture user subscriptions and provide dynamic content.
/**
* Subscribe Block Template.
*/
$section_name = get_field('section_name');
$heading = get_field('heading');
$copy = get_field('copy');
This code implements a block template called "Features Grid" for a WordPress theme. It is designed to display a grid of features with a structured and dynamic layout. The content is managed through ACF (Advanced Custom Fields), allowing administrators to easily customize the block from the WordPress admin panel.
/**
* Features Grid Block Template.
*/
$section_name = get_field('section_name');
$heading = get_field('heading');
$features = get_field('features');
= $heading ?>
= $feature['feature_name'] ?>
= $feature['feature_description'] ?>