Skip to content

Instantly share code, notes, and snippets.

@pingram3541
Last active December 16, 2024 15:49
Show Gist options
  • Save pingram3541/d1a98d35fa39b3101c8723570634d528 to your computer and use it in GitHub Desktop.
Save pingram3541/d1a98d35fa39b3101c8723570634d528 to your computer and use it in GitHub Desktop.
Elementor Element Display Condition by Post ID
<?php
/**
* Plugin Name: Elementor Conditions for Elements
* Description: A plugin to add custom display conditions
* Plugin URI: https://elementor.com/
* Version: 1.0.0
* Author: Phil Ingram
* Author URI: https://developers.elementor.com/
* Text Domain: elementor-conditions
*
* Requires Plugins: elementor
* Elementor tested up to: 3.26.0
* Elementor Pro tested up to: 3.26.0
*
*
*
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
if(function_exists('is_plugin_active')){
$elementorpro_active = is_plugin_active('elementor-pro/elementor-pro.php');
} else {
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
$elementorpro_active = is_plugin_active('elementor-pro/elementor-pro.php');
}
if($elementorpro_active){
// extends class ElementorPro\Modules\DisplayConditions\Classes\Conditions_Manager
add_action( 'elementor/display_conditions/register', 'ec_add_elementor_conditions' );
}
function ec_add_elementor_conditions( $conditions_manager ) {
require_once( __DIR__ . '/display-conditions/post-id-condition.php' );
$conditions_manager->register_condition_instance( new \Post_ID_Condition() );
}
//fin
<?php
/**
* Post ID as condition (loop element, product element, cpt etc.)
* Place in folder /display-conditions/
*
* ref: /elementor-pro/modules/display-conditions/conditions/
*/
use Elementor\Controls_Manager;
use ElementorPro\Modules\DisplayConditions\Classes\Comparator_Provider;
use ElementorPro\Modules\DisplayConditions\Classes\Comparators_Checker;
use ElementorPro\Modules\DisplayConditions\Conditions\Base\Condition_Base;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class Post_ID_Condition extends Condition_Base {
public function get_name() {
return 'post_id';
}
public function get_label() {
return esc_html__( 'Post ID', 'elementor-pro' );
}
public function get_group() {
return 'post';
}
public function check( $args ) :bool {
$value = [ $args['post_id'] ];
$current_post = get_post();
return Comparators_Checker::check_array_contains( $args['comparator'], [ $current_post->ID ], $value );
}
public function get_options() {
$comparators = Comparator_Provider::get_comparators(
[
Comparator_Provider::COMPARATOR_IS,
Comparator_Provider::COMPARATOR_IS_NOT,
Comparator_Provider::COMPARATOR_CONTAINS,
Comparator_Provider::COMPARATOR_NOT_CONTAIN,
Comparator_Provider::COMPARATOR_IS_EMPTY,
Comparator_Provider::COMPARATOR_IS_NOT_EMPTY,
]
);
$this->add_control(
'comparator',
[
'type' => Controls_Manager::SELECT,
'options' => $comparators,
'default' => Comparator_Provider::COMPARATOR_IS,
]
);
$this->add_control(
'post_id',
[
'label' => esc_html__( 'Post ID', 'elementor-pro' ),
'type' => Controls_Manager::TEXT,
'placeholder' => esc_html__( 'Post ID', 'elementor-pro' ),
'required' => true,
]
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment