If you’re in JS you can’t use PHP (<?php ?>
), you need to another way to get a PHP variable into JS. The normal way with WordPress is to use wp_localize_script()
.
wp_localize_script( 'wpse_274344', 'myScriptObject', array(
'markerImageUrl' => get_theme_file_uri( '/img/mapmarker.png' ),
) );
Add that code after your call to wp_enqueue_script()
or wp_register_script()
. Replace wpse_274344
with the name you used when enqueuing the script.
This means that WordPress will output a JavaScript object named myScriptObject
to the page containing the values that you passed to wp_localize_script()
.
Then in your script file you can access the markerImageUrl
like this:
markerImage = {
url: window.myScriptObject.markerImageUrl
}
Related Posts:
- Opinions and recommendations on the best barebones base theme [closed]
- Customizing a WordPress theme without changing it?
- How to Link External jQuery/Javascript files with WordPress
- Custom Taxonomy-specific JavaScript
- Any official way to create an admin theme?
- Splitting an Evolving Site into Multiple Sites, or Maintaining as One Site?
- Building useful features into your theme
- Multisite: setting theme and options when a new blog is created
- Editing theme files and access to the Code pages only to super-admin?
- Change Default Image HTML
- How to delete default themes
- Extra User Profile Field Upload File / Image
- Edit raw HTML of the landing page
- Problem : deletion failed, the theme requested does not exist
- Theme Activation, Create Database Tables
- Will activating a different theme preserve custom settings to current theme?
- Can I apply a WP theme to a specific custom page template?
- How to trigger function on theme delete?
- Setting a static home page and blog page without using the settings
- Two Navigation Menus – secondary nav is including main nav
- Possible to put header-{your_custom_template}.php in subdirectory?
- Renaming the Twenty Fourteen WordPress theme
- Protect changes made to the theme when updating
- Theme Development Admin Area [closed]
- How to set a single theme directory on a multisite blog?
- Static page not working
- Custom options below pages/posts editor?
- Problem of running development copy of WordPress on WebMatrix?
- Different logo on homepage
- the_content() is not displaying content, why? [closed]
- What is the correct way to integrate wordpress with another php application?
- Customizing page content layout
- Tips for managing code when developing a parent theme framework
- Trying to uninstall a theme, but it’s hijacking my home page
- Most wp_nav_menu parameters ignored
- Using the same database on another Subdomain
- How To Show Different Footers For Different Post Types Pages?
- How to include stylesheet in custom admin using parent_slug
- Custom fields not displaying on front end
- My blog suddenly can’t find my stylesheet?
- Customizing 1 theme for multiple blogs in a multisite setup
- Is there any way to modify images from a theme using the web interface?
- Default and warning messages & no login
- Display ads by location [closed]
- WordPress Dashboard Problem [closed]
- Theme Dev | Convert Template to Theme
- Work on theme files on a server & browser preview before installing it
- Create a child Theme or custom Theme
- Theme Options Menu Item – Permission Issue
- Static front page displaying twice
- Static posts page is not working
- Adding admin-ajax.php to the frontend. Good or bad idea?
- Can wordpress theme folder name be changed freely and nothing technically happens
- How do I add support to my theme for custom menus?
- How do I white label my self-hosted site created by wordpress?
- can require “themefolder/includes/widgets.php” in “themefolder/functions.php” yet widgets.php doesn’t run
- How do i structure my theme folder to avoid one huge list of files
- How to use scss in wordpress theme?
- How to get Ajax into a theme – without writing a plugin?
- WordPress as Backend, Laravel Front End: How to connect Routes?
- Updating custom theme that is built from scratch
- single.php change into a modal popup bootstrap wordpress
- get_header(‘header2’) not working properly in child themes
- How do you remove Link backs on Theme settings page?
- Theme broken after upgrading to WordPress 4.5, missing stylesheet?
- Do not show excerpt in post content
- $wp_customize->remove_section for customizer setting?
- Modify a theme to insert custom widgets?
- ‘Customize’ button in admin bar for CSS
- Calling widget via function in themes files (hard code)
- Posts vs. Pages
- What’s the order of loading wordpress elements?
- How to add pagination to my code?
- Creating new templates in child themes breaks layout
- Woocommerce StoreFront Showing Incorrect Subtotal in Customer Total
- Depending on third party plugins for theme functionality
- How can you showcase premium themes to clients without having to pre-purchase the theme?
- Functions containing parameter ‘yourtheme’
- Cant remove white space on my wordpress [closed]
- get_the_post_thumbnail() produces different HTML on same arguments
- .PO file is found but I don’t see translations: how to debug the problem?
- Cannot figure out what element header color is in wordpress theme [closed]
- An unexpected error occurred when add theme
- Searching for themes that post full article on home page by default
- Posts & page twice display
- How to remove date, author and comment count from Chameleon search results page
- Index page is not working
- Custom fields in post later used in loop
- How to create a theme that can be updated by people using the theme on their site
- Set custom directory in theme for page templates
- how do you add a gallery to the homepage without using a post or page?
- How to change WordPress theme outside of admin? Can’t access dashboard
- Divi Blog Module Plugin and YOAST SEO “Primary Category”
- How to show taxonomy in WordPress shortcode
- how to load rtl.css file before style.css [duplicate]
- Create a live demo gallery for themes
- Lightword Theme – only one menu item
- How to create an Single-Portfolio page?
- “Parse error: syntax error, unexpected” and the WordPress Theme Editor no longer working!
- Installing themes from an external website [closed]