• What I want to do is to set custom widths for the canvas element in the visual editor area. I want the mobile/tablet view sizes to be the same as the media queries in my custom theme.

    I did a bit of research and found that if you go to wp-includes/js/dist/block-editor.js, on line 66209 and take a look at the getCanvasWidth method, you’ll notice these are the width values that WP uses to set the iframe (canvas) width for mobile and tablet views. The thing is that they are hard coded and I don’t see a way to modify them.

    Is there a way to get this done as of now? I was thinking something similar to re-defining a function or may be a hook. Any help is appreciated!

Viewing 4 replies - 1 through 4 (of 4 total)
  • 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 ImageAlberto

    (@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 ImageAlberto

    (@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!

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Custom width for mobile view in visual editor’ is closed to new replies.