Home >CMS Tutorial >WordPress >Using Polymer in WordPress: Build a Custom Google Maps Component
This tutorial demonstrates how to easily integrate Polymer and web components into WordPress, specifically adding a Google Maps component to a sidebar. It emphasizes the advantages of Polymer, even with increasing native web component support, highlighting its speed, features (data binding, computed properties), and CLI tools.
The tutorial begins by setting up a WordPress environment using ScotchBox Vagrant VM (or suggesting alternatives for those with existing installations). It then recommends using the SitePoint Base theme for its lightweight and SEO-friendly nature, guiding the reader through creating a child theme for customization.
Polymer is installed via Bower, along with necessary components (like paper-input
and later, google-map
). The process includes adding scripts and HTML imports to the child theme's functions.php
file to correctly enqueue Polymer and its polyfills for broader browser compatibility.
A custom WordPress widget is created to showcase the Polymer components. The tutorial details creating a new widget class extending WP_Widget
and registering it. Initially, a simple paper-input
element is used for demonstration.
Next, the tutorial introduces the google-map
web component, obtained from WebComponents.org. It guides the creation of a new Polymer component (sitepoint-map.html
) with sections for dependency imports, a template defining the component's structure (including the google-map
element and a paper-input
for displaying coordinates), and a script section defining the component's behavior (handling mouse events on the map). The importance of obtaining a Google Maps API key and using it within the component is stressed.
The tutorial explains how to manage multiple custom components by creating a central index.html
file to import them all, simplifying the enqueue process. The sitepoint-map
component is then integrated into the WordPress widget, replacing the paper-input
with the map. The final result is a functional Google Maps widget within the WordPress sidebar.
The conclusion encourages readers to explore further, suggesting extending the widget to accept configuration parameters from the WordPress admin panel. It also promotes exploring the WebComponents.org library for other components to integrate.
The FAQ section addresses common questions about Polymer and WordPress integration, covering installation, compatibility, benefits, drawbacks, browser support, component creation, and the current status of Polymer's maintenance.
(Note: The images from the original input are included as requested. Their alt text has been slightly modified for clarity.)
The above is the detailed content of Using Polymer in WordPress: Build a Custom Google Maps Component. For more information, please follow other related articles on the PHP Chinese website!