Skip to content

Instantly share code, notes, and snippets.

@arfinmilondev
Forked from iqbalrony/functions.php
Created May 30, 2023 10:09
Show Gist options
  • Select an option

  • Save arfinmilondev/0fa17359a4844b6ad0c036cc54c7bce4 to your computer and use it in GitHub Desktop.

Select an option

Save arfinmilondev/0fa17359a4844b6ad0c036cc54c7bce4 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 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment