{"id":44,"date":"2020-04-30T11:07:12","date_gmt":"2020-04-30T11:07:12","guid":{"rendered":"https:\/\/codexcoach.com\/?p=44"},"modified":"2023-10-19T06:12:41","modified_gmt":"2023-10-19T06:12:41","slug":"how-to-create-custom-elements-using-visual-composer-api","status":"publish","type":"post","link":"https:\/\/codexcoach.com\/how-to-create-custom-elements-using-visual-composer-api\/","title":{"rendered":"How to Create Custom Elements Using Visual Composer API"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>What is a visual composer?<\/strong><\/h2>\n\n\n\n<p>Visual Composer is a page-building application. It facilitates the creation of pages. It provides a variety of aspects that aid in the creation of the site. However, there may be times when we need to build a custom element with special capabilities.<\/p>\n\n\n\n<p>When we are making a site with a visual composer, it is sometimes not possible to build some designs using the visual composer elements. So at that time, we need to use HTML elements for built designs.&nbsp;<br>But the Visual Composer API provides the facility to build your own custom element for some special requirements, and it&#8217;s very easy. We can build custom elements using the <strong>vc_before_init<\/strong> action. Add the below code in the theme\u2019s <strong>function.php<\/strong> file.<\/p>\n\n\n\n<figure class=\"wp-container-2 wp-block-gallery-1 wp-block-gallery has-nested-images columns-default is-cropped\">\n<figure class=\"wp-block-image size-full is-style-twentytwentyone-border\"><img loading=\"lazy\" width=\"1832\" height=\"802\" data-id=\"1382\"  src=\"https:\/\/codexcoach.com\/app\/uploads\/2022\/12\/screenshot-webbydemo.in-2022.11.29-16_39_26.png\" alt=\"\" class=\"wp-image-1382\" srcset=\"https:\/\/codexcoach.com\/app\/uploads\/2022\/12\/screenshot-webbydemo.in-2022.11.29-16_39_26.png 1832w, https:\/\/codexcoach.com\/app\/uploads\/2022\/12\/screenshot-webbydemo.in-2022.11.29-16_39_26-300x131.png 300w, https:\/\/codexcoach.com\/app\/uploads\/2022\/12\/screenshot-webbydemo.in-2022.11.29-16_39_26-1024x448.png 1024w, https:\/\/codexcoach.com\/app\/uploads\/2022\/12\/screenshot-webbydemo.in-2022.11.29-16_39_26-768x336.png 768w, https:\/\/codexcoach.com\/app\/uploads\/2022\/12\/screenshot-webbydemo.in-2022.11.29-16_39_26-1536x672.png 1536w, https:\/\/codexcoach.com\/app\/uploads\/2022\/12\/screenshot-webbydemo.in-2022.11.29-16_39_26-1568x686.png 1568w\" sizes=\"(max-width: 1832px) 100vw, 1832px\" \/><\/figure>\n<\/figure>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-file=\"Function.php\" data-lang=\"PHP\"><code>&lt;?php\nadd_action( &#39;vc_before_init&#39;, &#39;cxc_create_custom_element_in_vc&#39; );\n\nfunction cxc_create_custom_element_in_vc() {\n\tvc_map(\n\t\tarray(\n\t\t\t&quot;name&quot; =&gt; __( &quot;My Custom Element&quot;, &quot;cxc-codexcoach&quot; ), \/\/ Element name\n\t\t\t&quot;base&quot; =&gt; &quot;cxc_my_custom_element&quot;, \/\/ Element shortcode\n\t\t\t&quot;class&quot; =&gt; &quot;custom-element&quot;,\n\t\t\t&quot;category&quot; =&gt; __( &#39;Content&#39;, &#39;cxc-codexcoach&#39; ),\n\t\t\t&quot;params&quot; =&gt; array(\n\t\t\t\tarray(\n\t\t\t\t\t&quot;type&quot; =&gt; &quot;textfield&quot;,\n\t\t\t\t\t&quot;holder&quot; =&gt; &quot;div&quot;,\n\t\t\t\t\t&quot;class&quot; =&gt; &quot;&quot;,\n\t\t\t\t\t&quot;heading&quot; =&gt; __( &quot;Title&quot;, &quot;cxc-codexcoach&quot; ),\n\t\t\t\t\t&quot;param_name&quot; =&gt; &quot;cxc_my_custom_element_title&quot;,\n\t\t\t\t\t&quot;value&quot; =&gt; __( &quot;&quot;, &quot;cxc-codexcoach&quot; ),\n\t\t\t\t\t&quot;description&quot; =&gt; __( &#39;Add a Main section\\&#39;s title.&quot;.&#39;, &quot;cxc-codexcoach&quot; )\n\t\t\t\t),\n\t\t\t\tarray(\n\t\t\t\t\t&quot;type&quot; =&gt; &quot;textarea&quot;,\n\t\t\t\t\t&quot;holder&quot; =&gt; &quot;div&quot;,\n\t\t\t\t\t&quot;class&quot; =&gt; &quot;&quot;,\n\t\t\t\t\t&quot;heading&quot; =&gt; __( &quot;Description&quot;, &quot;cxc-codexcoach&quot; ),\n\t\t\t\t\t&quot;param_name&quot; =&gt; &quot;cxc_my_custom_element_description&quot;,\n\t\t\t\t\t&quot;value&quot; =&gt; __( &quot;&quot;, &quot;cxc-codexcoach&quot; ),\n\t\t\t\t\t&quot;description&quot; =&gt; __( &#39;Add a Main section\\&#39;s description.&quot;.&#39;, &quot;cxc-codexcoach&quot; )\n\t\t\t\t),\n\t\t\t\tarray(\n\t\t\t\t\t&quot;type&quot; =&gt; &quot;textfield&quot;,\n\t\t\t\t\t&quot;holder&quot; =&gt; &quot;div&quot;,\n\t\t\t\t\t&quot;class&quot; =&gt; &quot;&quot;,\n\t\t\t\t\t&quot;heading&quot; =&gt; __( &quot;Extra class name&quot;, &quot;cxc-codexcoach&quot; ),\n\t\t\t\t\t&quot;param_name&quot; =&gt; &quot;cxc_my_custom_element_extra_class&quot;,\n\t\t\t\t\t&quot;value&quot; =&gt; __( &quot;&quot;, &quot;cxc-codexcoach&quot; ),\n\t\t\t\t\t&quot;description&quot; =&gt; __( &#39;Style particular content element differently - add a class name and refer to it in custom CSS.&quot;.&#39;, &quot;cxc-codexcoach&quot; )\n\t\t\t\t),\n\t\t\t)\n\t\t)\n\t);\n}\n\n\n\nadd_shortcode( &#39;cxc_my_custom_element&#39;, &#39;cxc_my_custom_element_func&#39; );\n\nfunction cxc_my_custom_element_func( $atts ) {\n\tob_start();\n\n\t$atts = shortcode_atts(\n\t\tarray(\n\t\t\t&#39;cxc_my_custom_element_title&#39; =&gt; &#39;&#39;,\n\t\t\t&#39;cxc_my_custom_element_description&#39; =&gt; &#39;&#39;,\n\t\t\t&#39;cxc_my_custom_element_extra_class&#39; =&gt; &#39;&#39;,\n\t\t),\n\t\t$atts, &#39;cxc_my_custom_element&#39;\n\t);\n\n\t$title = isset( $atts[&#39;cxc_my_custom_element_title&#39;] ) ? $atts[&#39;cxc_my_custom_element_title&#39;] : &#39;&#39;;\n\t$class = isset( $atts[&#39;cxc_my_custom_element_extra_class&#39;] ) ? $atts[&#39;cxc_my_custom_element_extra_class&#39;] : &#39;&#39;;\n\t$description = isset( $atts[&#39;cxc_my_custom_element_description&#39;] ) ? $atts[&#39;cxc_my_custom_element_description&#39;] : &#39;&#39;;\n\t?&gt;\n\n\t&lt;div class=&quot;main_title &lt;?php echo $class; ?&gt;&quot;&gt;\n\t\t&lt;h2&gt;&lt;?php echo $title; ?&gt;&lt;\/h2&gt;\n\t\t&lt;p&gt;&lt;?php echo $description; ?&gt;&lt;\/p&gt;\n\t&lt;\/div&gt;\n\n\t&lt;?php\n\t$result = ob_get_clean();\n\treturn $result;\n}\n?&gt;<\/code><\/pre><\/div>\n\n\n\n<ul>\n<li>&nbsp;The code starts by creating a new custom element called &#8220;My Custom Element&#8221;.<\/li>\n\n\n\n<li>&nbsp;The shortcode for this is wc_my_custom_element.<\/li>\n\n\n\n<li>&nbsp;It then creates a div with the class of &#8220;custom-element&#8221; and places it inside the map.<\/li>\n\n\n\n<li>&nbsp;Next, it sets up an action to run when someone clicks on the map&#8217;s title (wc_my_custom_element).<\/li>\n\n\n\n<li>&nbsp;This action will create a text field that has no value set in it.<\/li>\n\n\n\n<li>&nbsp;The code starts by creating a new custom element called &#8220;My Custom Element&#8221;.<\/li>\n\n\n\n<li>&nbsp;The shortcode for this is wc_my_custom_element.<\/li>\n\n\n\n<li>&nbsp;It then creates a div with the class of &#8220;custom-element&#8221; and places it inside the map.<\/li>\n\n\n\n<li>&nbsp;Next, it sets up an action to run when someone clicks on the map&#8217;s title (wc<\/li>\n\n\n\n<li>&nbsp;The code will produce a custom element with the name of &#8220;My Custom Element&#8221; and a shortcode of wc_my_custom_element.<\/li>\n\n\n\n<li>&nbsp;The class is set to &#8220;custom-element&#8221;.<\/li>\n\n\n\n<li>&nbsp;This element has a category called &#8220;Content&#8221; which is defined in the code above.<\/li>\n\n\n\n<li>&nbsp;The parameter for this custom element is called wc_my_custom_element_title, which takes a string value and outputs it as a heading on the page.<\/li>\n\n\n\n<li>&nbsp;The purpose of this code is that there will be an input field on the page with the label &#8220;Title&#8221;, which when clicked, will output whatever was typed into that input field as text on the page.<\/li>\n<\/ul>\n<div id=\"ezoic-pub-ad-placeholder-118\"><\/div>","protected":false},"excerpt":{"rendered":"<p>What is a visual composer? Visual Composer is a page-building application. It facilitates the creation of pages. It provides a variety of aspects that aid in the creation of the site. However, there may be times when we need to build a custom element with special capabilities. When we are making a site with a [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":1162,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_aib_schema_json_ld":""},"categories":[10,11],"tags":[],"_links":{"self":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/44"}],"collection":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/comments?post=44"}],"version-history":[{"count":11,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/44\/revisions"}],"predecessor-version":[{"id":13213,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/posts\/44\/revisions\/13213"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/media\/1162"}],"wp:attachment":[{"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/media?parent=44"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/categories?post=44"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexcoach.com\/wp-json\/wp\/v2\/tags?post=44"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}