-
-
Save robertdevore/97a47e8de3f82688d6153f2a77e1a8e0 to your computer and use it in GitHub Desktop.
Conditionally hide modules in WPBakery based on a date setting
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 | |
/** | |
* The plugin bootstrap file | |
* | |
* @link https://robertdevore.com | |
* @since 1.0.0 | |
* @package WPBakery_Module_Visibility | |
* | |
* @wordpress-plugin | |
* | |
* Plugin Name: WPBakery Module Visibility | |
* Description: Adds a conditional option to hide modules after a specific date in WPBakery. | |
* Plugin URI: https://www.robertdevore.com/ | |
* Version: 1.0.0 | |
* Author: Robert DeVore | |
* Author URI: https://robertdevore.com/ | |
* License: GPL-2.0+ | |
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt | |
* Text Domain: wpbakery-module-visibility | |
* Domain Path: /languages | |
*/ | |
// If this file is called directly, abort. | |
if ( ! defined( 'WPINC' ) ) { | |
die; | |
} | |
/** | |
* Current plugin version. | |
*/ | |
define( 'WPBAKERY_MODULE_VISIBILITY_VERSION', '1.0.0' ); | |
/** | |
* Add a custom date parameter to the WPBakery module settings to control visibility. | |
* | |
* @return void | |
*/ | |
function custom_vc_module_visibility_settings() { | |
// Get all existing shortcodes. | |
$shortcodes = WPBMap::getShortCodes(); | |
// Loop through each shortcode and add the custom parameter. | |
foreach ( $shortcodes as $shortcode => $data ) { | |
vc_add_param( $shortcode, array( | |
'type' => 'textfield', | |
'heading' => __( 'Hide Module After Date', 'wpbakery-module-visibility' ), | |
'param_name' => 'hide_after_date', | |
'description' => __( 'Add a date and hide the module after that date.', 'wpbakery-module-visibility' ), | |
) ); | |
} | |
} | |
add_action( 'vc_after_init', 'custom_vc_module_visibility_settings' ); | |
/** | |
* Check if a date is in the past. | |
* | |
* @param string $hide_after_date The date to check. | |
* | |
* @return bool True if the date is in the past, false otherwise. | |
*/ | |
function is_past_date( $hide_after_date ) { | |
return $hide_after_date && strtotime( $hide_after_date ) <= strtotime( 'today' ); | |
} | |
/** | |
* Custom function to modify the output of VC shortcodes based on the 'hide_after_date' parameter. | |
* | |
* @param string $output The original shortcode output. | |
* @param object $object The VC map object. | |
* @param array $atts The attributes of the shortcode. | |
* | |
* @return string The modified shortcode output. | |
*/ | |
function custom_vc_module_visibility_output( $output, $object, $atts ) { | |
// Check if the 'hide_after_date' parameter is set | |
$hide_after_date = isset( $atts['hide_after_date'] ) ? $atts['hide_after_date'] : false; | |
// If 'hide_after_date' is in the past, return an empty string to hide the module. | |
if ( is_past_date( $hide_after_date ) ) { | |
return ''; | |
} | |
return $output; | |
} | |
add_filter( 'vc_shortcode_output', 'custom_vc_module_visibility_output', 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment