Skip to content

Instantly share code, notes, and snippets.

@djcowan
Created December 9, 2024 04:42
Show Gist options
  • Save djcowan/1100d23172469679ffe68cfd166ebac3 to your computer and use it in GitHub Desktop.
Save djcowan/1100d23172469679ffe68cfd166ebac3 to your computer and use it in GitHub Desktop.
Wordpress custom form css loader for non gutenberg forms
<?php
declare(strict_types=1);
namespace imageDirect;
/**
* Inline Block Styles
* @package imagedirect-phn-pathway
* @since 1.0.13
* ---------------- */
defined('ABSPATH') || exit();
class Custom_Form_Block_Styles
{
/**
* Enqueue Contact Form Seven (cf7) styles
* -if- [contact-form-7] shortcode is present
* @since 1.1.0
* ---------------- */
/**
* Load additional style sheet
* @since 1.0.17
* ---------------- */
function contact_form_seven_styles(): void
{
\add_action('wp_enqueue_scripts', function (): void {
\wp_register_style(
'imagedirect-contact-form-seven',
\get_theme_file_uri('assets/css/imagedirect-contact-form-seven.min.css'),
['contact-form-7'],
false
);
if (\has_shortcode(get_the_content(), 'contact-form-7')) {
\wp_enqueue_style('imagedirect-contact-form-seven');
}
});
}
/**
* Enqueue Forminator styles
* -if- [forminator_form] shortcode is present
* @since 1.0.17
* ---------------- */
public function forminator_styles(): void
{
/**
* Enqueue Forminator styles
* -if- [forminator_form] shortcode is present
* @since 1.0.17
*/
\add_action('wp_enqueue_scripts', function (): void {
\wp_register_style(
'imagedirect-forminator-pro',
\get_theme_file_uri('assets/css/imagedirect-forminator-ui-pro.min.css'),
['forminator-utilities'],
false
);
if (\has_shortcode(\get_the_content(), 'forminator_form')) {
\wp_enqueue_style('imagedirect-forminator-pro');
\wp_enqueue_style('jquery-ui-datepicker');
}
});
/**
* Forminator Gutenberg Editor Styles
* -if- Forminator form block is present
* @since 1.1.17
*/
\add_action('render_block_forminator/forms', function (string $block_content, array $block): string {
if ($block['blockName'] == 'forminator/forms') {
\add_editor_style('./assets/css/imagedirect-forminator-ui-pro.min.css');
}
return $block_content;
}, 10, 2);
/**
* Detect form in template and template parts
* @TODO not working in editor 2024-06-05
* @since 1.3.1
*/
\add_filter('render_block', function ($block_content, $block) {
// Make sure we have the blockName.
if (empty($block['blockName'])) {
return $block_content;
}
if ('forminator/forms' == $block['blockName']) {
\wp_enqueue_style('imagedirect-forminator-pro');
\add_editor_style('./assets/css/imagedirect-forminator-ui-pro.min.css');
}
if (\has_shortcode($block_content, 'forminator_form')) {
\wp_enqueue_style('imagedirect-forminator-pro');
}
return $block_content;
}, 10, 2);
}
}
\add_action('init', function (): void {
if (!function_exists('is_plugin_active')) {
require_once(ABSPATH . '/wp-admin/includes/plugin.php');
}
$Forms = new Custom_Form_Block_Styles();
// Forminator
if (\is_plugin_active('forminator-pro/forminator.php') || \is_plugin_active('forminator/forminator.php')) {
$Forms->forminator_styles();
}
// Contact Form Seven
if (\is_plugin_active('contact-form-7/wp-contact-form-7.php')) {
$Forms->contact_form_seven_styles();
}
}, 100);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment