-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisable-shortcode-pasting.php
55 lines (47 loc) · 2.06 KB
/
disable-shortcode-pasting.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/**
* Plugin Name: Disable Shortcode Block Creation when Pasting in Plain Text Blocks
* Description: Prevents new shortcode blocks from being inserted when pasting shortcodes into plain text blocks like paragraphs or headings.
* Version: 1.0
* Author: Joel Schlotterer
* Author URI: https://yourwebsite.com
* License: GPLv2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Requires at least: 6.5
* Tested up to: 6.6.1
* Requires PHP: 8.0
* Tags: shortcode, paste, block editor, gutenberg, content
* Text Domain: disable-shortcode-pasting
* Domain Path: /languages
*/
namespace Flexline\DisableShortcodePasting;
// Enqueue the block editor script
function enqueue_block_editor_assets() {
// Dependencies explained:
// - 'wp-blocks': Provides essential functions for registering and working with blocks.
// - 'wp-dom-ready': Ensures that the script is executed when the DOM is fully loaded.
// - 'wp-edit-post': Provides editor-level functionalities and controls used for block management.
wp_enqueue_script(
'dsp-shortcode-paste-handler',
plugin_dir_url(__FILE__) . 'shortcode-paste-handler.js',
array('wp-blocks', 'wp-dom-ready', 'wp-edit-post'), // Specify dependencies
filemtime(plugin_dir_path(__FILE__) . 'shortcode-paste-handler.js'), // Cache busting based on file modification time
true // Load in the footer for better performance
);
}
add_action('enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_block_editor_assets');
function render_block_with_shortcodes( $block_content, $block ) {
// Apply to reusable blocks and other block types if needed
$blocks_with_shortcodes = [
'core/paragraph',
'core/heading',
'core/quote',
'core/list',
'core/preformatted',
]; // Add more as needed
if ( in_array( $block['blockName'], $blocks_with_shortcodes ) ) {
$block_content = do_shortcode( $block_content );
}
return $block_content;
}
add_filter( 'render_block', __NAMESPACE__ . '\render_block_with_shortcodes', 10, 2 );