Skip to main content
Added additional method.
Source Link
Hardeep Asrani
  • 692
  • 2
  • 10
  • 24

You can use wp_localize_script to pass anything you want, including site URL, to the editor as a global variable:

wp_localize_script(
    'script_name',
    'my_data',
    [
        'siteUrl' => get_site_url()
    ]
);

And then you can get it in the editor from window.my_data.siteUrl variable. Here script_name will be the script that you're enqueuing to the editor.

If you want to use the Data API, you can use wp.data.select( 'core' ).getSite(); and once the promise has resolved, you can use the URL property.

enter image description here

You can use wp_localize_script to pass anything you want, including site URL, to the editor as a global variable:

wp_localize_script(
    'script_name',
    'my_data',
    [
        'siteUrl' => get_site_url()
    ]
);

And then you can get it in the editor from window.my_data.siteUrl variable. Here script_name will be the script that you're enqueuing to the editor.

You can use wp_localize_script to pass anything you want, including site URL, to the editor as a global variable:

wp_localize_script(
    'script_name',
    'my_data',
    [
        'siteUrl' => get_site_url()
    ]
);

And then you can get it in the editor from window.my_data.siteUrl variable. Here script_name will be the script that you're enqueuing to the editor.

If you want to use the Data API, you can use wp.data.select( 'core' ).getSite(); and once the promise has resolved, you can use the URL property.

enter image description here

Source Link
Hardeep Asrani
  • 692
  • 2
  • 10
  • 24

You can use wp_localize_script to pass anything you want, including site URL, to the editor as a global variable:

wp_localize_script(
    'script_name',
    'my_data',
    [
        'siteUrl' => get_site_url()
    ]
);

And then you can get it in the editor from window.my_data.siteUrl variable. Here script_name will be the script that you're enqueuing to the editor.