Skip to content

Instantly share code, notes, and snippets.

@webdevs-pro
Created November 2, 2020 23:57
Show Gist options
  • Save webdevs-pro/64a6fb88fad097434e8f9468f079f344 to your computer and use it in GitHub Desktop.
Save webdevs-pro/64a6fb88fad097434e8f9468f079f344 to your computer and use it in GitHub Desktop.
<?php
// MS Dynamic tags
add_action( 'elementor/dynamic_tags/register_tags', function( $dynamic_tags ) {
// conditional archive title (TEXT)
class MS_Custom_Archive_Title_Tag extends \Elementor\Core\DynamicTags\Tag {
public function get_name() {
return 'MS_Custom_Archive_Title_Tag';
}
public function get_categories() {
return [ 'text' ];
}
public function get_group() {
return [ 'archive' ];
}
public function get_title() {
return 'MS Archive Title';
}
protected function _register_controls() {
$this->add_control(
'option_field_name',
[
'label' => 'Option Name',
'type' => 'text',
'description' => 'Fallback option field name for front page',
]
);
$this->add_control(
'include_context',
[
'label' => __( 'Include Context', 'elementor-pro' ),
'type' => 'switcher',
'default' => 'yes',
]
);
}
public function render() {
$include_context = 'yes' === $this->get_settings('include_context');
if (is_front_page()) {
$value = get_option($this->get_settings('option_field_name'));
} else {
$value = Utils::get_page_title($include_context);
}
echo wp_kses_post( $value );
}
}
$dynamic_tags->register_tag( 'MS_Custom_Archive_Title_Tag' );
// conditional archive image (IMAGE)
class MS_Custom_Archive_Image_Tag extends \Elementor\Core\DynamicTags\Data_Tag {
public function get_name() {
return 'MS_Custom_Archive_Image_Tag';
}
public function get_categories() {
return [ 'image' ];
}
public function get_group() {
return [ 'archive' ];
}
public function get_title() {
return 'MS Archive Image';
}
protected function _register_controls() {
$this->add_control(
'term_field_name',
[
'label' => 'Term Meta Name',
'type' => 'text',
'description' => 'Term meta field',
]
);
$this->add_control(
'option_field_name',
[
'label' => 'Option Name',
'type' => 'text',
'description' => 'Fallback option field name for front page',
]
);
}
public function get_value(array $options = []) {
if (is_front_page()) {
$id = get_option($this->get_settings('option_field_name'));
} else {
$id = get_term_meta(get_queried_object_id(), $this->get_settings('term_field_name'), true);
}
$src = wp_get_attachment_image_src($id, 'full');
$image_data = array(
'id' => $id,
'url' => $src[0],
);
return $image_data;
}
}
$dynamic_tags->register_tag( 'MS_Custom_Archive_Image_Tag' );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment