Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
新增客製化 Elementor 小工具的方法
<?php
define('KS_MINIMUM_PHP_VERSION', 5.6); //定義最小支援的 PHP 版本
define('KS_MINIMUM_ELEMENTOR_VERSION', 2.0); //定義最小支援的 Elementor 版本
$ks_ele_widget_enable = true;
// Check if Elementor installed and activated
if (!did_action('elementor/loaded')) {
add_action('admin_notices', function () {
$message = sprintf(
/* translators: 1: Plugin name 2: Elementor */
esc_html__('"%1$s" requires "%2$s" to be installed and activated.', 'elementor-hello-world'),
'<strong>SHL Elementor Widget</strong>',
'<strong>Elementor</strong>'
);
printf('<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $message);
});
$ks_ele_widget_enable = false;
}
// Check for required Elementor version
if (!version_compare(ELEMENTOR_VERSION, KS_MINIMUM_ELEMENTOR_VERSION, '>=')) {
add_action('admin_notices', function () {
$message = sprintf(
/* translators: 1: Plugin name 2: Elementor 3: Required Elementor version */
esc_html__('"%1$s" requires "%2$s" version %3$s or greater.', 'elementor-hello-world'),
'<strong>SHL Elementor Widget</strong>',
'<strong>Elementor</strong>',
KS_MINIMUM_ELEMENTOR_VERSION
);
printf('<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $message);
});
$ks_ele_widget_enable = false;
}
// Check for required PHP version
if (version_compare(PHP_VERSION, KS_MINIMUM_PHP_VERSION, '<')) {
add_action('admin_notices', function () {
$message = sprintf(
/* translators: 1: Plugin name 2: PHP 3: Required PHP version */
esc_html__('"%1$s" requires "%2$s" version %3$s or greater.', 'elementor-hello-world'),
'<strong>SHL Elementor Widget</strong>',
'<strong>PHP</strong>',
KS_MINIMUM_PHP_VERSION
);
printf('<div class="notice notice-warning is-dismissible"><p>%1$s</p></div>', $message);
});
$ks_ele_widget_enable = false;
}
if ($ks_ele_widget_enable) {
function ks_shl_elementor_widgets() {
class Elementor_SHL_Widget extends \Elementor\Widget_Base {
public function get_name() {
return 'shl-random-banner-post';
}
public function get_title() {
return 'SHL Random Banner Post';
}
public function get_icon() {
return 'fa fa-code';
}
public function get_categories() {
return ['general'];
}
protected function _register_controls() {
global $wpdb;
$this->start_controls_section(
'content_section',
[
'label' => 'Content',
'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
]
);
$querystr = "SELECT ID,post_title FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'services' AND $wpdb->posts.post_date < NOW() ORDER BY $wpdb->posts.post_date DESC";
$posts = $wpdb->get_results($querystr, ARRAY_A);
$p_arr = array(0 => 'None');
foreach ($posts as $key => $post) {
$p_arr[$post['ID']] = $post['post_title'];
}
$this->add_control(
'services_posts',
[
'label' => 'Choose a Post',
'type' => \Elementor\Controls_Manager::SELECT,
'default' => 0,
'options' => $p_arr,
]
);
$this->end_controls_section();
}
protected function render() {
$settings = $this->get_settings_for_display();
$img = 'blank_image_url';
if (function_exists('get_field')) {
$num = time() % 3 + 1;
$img = get_field("services_random_img_" . $num, $settings['services_posts']);
}
echo '<div class="ks post-' . $settings['services_posts'] . '">';
echo '<img src="' . $img . '"/>';
echo '</div>';
}
protected function _content_template() {
echo '<div class="ks"> {{ settings.services_posts }}</div>';
}
}
\Elementor\Plugin::instance()->widgets_manager->register_widget_type(new \Elementor_SHL_Widget());
}
add_action('elementor/widgets/widgets_registered', 'ks_shl_elementor_widgets');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.