Posted in wordpress
749
6:38 am, February 27, 2025
 

wp simple blocks plugin

<p>Just saving this here for later testing:</p>
<p>This is just AI generated crap that does not work, ignore it... ;)</p>
<p>&nbsp;</p>
<p>Absolutely, let&rsquo;s start fresh with a simple WordPress plugin that loads a single custom Gutenberg block. This version will be straightforward, and you can manually add more blocks to the index.php file as needed. We&rsquo;ll keep it minimal and focused on getting one block working reliably, then you can scale it up by duplicating the pattern.</p>
<p>---</p>
<p>### Fresh Plugin: Single Block Version</p>
<p>#### Directory Structure<br />``<br />/wp-content/plugins/simple-gutenberg-block/<br />&nbsp; &nbsp; index.php &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Main plugin file<br />&nbsp; &nbsp; /blocks/<br />&nbsp; &nbsp; &nbsp; &nbsp; /simple-block/<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; block.js &nbsp; // Block definition<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; block.css &nbsp;// Optional styling<br />`</p>
<p>####
index.php<br />`php<br />&lt;?php<br />/<br />Plugin Name: Simple Gutenberg Block<br />Description: A simple plugin to load a single custom Gutenberg block<br />Version: 1.0.0<br />Author: Grok<br />/</p>
<p>// Prevent direct access<br />if (!defined('ABSPATH')) {<br />&nbsp; &nbsp; exit;<br />}</p>
<p>// Enqueue block editor assets<br />function simplegutenbergblockscripts() {<br />&nbsp; &nbsp; // Enqueue the block JavaScript<br />&nbsp; &nbsp; wpenqueuescript(<br />&nbsp; &nbsp; &nbsp; &nbsp; 'simple-block-script',<br />&nbsp; &nbsp; &nbsp; &nbsp; pluginsurl('/blocks/simple-block/block.js', FILE),<br />&nbsp; &nbsp; &nbsp; &nbsp; array('wp-blocks', 'wp-element', 'wp-editor'), // Dependencies<br />&nbsp; &nbsp; &nbsp; &nbsp; filemtime(plugindirpath(FILE) . 'blocks/simple-block/block.js'), // Version for cache busting<br />&nbsp; &nbsp; &nbsp; &nbsp; true // Load in footer<br />&nbsp; &nbsp; );</p>
<p>&nbsp; &nbsp; // Enqueue the block CSS (optional)<br />&nbsp; &nbsp; wpenqueuestyle(<br />&nbsp; &nbsp; &nbsp; &nbsp; 'simple-block-style',<br />&nbsp; &nbsp; &nbsp; &nbsp; pluginsurl('/blocks/simple-block/block.css', FILE),<br />&nbsp; &nbsp; &nbsp; &nbsp; array('wp-edit-blocks'),<br />&nbsp; &nbsp; &nbsp; &nbsp; filemtime(plugindirpath(FILE) . 'blocks/simple-block/block.css')<br />&nbsp; &nbsp; );<br />}<br />addaction('enqueueblockeditorassets', 'simplegutenbergblockscripts');</p>
<p>// Register the block<br />function simplegutenbergblockregister() {<br />&nbsp; &nbsp; registerblocktype('simple-gutenberg/simple-block', array(<br />&nbsp; &nbsp; &nbsp; &nbsp; 'editorscript' =&gt; 'simple-block-script', // Links to the enqueued JS<br />&nbsp; &nbsp; &nbsp; &nbsp; 'style' =&gt; 'simple-block-style', &nbsp; &nbsp; &nbsp; &nbsp; // Links to the enqueued CSS<br />&nbsp; &nbsp; ));<br />}<br />addaction('init', 'simplegutenbergblockregister');<br />
`</p>
<p>####
blocks/simple-block/block.js<br />`javascript<br />const { registerBlockType } = wp.blocks;<br />const { RichText } = wp.blockEditor;</p>
<p>registerBlockType('simple-gutenberg/simple-block', {<br />&nbsp; &nbsp; title: 'Simple Block',<br />&nbsp; &nbsp; icon: 'smiley',<br />&nbsp; &nbsp; category: 'common',<br />&nbsp; &nbsp; attributes: {<br />&nbsp; &nbsp; &nbsp; &nbsp; content: {<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type: 'string',<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; source: 'html',<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; selector: 'p',<br />&nbsp; &nbsp; &nbsp; &nbsp; },<br />&nbsp; &nbsp; },<br />&nbsp; &nbsp; edit: (props) =&gt; {<br />&nbsp; &nbsp; &nbsp; &nbsp; const { attributes: { content }, setAttributes } = props;<br />&nbsp; &nbsp; &nbsp; &nbsp; return (<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;RichText<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tagName="p"<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value={content}<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; onChange={(newContent) =&gt; setAttributes({ content: newContent })}<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; placeholder="Enter your message here..."<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; );<br />&nbsp; &nbsp; },<br />&nbsp; &nbsp; save: (props) =&gt; {<br />&nbsp; &nbsp; &nbsp; &nbsp; const { attributes: { content } } = props;<br />&nbsp; &nbsp; &nbsp; &nbsp; return (<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;RichText.Content<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tagName="p"<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value={content}<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /&gt;<br />&nbsp; &nbsp; &nbsp; &nbsp; );<br />&nbsp; &nbsp; },<br />});<br />
`</p>
<p>####
blocks/simple-block/block.css (Optional)<br />`css<br />.wp-block-simple-gutenberg-simple-block {<br />&nbsp; &nbsp; padding: 20px;<br />&nbsp; &nbsp; background-color: #f5f5f5;<br />&nbsp; &nbsp; border: 1px solid #ddd;<br />&nbsp; &nbsp; border-radius: 4px;<br />}<br />`</p>
<p>---</p>
<p>### Installation Steps<br />1. Create the Folder:<br />&nbsp; &nbsp;- In your WordPress installation, go to
/wp-content/plugins/.<br />&nbsp; &nbsp;- Create a new folder named simple-gutenberg-block.</p>
<p>2. Add the Files:<br />&nbsp; &nbsp;- Save the first code block as
index.php in /wp-content/plugins/simple-gutenberg-block/.<br />&nbsp; &nbsp;- Create a blocks subfolder: /wp-content/plugins/simple-gutenberg-block/blocks/.<br />&nbsp; &nbsp;- Create a simple-block subfolder: /wp-content/plugins/simple-gutenberg-block/blocks/simple-block/.<br />&nbsp; &nbsp;- Save the second code block as block.js in blocks/simple-block/.<br />&nbsp; &nbsp;- Save the third code block as block.css in blocks/simple-block/ (optional).</p>
<p>3. Activate the Plugin:<br />&nbsp; &nbsp;- Go to Plugins &gt; Installed Plugins in your WordPress admin panel.<br />&nbsp; &nbsp;- Find "Simple Gutenberg Block" and click "Activate".</p>
<p>4. Test in the Editor:<br />&nbsp; &nbsp;- Go to Pages &gt; Add New or edit an existing page.<br />&nbsp; &nbsp;- Click the "+" button to open the block inserter.<br />&nbsp; &nbsp;- Search for "Simple Block" or look under the "Common Blocks" category.<br />&nbsp; &nbsp;- Add the block and type a message to test it.</p>
<p>---</p>
<p>### Adding More Blocks Manually<br />If this works and you want to add another block later, you can duplicate the pattern in
index.php. For example, to add a fancy-block:</p>
<p>1. Create the New Block Files:<br />&nbsp; &nbsp;- Add
/blocks/fancy-block/block.js (copy simple-block/block.js and adjust the name to simple-gutenberg/fancy-block and title).<br />&nbsp; &nbsp;- Add /blocks/fancy-block/block.css (optional).</p>
<p>2. Update
index.php:<br />&nbsp; &nbsp;`php<br />&nbsp; &nbsp;&lt;?php<br />&nbsp; &nbsp;/<br />&nbsp; &nbsp;Plugin Name: Simple Gutenberg Block<br />&nbsp; &nbsp;Description: A simple plugin to load custom Gutenberg blocks<br />&nbsp; &nbsp;Version: 1.0.0<br />&nbsp; &nbsp;Author: Grok<br />&nbsp; &nbsp;/<br />&nbsp; &nbsp;if (!defined('ABSPATH')) {<br />&nbsp; &nbsp; &nbsp; &nbsp;exit;<br />&nbsp; &nbsp;}</p>
<p>&nbsp; &nbsp;function simplegutenbergblockscripts() {<br />&nbsp; &nbsp; &nbsp; &nbsp;// First block: simple-block<br />&nbsp; &nbsp; &nbsp; &nbsp;wpenqueuescript(<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'simple-block-script',<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pluginsurl('/blocks/simple-block/block.js', FILE),<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;array('wp-blocks', 'wp-element', 'wp-editor'),<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;filemtime(plugindirpath(FILE) . 'blocks/simple-block/block.js'),<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;true<br />&nbsp; &nbsp; &nbsp; &nbsp;);<br />&nbsp; &nbsp; &nbsp; &nbsp;wpenqueuestyle(<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'simple-block-style',<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pluginsurl('/blocks/simple-block/block.css', FILE),<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;array('wp-edit-blocks'),<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;filemtime(plugindirpath(FILE) . 'blocks/simple-block/block.css')<br />&nbsp; &nbsp; &nbsp; &nbsp;);</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;// Second block: fancy-block<br />&nbsp; &nbsp; &nbsp; &nbsp;wp
enqueuescript(<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'fancy-block-script',<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pluginsurl('/blocks/fancy-block/block.js', FILE),<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;array('wp-blocks', 'wp-element', 'wp-editor'),<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;filemtime(plugindirpath(FILE) . 'blocks/fancy-block/block.js'),<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;true<br />&nbsp; &nbsp; &nbsp; &nbsp;);<br />&nbsp; &nbsp; &nbsp; &nbsp;wpenqueuestyle(<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'fancy-block-style',<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;pluginsurl('/blocks/fancy-block/block.css', FILE),<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;array('wp-edit-blocks'),<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;filemtime(plugindirpath(FILE) . 'blocks/fancy-block/block.css')<br />&nbsp; &nbsp; &nbsp; &nbsp;);<br />&nbsp; &nbsp;}<br />&nbsp; &nbsp;addaction('enqueueblockeditorassets', 'simplegutenbergblockscripts');</p>
<p>&nbsp; &nbsp;function simplegutenbergblockregister() {<br />&nbsp; &nbsp; &nbsp; &nbsp;// Register simple-block<br />&nbsp; &nbsp; &nbsp; &nbsp;registerblocktype('simple-gutenberg/simple-block', array(<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'editorscript' =&gt; 'simple-block-script',<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'style' =&gt; 'simple-block-style',<br />&nbsp; &nbsp; &nbsp; &nbsp;));</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp;// Register fancy-block<br />&nbsp; &nbsp; &nbsp; &nbsp;registerblocktype('simple-gutenberg/fancy-block', array(<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'editorscript' =&gt; 'fancy-block-script',<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'style' =&gt; 'fancy-block-style',<br />&nbsp; &nbsp; &nbsp; &nbsp;));<br />&nbsp; &nbsp;}<br />&nbsp; &nbsp;addaction('init', 'simplegutenbergblockregister');<br />&nbsp; &nbsp;
`</p>
<p>3. Repeat as Needed:<br />&nbsp; &nbsp;- Add more blocks by duplicating the
wp
enqueuescript, wpenqueuestyle, and registerblocktype calls with unique handles and paths.</p>
<p>---</p>
<p>### Troubleshooting Tips<br />If the block doesn&rsquo;t appear:<br />- Check Activation: Ensure "Simple Gutenberg Block" is active in the Plugins list.<br />- Verify Paths: Double-check the folder structure matches exactly as above.<br />- Debug Loading:<br />&nbsp; - Add
error
log('Script URL: ' . pluginsurl('/blocks/simple-block/block.js', FILE)); in simplegutenbergblockscripts() and check wp-content/debug.log after enabling WPDEBUG and WPDEBUG_LOG.<br />- Console Errors: Open the editor, press F12 &gt; Console, and look for errors.<br />- Clear Cache: Hard refresh (Ctrl+F5) the editor page.</p>
<p>---</p>
<p>### Why This Should Work<br />- It&rsquo;s a minimal setup with no dynamic loading&mdash;just one block hardcoded.<br />- The namespace (
simple-gutenberg) is consistent across PHP and JS.<br />- File paths are explicitly defined relative to index.php`.</p>
<p>Let me know if this works or if you hit any snags! We&rsquo;ll get that first block showing up, then you can add more at your pace.</p>

View Statistics
This Week
238
This Month
671
This Year
1283

No Items Found.

Add Comment
Type in a Nick Name here
 
Other Items in wordpress
wp simple blocks plugin Wordpress Use the settings API Wordpress add a sub menu to your plugin Disable the ACF Warning message in wordpress dashboard load a page as a menu in wordpress extract a wordpress menu using its id Wordpress template with fixed sidebar in flex add a page template in a wordpress theme wordpress jquery find and replace option text value wordpress block grid code display a subnav for the current wordpress page using wp_nav_menu to show a custom menu in wordpress get and show the featured image on a wordpress page template link to a custom style sheet in your theme directory wordpress wordpress show page content on template file show post content on template wordpress wordpress add a template file to your theme fix for wordpress requesting ftp login details installing plugins wordpress category list sorting custom adding pagination to custom wp_query add paging to wp list query list items from blog and filter by category name match a category id in wordpress and then add styles just for that id get the current post id from content.php wordpress get the current category id name and slug get the wordpress category name from a category id wordpress adding post custom field meta and displaying it on you template getting the site title vs the page name wordpress get home url add a tag to wordpress header from a plugin list items matching a category title wp register plugin settings admin Create a admin main wordpress custom menu item wordpress show the parent page title with fallback to title Add a post date to your custom wordpress post listing wordpress get page content to display on template page get the stylesheet directory in wordpress theme wordpress register enqueue javascript require jquery wordpress show the post content for use in a template wordpress show posts loop with feature image thumbnail wordpress get site url wordpress advanced custom fields replace shortcode for site url with blog url wordpress check home and not home for banners and things wordpress use a shortcode in php gravity forms wordpress include jquery in theme functions wordpress enqueue slicknav and slick slider wordpress show the page content for use in a template wordpress change domain in config, wordpress domain config wordpress get template directory
Related Search Terms
Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code here, mostly for my reference. Also if i find a good link, i usually add it here and then forget about it. more...

You could also follow me on twitter. I have a couple of youtube channels if you want to see some video related content. RuneScape 3, Minecraft and also a coding channel here Web Dev.

If you found something useful or like my work, you can buy me a coffee here. Mmm Coffee. ☕

❤️👩‍💻🎮

🪦 2000 - 16 Oct 2022 - Boots
Random Quote
Don't look to be the next Facebook, try to solve a real problem instead.
Unknown
Latest News
## 🚀 AI Giants Hit Bullseye: Anthropic & OpenAI Achieve Product-Market Fit Anthropic and OpenAI have reached a significant milestone, finding product-market fit with their AI technologies, which means their products effectively meet the needs of their customers, driving growth and adoption. This achievement showcases the practical value of their innovations, enabling businesses and individuals to leverage AI for enhanced productivity and efficiency. With this alignment of product and market needs, these companies are poised to transform industries and shape the future of technology.