Skip to content

Instantly share code, notes, and snippets.

@iqbalrony
Last active May 30, 2023 10:09
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save iqbalrony/7ee129379965082fb6c62cf5db372752 to your computer and use it in GitHub Desktop.
Save iqbalrony/7ee129379965082fb6c62cf5db372752 to your computer and use it in GitHub Desktop.
How to add/use/register elementor dynamic tag with elementor free version. This is a very easy and useful system of Elementor. Especially I feel the need for this when I use URL control. Help link -> (https://developers.elementor.com/dynamic-tags/)
<?php
/**
* Post URL list
*/
if (!function_exists('prefix_get_all_posts_url')) {
function prefix_get_all_posts_url($posttype = 'post') {
$args = array(
'post_type' => $posttype,
'post_status' => 'publish',
'posts_per_page' => -1
);
$post_list = array();
if ($data = get_posts($args)) {
foreach ($data as $key) {
$post_list[$key->post_name] = $key->post_title;
}
}
return $post_list;
}
}
/**
* Include Dynamic Tag Class files
*/
require_once(plugin_dir_path(__FILE__).'inc/widgets/elementor/dynamic-tag/prefix-post-url.php');
if (class_exists('WooCommerce')) {
require_once(plugin_dir_path(__FILE__).'inc/widgets/elementor/dynamic-tag/prefix-product-url.php');
}
/**
* Register the Dynamic Tag
*/
add_action( 'elementor/dynamic_tags/register_tags', 'prefix_register_tags' );
function prefix_register_tags( $dynamic_tags ) {
// To register that group as well before the tag
\Elementor\Plugin::$instance->dynamic_tags->register_group( 'post', [
'title' => 'Post'
] );
if (class_exists('WooCommerce')) {
\Elementor\Plugin::$instance->dynamic_tags->register_group( 'woocommerce', [
'title' => 'WooCommerce'
] );
}
// Finally register the single post url tag
$dynamic_tags->register_tag( new Prefix\Elementor\Tag\Prefix_Post_Url() );
// Finally register the single product url tag
if (class_exists('WooCommerce')) {
$dynamic_tags->register_tag( new Prefix\Elementor\Tag\Prefix_Product_Url() );
}
}
<?php
namespace Prefix\Elementor\Tag;
use Elementor\Core\DynamicTags\Tag;
/**
* Post Url
*/
Class Prefix_Post_Url extends Tag {
public function get_name() {
return 'prefix-single-post-url';
}
public function get_title() {
return __( 'Single Post Url', 'elementor-pro' );
}
public function get_group() {
return 'post';
}
public function get_categories() {
return [
\Elementor\Modules\DynamicTags\Module::URL_CATEGORY
];
}
protected function _register_controls() {
$this->add_control(
'prefix_single_post_url',
[
'label' => __( 'Post Url', 'elementor-pro' ),
'type' => \Elementor\Controls_Manager::SELECT,
'options' => prefix_get_all_posts_url(),
]
);
}
public function render() {
$param_name = $this->get_settings( 'prefix_single_post_url' );
echo wp_kses_post( $param_name );
}
}
<?php
namespace Prefix\Elementor\Tag;
use Elementor\Core\DynamicTags\Tag;
/**
* Product Url
*/
Class Prefix_Product_Url extends Tag {
public function get_name() {
return 'prefix-single-product-url';
}
public function get_title() {
return __( 'Single Product Url', 'elementor-pro' );
}
public function get_group() {
return 'woocommerce';
}
public function get_categories() {
return [
\Elementor\Modules\DynamicTags\Module::URL_CATEGORY
];
}
protected function _register_controls() {
$this->add_control(
'prefix_single_product_url',
[
'label' => __( 'Product Url', 'elementor-pro' ),
'type' => \Elementor\Controls_Manager::SELECT,
'options' => prefix_get_all_posts_url('product'),
]
);
}
public function render() {
$param_name = $this->get_settings( 'prefix_single_product_url' );
echo wp_kses_post( $param_name );
}
}
@A-safarji
Copy link

Hi there, could you please show us where to add it?

in the Elementor files or my child theme.
so, this is for using dynamic tags for the free version right?

thanks alot!

@A-safarji
Copy link

your help is much appreciated!

@iqbalrony
Copy link
Author

iqbalrony commented Aug 15, 2020

Hi there, could you please show us where to add it?

in the Elementor files or my child theme.
so, this is for using dynamic tags for the free version right?

thanks alot!

Copy all code of the 'functions.php' file and paste this at the end of the main function file of your theme or plugin. Put the other two files on your theme or plugins directory and then show the paths of those two files in require_once() function.
This is just a demo on how to register the dynamic tag feature for widgets. If you understand what I just trying to do in this code, you can modify it by yourself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment