Created
November 27, 2023 17:20
-
-
Save damiencarbery/dbbc6b4b2779153d1607876bdc3f5045 to your computer and use it in GitHub Desktop.
Add controls to Elementor Post Settings panel - Short demonstration to add a custom control to the Elementor Post Settings panel. https://www.damiencarbery.com/2023/11/add-controls-to-elementor-post-settings-panel/
This file contains 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 | |
/* | |
Plugin Name: Add controls to Elementor Post Settings panel | |
Description: Short demonstration to add a custom control to the Elementor Post Settings panel. | |
Plugin URI: https://www.damiencarbery.com/2023/11/add-controls-to-elementor-post-settings-panel/ | |
Version: 0.1 | |
Author: Damien Carbery | |
*/ | |
defined( 'ABSPATH' ) || exit; | |
class AddLoginRequiredSettingsToElementor { | |
// Returns an instance of this class. | |
public static function get_instance() { | |
if ( null == self::$instance ) { | |
self::$instance = new self; | |
} | |
return self::$instance; | |
} | |
// Initialize the plugin variables. | |
public function __construct() { | |
$this->init(); | |
} | |
// Set up WordPress specfic actions. | |
public function init() { | |
add_action( 'elementor/documents/register_controls', array( $this, 'register_login_required_document_controls' ) ); | |
// | |
add_action( 'wp_head', array( $this, 'retrieve_elementor_login_required_value' ) ); | |
} | |
// Add document controls to Page Settings/Settings tab. | |
// @param \Elementor\Core\DocumentTypes\PageBase $document The PageBase document instance. | |
function register_login_required_document_controls( $document ) { | |
if ( ! $document instanceof \Elementor\Core\DocumentTypes\PageBase || ! $document::get_property( 'has_elements' ) ) { | |
return; | |
} | |
$document->start_controls_section( | |
'test_section', | |
[ | |
'label' => esc_html( 'Login required' ), | |
'tab' => \Elementor\Controls_Manager::TAB_SETTINGS, | |
// Other tabs: TAB_STYLE, TAB_ADVANCED | |
] | |
); | |
$document->add_control( | |
'login_required', | |
[ | |
'label' => esc_html( 'Login required' ), | |
'description' => esc_html( 'Enable to require that the user must be logged in to see this page.' ), | |
'type' => \Elementor\Controls_Manager::SWITCHER, | |
// Other control types: CHOOSE, COLOR, DIMENSIONS, HEADING, HOVER_ANIMATION, ICONS, RAW_HTML, SELECT, SLIDER, TEXT. | |
] | |
); | |
$document->end_controls_section(); | |
} | |
// Retrive the value of the Login Required switcher control and add comment to header. | |
function retrieve_elementor_login_required_value() { | |
if ( defined( 'ELEMENTOR_VERSION' ) ) { | |
$current_doc = Elementor\Plugin::instance()->documents->get( get_the_ID() ); | |
//error_log( 'Current doc: ' . var_export( $current_doc->get_settings(), true ) ); | |
if ( $current_doc && 'yes' === $current_doc->get_settings( 'login_required' ) ) { | |
echo '<!-- Login required for this page -->'; | |
//error_log( 'Login required for this page.' ); | |
} | |
else { | |
echo '<!-- Login NOT required for this page -->'; | |
//error_log( 'Login NOT required for this page.' ); | |
} | |
} | |
} | |
} | |
$AddLoginRequiredSettingsToElementor = new AddLoginRequiredSettingsToElementor(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment