Skip to content

Instantly share code, notes, and snippets.

@igorbenic
Last active April 11, 2024 09:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorbenic/e8f0e7d8badb97bfafe58fbb1f1fdf9e to your computer and use it in GitHub Desktop.
Save igorbenic/e8f0e7d8badb97bfafe58fbb1f1fdf9e to your computer and use it in GitHub Desktop.
Shortcode Hide/Show Content
<?php
add_action( 'init', 'ibenic_register_hide_shortcode' );
function ibenic_register_hide_shortcode() {
add_shortcode( 'hide_content_if', 'ibenic_hide_content_if' );
}
function ibenic_hide_content_if( $atts, $content ) {
$atts = shortcode_atts( array(
'userfield' => '',
'value' => '',
'compare' => '=',
'format' => 'strval', // format for the value we enter
'format_meta' => '', // format for the value from meta
), $atts, 'show_content_if' );
$user_id = get_current_user_id();
if ( ! $user_id ) {
return $content;
}
if ( ! $atts['userfield'] || ! $atts['value'] ) {
return $content;
}
$meta_value = get_user_meta( $user_id, $atts['userfield'], true );
$meta_value = $atts['format_meta'] ? call_user_func( $atts['format_meta'], $meta_value ) : $meta_value;
$value = $atts['format'] ? call_user_func( $atts['format'], $atts['value'] ) : $atts['value'];
$hide_content = false;
switch( $atts['compare'] ) {
case '<':
$hide_content = $value < $meta_value;
break;
case '<=':
$hide_content = $value <= $meta_value;
break;
case '>':
$hide_content = $value > $meta_value;
break;
case '>=':
$hide_content = $value >= $meta_value;
break;
default:
$hide_content = $value === $meta_value;
break;
}
if ( ! $hide_content ) {
return $content;
}
return '';
}
<?php
add_action( 'init', 'ibenic_register_show_shortcode' );
function ibenic_register_show_shortcode() {
add_shortcode( 'show_content_if', 'ibenic_show_content_if' );
}
function show_content_if( $atts, $content ) {
$atts = shortcode_atts( array(
'userfield' => '',
'value' => '',
'compare' => '=',
'format' => 'strval', // format for the value we enter
'format_meta' => '', // format for the value from meta
), $atts, 'show_content_if' );
$user_id = get_current_user_id();
if ( ! $user_id ) {
return '';
}
if ( ! $atts['userfield'] || ! $atts['value'] ) {
return '';
}
$meta_value = get_user_meta( $user_id, $atts['userfield'], true );
$meta_value = $atts['format_meta'] ? call_user_func( $atts['format_meta'], $meta_value ) : $meta_value;
$value = $atts['format'] ? call_user_func( $atts['format'], $atts['value'] ) : $atts['value'];
$show_content = false;
switch( $atts['compare'] ) {
case '<':
$show_content = $value < $meta_value;
break;
case '<=':
$show_content = $value <= $meta_value;
break;
case '>':
$show_content = $value > $meta_value;
break;
case '>=':
$show_content = $value >= $meta_value;
break;
default:
$show_content = $value === $meta_value;
break;
}
if ( ! $show_content ) {
return '';
}
return $content;
}
<?php
add_action( 'init', 'ibenic_register_toggle_shortcode' );
function ibenic_register_toggle_shortcode() {
add_shortcode( 'toggle_content_if', 'ibenic_toggle_content_if' );
}
function ibenic_toggle_content_if( $atts, $content ) {
$atts = shortcode_atts( array(
'action' => 'show',
'userfield' => '',
'value' => '',
'compare' => '=',
'format' => 'strval', // format for the value we enter
'format_meta' => '', // format for the value from meta
), $atts, 'show_content_if' );
$user_id = get_current_user_id();
$togglable_content = $content;
if ( 'hide' === $atts['action'] ) {
$togglable_content = '';
}
if ( ! $user_id ) {
return $togglable_content;
}
if ( ! $atts['userfield'] || ! $atts['value'] ) {
return $togglable_content;
}
$meta_value = get_user_meta( $user_id, $atts['userfield'], true );
$meta_value = $atts['format_meta'] ? call_user_func( $atts['format_meta'], $meta_value ) : $meta_value;
$value = $atts['format'] ? call_user_func( $atts['format'], $atts['value'] ) : $atts['value'];
$toggle_content = false;
switch( $atts['compare'] ) {
case '<':
$toggle_content = $value < $meta_value;
break;
case '<=':
$toggle_content = $value <= $meta_value;
break;
case '>':
$toggle_content = $value > $meta_value;
break;
case '>=':
$toggle_content = $value >= $meta_value;
break;
default:
$toggle_content = $value === $meta_value;
break;
}
if ( ! $toggle_content ) {
if ( 'hide' === $atts['action'] ) {
// If we are trying to hide it but we will not, return content.
return $content;
} else {
// If we are trying to show it but we can't, don't show it.
return '';
}
}
return $togglable_content;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment