{"id":20288,"date":"2019-11-28T11:01:48","date_gmt":"2019-11-28T10:01:48","guid":{"rendered":"https:\/\/avengering.com\/?p=20288"},"modified":"2019-12-12T09:33:04","modified_gmt":"2019-12-12T08:33:04","slug":"how-to-create-an-elementor-widget-for-advanced-custom-fields","status":"publish","type":"post","link":"https:\/\/avengering.com\/en\/how-to-create-an-elementor-widget-for-advanced-custom-fields\/","title":{"rendered":"how to create an Elementor widget for advanced custom fields"},"content":{"rendered":"<h2>Preface<\/h2>\n<p>Sometimes you need to add custom fields to a post type. for example, if your post is about a book, you may need to have publisher name, publish date and author fields behind of post content that contains abstract of book. but there is no specific Elementor widget for Advanced custom fields. you must use and insert these fields one by one in your templates.<\/p>\n<h3>Steps to create an Elementor widget for advanced custom fields<\/h3>\n<p>In the previous article, I show you<a href=\"https:\/\/avengering.com\/en\/how-to-add-grouped-product-attributes-comparison-in-woocommerce\/\"> how to create a grouped comparison of product attributes with advanced custom fields and Yith compare plugin<\/a>. In this article, I create an Elementor widget to show a group of advanced custom fields on the single product page.<\/p>\n<p>If you want to have some additional attributes for each product and showing them on the product page, there are some solutions but no one is perfect. I want to have several groups of attributes and show each group in a separate section of the page. So I developed a Widget and put it in a plugin for my specific problem. note that I keep my code very simple for learning proposes.<\/p>\n<h4>1- create a simple plugin structure<\/h4>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n\/**\r\n * Plugin Name: Elementor ACF Widget\r\n * Description: Elementor Widget to show a group of advanced custom fields\r\n * Plugin URI:  https:\/\/avengering.com\/\r\n * Version:     1.3.0\r\n * Author:      Ali Sohrabi\r\n * Author URI:  https:\/\/avengering.com\/\r\n * Text Domain: elementor-advanced-custom-fields\r\n *\/\r\n\r\n<\/pre>\n<p>a plugin structure is a simple PHP file inside a directory. as you can see there is some comment line in the first lines of file. this file must be named as the directory name. you can find complete help in this address <a href=\"https:\/\/codex.wordpress.org\/Writing_a_Plugin\">https:\/\/codex.wordpress.org\/Writing_a_Plugin<\/a><\/p>\n<h4>2- add Elementor widget structures<\/h4>\n<p>Creating a widget for Elementor is very different from creating a widget for WordPress itself. you must create a class that derives from <strong><a href=\"https:\/\/code.elementor.com\/classes\/elementor-widget_base\/\" target=\"_blank\" rel=\"noopener noreferrer\">Widget_Base<\/a> <\/strong>class.you can find a full document here <a href=\"https:\/\/developers.elementor.com\/creating-a-new-widget\/\">https:\/\/developers.elementor.com\/creating-a-new-widget\/<\/a><\/p>\n<p>you must create a &#8220;widget&#8221; folder under the plugin root directory. I have put Widget_Base derived class in the &#8220;acf4ele.php&#8221; file.<\/p>\n<pre class=\"lang:php decode:true \">&lt;?php\r\nnamespace Elementoracf4ele\\Widgets;\r\n\r\nuse Elementor\\Widget_Base;\r\nuse Elementor\\Controls_Manager;\r\n\r\nif ( ! defined( 'ABSPATH' ) ) exit; \/\/ Exit if accessed directly\r\n\r\n\/**\r\n * Acf4Ele\r\n *\r\n * Elementor widget for ACF.\r\n *\r\n * @since 1.0.0\r\n *\/\r\nclass acf4ele extends Widget_Base {\r\n<\/pre>\n<p>then you must declare at least 8 functions in this class. 6 of them are very simple functions that you can understand them by visiting the source code.<\/p>\n<pre class=\"lang:default decode:true \">\tpublic function get_name() \r\n\tpublic function get_title() \r\n\tpublic function get_icon() \r\n\tpublic function get_categories() \r\n\tpublic function get_script_depends() \r\n<\/pre>\n<p>you can find detailed information about these 5 functions in the above link. the two remained functions described in the following paragraphs.<\/p>\n<h3>3- add parameter input controls for design time<\/h3>\n<p>in the Elementor designer panel, you have the choices to change some Controls of a Widget. but you need some more Controls, for example, the group_ID of advanced custom fields. in the _register_controls function, you must define these Controls.<\/p>\n<pre class=\"lang:default decode:true\">\tprotected function _register_controls() {\r\n\t\t$this-&gt;start_controls_section(\r\n\t\t\t'section_content',\r\n\t\t\t[\r\n\t\t\t\t'label' =&gt; __( 'Content', 'elementor-acf4ele' ),\r\n\t\t\t]\r\n\t\t);\r\n\r\n\t\t$this-&gt;add_control(\r\n\t\t\t'groupid',\r\n\t\t\t[\r\n\t\t\t\t'label' =&gt; __( 'ACF Group ID', 'elementor-acf4ele' ),\r\n\t\t\t\t'type' =&gt; Controls_Manager::TEXT,\r\n\t\t\t]\r\n\t\t);\r\n\r\n\t\t$this-&gt;end_controls_section();<\/pre>\n<p>With\u00a0 <em>$this-&gt;start_controls_section <\/em>command, a new section appears in designer panel of Elementor.\u00a0 it is named as <em>&#8216;section_content&#8217;<\/em> and labeled as<em> &#8216;Content&#8217;<\/em> with <em>&#8216;label&#8217; =&gt;<img fetchpriority=\"high\" decoding=\"async\" class=\"size-medium wp-image-20652 alignright\" src=\"https:\/\/avengering.com\/wp-content\/uploads\/2019\/11\/acfgroupid-300x224.jpg\" alt=\"elementor control panel for widget\" width=\"300\" height=\"224\" \/><\/em><\/p>\n<p><em> __( &#8216;Content&#8217;, &#8216;elementor-acf4ele&#8217; )<\/em> .<\/p>\n<p>this section include a Control named as<em> &#8216;groupid&#8217;<\/em> witch labeled as <em>&#8216;ACF Group ID&#8217;<\/em> and type of <em>Text &#8216;type&#8217; =&gt; Controls_Manager::TEXT<\/em>, finally this section is closed with <em>$this-&gt;end_controls_section().<\/em> now you have a control that must be filled by Group_ID parameter.<\/p>\n<p>To finding group_ID of a parameter group in ACF you must go to Custom Fields&gt;Fields Group menu. then select the desired group. now select the Edit link. In the new window go to the right upper corner and copy the group ID and paste it to the Elementor Control panel.<\/p>\n<figure id=\"attachment_20654\" aria-describedby=\"caption-attachment-20654\" style=\"width: 280px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" class=\"wp-image-20654 size-full\" src=\"https:\/\/avengering.com\/wp-content\/uploads\/2019\/11\/photo_2019-11-25_11-05-44.jpg\" alt=\"find group id of advanced custom field\" width=\"280\" height=\"199\" \/><figcaption id=\"caption-attachment-20654\" class=\"wp-caption-text\">group ID of ACF group<\/figcaption><\/figure>\n<h4>4- modify the &#8220;Render()&#8221; procedure<\/h4>\n<p>this is the main part of the Widget that appears on the front page. you need a little modification to the code that I published in the <a href=\"https:\/\/avengering.com\/en\/how-to-add-grouped-product-attributes-comparison-in-woocommerce\/\">previous post.<\/a><\/p>\n<pre class=\"lang:php decode:true\">protected function render() {\r\n\t\techo '&lt;div class=\"title\"&gt;';\r\n\t\techo  '&lt;table&gt;';\r\n\t\t\r\n\t\t$settings = $this-&gt;get_settings_for_display();\r\n        $fields = acf_get_fields($settings['groupid']);\r\n\t\tforeach( $fields as $field ){\r\n\t\t\t  echo  '&lt;tr class=\"price\"&gt;&lt;td&gt;';\r\n\t\t      echo $field['label']; \t\r\n\t\t      echo '&lt;\/td&gt;';\r\n\t\t      echo \"&lt;td&gt;\";\r\n              echo get_field($field['name'],$post_id); \r\n              echo \"&lt;\/td&gt;&lt;\/tr&gt;\";\r\n\t\t}\r\n\t\techo '&lt;\/table&gt;&lt;\/div&gt;';\r\n\t}<\/pre>\n<p>you can find the complete source code of this project at <a href=\"https:\/\/github.com\/alisohrabit\/ACF4Elementor\">https:\/\/github.com\/alisohrabit\/ACF4Elementor.<\/a> Consider as I apply the new versions to GIT, you may find some modifications from the above codes.<\/p>\n<h2>Conclusion<\/h2>\n<p>As you can see this is very simple to add a new widget to Elementor. now we have a widget that shows a table of a group of additional parameters of a post. these additional parameters are prepared by Advanced custom fields plugin and you can put them grouped, in any section of the page. in the next article of this series, I will create a single product template in Elemntor and use this widget to show the advanced properties of the product on this template.<\/p>\n<blockquote><p><a href=\"https:\/\/developers.elementor.com\/creating-a-new-widget\/\">A few things to note<\/a>:<\/p><\/blockquote>\n<ol>\n<li>\n<blockquote><p>Your plugin should only include the widget class file if Elementor is active and loaded.<\/p><\/blockquote>\n<\/li>\n<li>\n<blockquote><p>There are other methods available to make your widget more reactive like\u00a0<code>_content_template()<\/code>\u00a0which lets you recreate the\u00a0<code>render()<\/code> method in JavaScript to be used in the Editor which uses the\u00a0<a href=\"https:\/\/marionettejs.com\/docs\/master\/template.html\" target=\"_blank\" rel=\"noopener noreferrer\">MarionetteJS template engine<\/a>\u00a0and using it will render the content of the widget on any instantly on any change to one of his controls without making a request to the server.<\/p><\/blockquote>\n<\/li>\n<li>\n<blockquote><p>There are many types of controls you can add to your widget to make it easier to configure. We have a separate section with a full\u00a0<a href=\"https:\/\/developers.elementor.com\/elementor-controls\/\">list of available controls<\/a>.<\/p><\/blockquote>\n<\/li>\n<\/ol>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Preface Sometimes you need to add custom fields to a post type. for example, if your post is about a book, you may need to have publisher name, publish date and author fields behind of post content that contains abstract of book. but there is no specific Elementor widget for Advanced custom fields. you must [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":20760,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-gradient":""}},"_glsr_average":5,"_glsr_ranking":3.6363636363636,"_glsr_reviews":1,"footnotes":""},"categories":[3370,1718],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v22.3 (Yoast SEO v23.9) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>how to create an Elementor widget for advanced custom fields -<\/title>\n<meta name=\"description\" content=\"You may need to add custom fields to a post type, but there is no Elementor widget for Advanced custom fields.We can use ACF With Elementor\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/avengering.com\/en\/how-to-create-an-elementor-widget-for-advanced-custom-fields\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"how to create an Elementor widget for advanced custom fields\" \/>\n<meta property=\"og:description\" content=\"You may need to add custom fields to a post type, but there is no Elementor widget for Advanced custom fields.We can use ACF With Elementor\" \/>\n<meta property=\"og:url\" content=\"https:\/\/avengering.com\/en\/how-to-create-an-elementor-widget-for-advanced-custom-fields\/\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/avengering\/\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-28T10:01:48+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-12-12T08:33:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/avengering.com\/wp-content\/uploads\/2019\/11\/acf4ele-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"450\" \/>\n\t<meta property=\"og:image:height\" content=\"300\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"ali sohrabi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@avengering1\" \/>\n<meta name=\"twitter:site\" content=\"@avengering1\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"ali sohrabi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"how to create an Elementor widget for advanced custom fields -","description":"You may need to add custom fields to a post type, but there is no Elementor widget for Advanced custom fields.We can use ACF With Elementor","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/avengering.com\/en\/how-to-create-an-elementor-widget-for-advanced-custom-fields\/","og_locale":"en_US","og_type":"article","og_title":"how to create an Elementor widget for advanced custom fields","og_description":"You may need to add custom fields to a post type, but there is no Elementor widget for Advanced custom fields.We can use ACF With Elementor","og_url":"https:\/\/avengering.com\/en\/how-to-create-an-elementor-widget-for-advanced-custom-fields\/","article_publisher":"https:\/\/www.facebook.com\/avengering\/","article_published_time":"2019-11-28T10:01:48+00:00","article_modified_time":"2019-12-12T08:33:04+00:00","og_image":[{"width":450,"height":300,"url":"https:\/\/avengering.com\/wp-content\/uploads\/2019\/11\/acf4ele-1.jpg","type":"image\/jpeg"}],"author":"ali sohrabi","twitter_card":"summary_large_image","twitter_creator":"@avengering1","twitter_site":"@avengering1","twitter_misc":{"Written by":"ali sohrabi","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/avengering.com\/en\/how-to-create-an-elementor-widget-for-advanced-custom-fields\/#article","isPartOf":{"@id":"https:\/\/avengering.com\/en\/how-to-create-an-elementor-widget-for-advanced-custom-fields\/"},"author":{"name":"ali sohrabi","@id":"https:\/\/avengering.com\/en\/#\/schema\/person\/c3f5a067a60c606366dab72381533aae"},"headline":"how to create an Elementor widget for advanced custom fields","datePublished":"2019-11-28T10:01:48+00:00","dateModified":"2019-12-12T08:33:04+00:00","mainEntityOfPage":{"@id":"https:\/\/avengering.com\/en\/how-to-create-an-elementor-widget-for-advanced-custom-fields\/"},"wordCount":833,"commentCount":0,"publisher":{"@id":"https:\/\/avengering.com\/en\/#organization"},"image":{"@id":"https:\/\/avengering.com\/en\/how-to-create-an-elementor-widget-for-advanced-custom-fields\/#primaryimage"},"thumbnailUrl":"https:\/\/avengering.com\/wp-content\/uploads\/2019\/11\/acf4ele-1.jpg","articleSection":["How to","wordpress"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/avengering.com\/en\/how-to-create-an-elementor-widget-for-advanced-custom-fields\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/avengering.com\/en\/how-to-create-an-elementor-widget-for-advanced-custom-fields\/","url":"https:\/\/avengering.com\/en\/how-to-create-an-elementor-widget-for-advanced-custom-fields\/","name":"how to create an Elementor widget for advanced custom fields -","isPartOf":{"@id":"https:\/\/avengering.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/avengering.com\/en\/how-to-create-an-elementor-widget-for-advanced-custom-fields\/#primaryimage"},"image":{"@id":"https:\/\/avengering.com\/en\/how-to-create-an-elementor-widget-for-advanced-custom-fields\/#primaryimage"},"thumbnailUrl":"https:\/\/avengering.com\/wp-content\/uploads\/2019\/11\/acf4ele-1.jpg","datePublished":"2019-11-28T10:01:48+00:00","dateModified":"2019-12-12T08:33:04+00:00","description":"You may need to add custom fields to a post type, but there is no Elementor widget for Advanced custom fields.We can use ACF With Elementor","breadcrumb":{"@id":"https:\/\/avengering.com\/en\/how-to-create-an-elementor-widget-for-advanced-custom-fields\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/avengering.com\/en\/how-to-create-an-elementor-widget-for-advanced-custom-fields\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/avengering.com\/en\/how-to-create-an-elementor-widget-for-advanced-custom-fields\/#primaryimage","url":"https:\/\/avengering.com\/wp-content\/uploads\/2019\/11\/acf4ele-1.jpg","contentUrl":"https:\/\/avengering.com\/wp-content\/uploads\/2019\/11\/acf4ele-1.jpg","width":450,"height":300,"caption":"advanced custom fields widget for elementor"},{"@type":"BreadcrumbList","@id":"https:\/\/avengering.com\/en\/how-to-create-an-elementor-widget-for-advanced-custom-fields\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/avengering.com\/en\/"},{"@type":"ListItem","position":2,"name":"how to create an Elementor widget for advanced custom fields"}]},{"@type":"WebSite","@id":"https:\/\/avengering.com\/en\/#website","url":"https:\/\/avengering.com\/en\/","name":"Avenger IT Next Generation","description":"site web Concepcion","publisher":{"@id":"https:\/\/avengering.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/avengering.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/avengering.com\/en\/#organization","name":"Avenger It Next Generation","url":"https:\/\/avengering.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/avengering.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/avengering.com\/wp-content\/uploads\/2019\/03\/av-logo.jpg","contentUrl":"https:\/\/avengering.com\/wp-content\/uploads\/2019\/03\/av-logo.jpg","width":672,"height":156,"caption":"Avenger It Next Generation"},"image":{"@id":"https:\/\/avengering.com\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/avengering\/","https:\/\/x.com\/avengering1","https:\/\/www.instagram.com\/avengeritnextgeneration\/","https:\/\/linkedin.com\/company\/avengering","https:\/\/www.pinterest.fr\/kaviani0593\/","https:\/\/www.youtube.com\/channel\/UCVwQU9Tx51ptiSG-Z9MOOTQ"]},{"@type":"Person","@id":"https:\/\/avengering.com\/en\/#\/schema\/person\/c3f5a067a60c606366dab72381533aae","name":"ali sohrabi","description":"master of science in software engineering. About 20 years of writing codes. developer of desktop and web based financial software.","sameAs":["https:\/\/avengering.com","https:\/\/www.linkedin.com\/in\/ali-sohrabi-9772a739\/"],"url":"https:\/\/avengering.com\/en\/author\/sohrabi-ali\/"}]}},"_links":{"self":[{"href":"https:\/\/avengering.com\/en\/wp-json\/wp\/v2\/posts\/20288"}],"collection":[{"href":"https:\/\/avengering.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/avengering.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/avengering.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/avengering.com\/en\/wp-json\/wp\/v2\/comments?post=20288"}],"version-history":[{"count":0,"href":"https:\/\/avengering.com\/en\/wp-json\/wp\/v2\/posts\/20288\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/avengering.com\/en\/wp-json\/wp\/v2\/media\/20760"}],"wp:attachment":[{"href":"https:\/\/avengering.com\/en\/wp-json\/wp\/v2\/media?parent=20288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/avengering.com\/en\/wp-json\/wp\/v2\/categories?post=20288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/avengering.com\/en\/wp-json\/wp\/v2\/tags?post=20288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}