You can achieve your goal by using a custom script to override the default behavior.Create a Custom JavaScript File: Add a custom JavaScript file to your theme or plugin. This file will contain the code to override the getCanvasWidth method.Create a file named custom-editor-width.js and add the following code:
(function() {
const originalGetCanvasWidth = wp.data.select('core/block-editor').getCanvasWidth;
wp.data.select('core/block-editor').getCanvasWidth = function() {
const width = originalGetCanvasWidth.apply(this, arguments);
// Define your custom widths here
const customWidths = {
mobile: 480, // Example width for mobile
tablet: 768 // Example width for tablet
};
// Apply custom widths based on the current viewport
if (window.innerWidth <= customWidths.mobile) {
return customWidths.mobile;
} else if (window.innerWidth <= customWidths.tablet) {
return customWidths.tablet;
}
return width;
};
})();
Thread Starter
Alberto
(@ixistudio)
Thanks @hexa1316, that looks interesting. I tried it but it doesnt work. Also, the following line of code returns undefined in the console.log:
wp.data.select('core/block-editor').getCanvasWidth
I tried running the code inside wp.domReady() as well but that didn’t do much.
Hi Alberto @ixistudio, I found a hack for this!
I think 360 px is a very outdated width for mobile devices, perhaps they didn’t change it because they are focused on full site editor. I had looking for a solution fo a while too, and finally found one.
If you work with a child theme, you can add a couple of lines.
In functions.php
function my_admin_theme_style() {
wp_enqueue_style('my-admin-style', 'https://......../wp-content/themes/....../admin.css');
}
add_action('admin_enqueue_scripts', 'my_admin_theme_style', 8 );
Don’t forget to fill ……..
In admin.css
/* Mobile preview */
.block-editor-iframe__scale-container iframe {
width: 560px !important;
}
It worked for me, I hope others found useful.
Saludos desde Argentina.
Thread Starter
Alberto
(@ixistudio)
Thanks for your input, @damen02. I tested your hack, but it seems that the mobile and tablet views are both using the same width defined in the CSS. This prevents the viewport from resizing correctly between the two, so I’m not sure if this solution will work for this.
On another note, the WP higher-ups are already aware of this limitation and have plans to address it in the near future. You can check out the “State of the Word 2024” keynote video (Tokyo) at around the 29:00 mark. Mathias talked just about this like 2 days ago:
https://www.youtube.com/watch?v=KLybH5YvIPQ&t=1740s
For now, it looks like our options are to either hack away or keep waiting for the official solution.
Saludos!