Last active
June 25, 2025 06:02
-
-
Save mehrshaddarzi/288bea5d9491be3c35c96a73909e1308 to your computer and use it in GitHub Desktop.
Dynamic tags in elementor with php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_filter( 'elementor/dynamic_tags/tag_value', function( $value, $tag ) { | |
// بررسی اینکه آیا تگ مورد نظر "custom field" هست | |
if ( $tag->get_name() === 'post-custom-field' ) { | |
// گرفتن post ID فعلی | |
$post_id = get_the_ID(); | |
// بررسی پست تایپ | |
if ( get_post_type( $post_id ) !== 'book' ) { | |
return $value; // اگر پست تایپ چیز دیگری بود، تغییر نده | |
} | |
// گرفتن نام کلید متا | |
$meta_key = $tag->get_settings( 'key' ); | |
// اگر کلید متا country بود، فرمت خاص بده | |
if ( $meta_key === 'country' ) { | |
if ( ! empty( $value ) ) { | |
$value = 'کشور: ' . $value; | |
} else { | |
$value = 'کشور مشخص نشده'; | |
} | |
} | |
// میتونی شرطهای بیشتری هم اینجا اضافه کنی | |
} | |
return $value; | |
}, 10, 2 ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Elementor\Core\DynamicTags\Tag; | |
if ( class_exists( '\Elementor\Core\DynamicTags\Tag' ) ) { | |
class Custom_Static_Meta_Sum extends \Elementor\Core\DynamicTags\Tag { | |
public function get_name() { | |
return 'static_meta_sum'; | |
} | |
public function get_title() { | |
return 'جمع قیمت و مالیات'; | |
} | |
public function get_group() { | |
return 'post'; | |
} | |
public function get_categories() { | |
return [ \Elementor\Modules\DynamicTags\Module::TEXT_CATEGORY ]; | |
} | |
public function render() { | |
$post_id = get_the_ID(); | |
$price = get_post_meta( $post_id, '_price', true ); | |
$tax = get_post_meta( $post_id, '_tax', true ); | |
$sum = floatval($price) + floatval($tax); | |
echo $sum; | |
} | |
} | |
add_action('elementor/dynamic_tags/register_tags', function( $dynamic_tags ) { | |
$dynamic_tags->register_tag( 'Custom_Static_Meta_Sum' ); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Elementor\Core\DynamicTags\Tag; | |
use Elementor\Controls_Manager; | |
if ( class_exists( '\Elementor\Core\DynamicTags\Tag' ) ) { | |
class My_Custom_Sum_Tag extends \Elementor\Core\DynamicTags\Tag { | |
public function get_name() { | |
return 'custom_meta_sum'; | |
} | |
public function get_title() { | |
return 'جمع متاها'; | |
} | |
public function get_group() { | |
return 'post'; // میتونید گروه دلخواه هم بسازید | |
} | |
public function get_categories() { | |
return [ \Elementor\Modules\DynamicTags\Module::TEXT_CATEGORY ]; | |
} | |
protected function register_controls() { | |
$this->add_control( | |
'meta_key_1', | |
[ | |
'label' => 'Meta Key 1', | |
'type' => Controls_Manager::TEXT, | |
] | |
); | |
$this->add_control( | |
'meta_key_2', | |
[ | |
'label' => 'Meta Key 2', | |
'type' => Controls_Manager::TEXT, | |
] | |
); | |
} | |
public function render() { | |
$meta_key_1 = $this->get_settings('meta_key_1'); | |
$meta_key_2 = $this->get_settings('meta_key_2'); | |
$post_id = get_the_ID(); | |
$val1 = get_post_meta( $post_id, $meta_key_1, true ); | |
$val2 = get_post_meta( $post_id, $meta_key_2, true ); | |
$sum = floatval($val1) + floatval($val2); | |
echo $sum; | |
} | |
} | |
add_action('elementor/dynamic_tags/register_tags', function( $dynamic_tags ) { | |
$dynamic_tags->register_tag( 'My_Custom_Sum_Tag' ); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function sum_post_meta_shortcode( $atts ) { | |
$atts = shortcode_atts( array( | |
'key1' => '', | |
'key2' => '', | |
'post_id' => get_the_ID(), | |
), $atts, 'sum_meta' ); | |
$val1 = get_post_meta( $atts['post_id'], $atts['key1'], true ); | |
$val2 = get_post_meta( $atts['post_id'], $atts['key2'], true ); | |
$sum = floatval($val1) + floatval($val2); | |
return $sum; | |
} | |
add_shortcode( 'sum_meta', 'sum_post_meta_shortcode' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment