Skip to content

Instantly share code, notes, and snippets.

@ethernick
Last active June 4, 2024 21:59
Show Gist options
  • Save ethernick/c6330bd413779f11d43c229bd08703c9 to your computer and use it in GitHub Desktop.
Save ethernick/c6330bd413779f11d43c229bd08703c9 to your computer and use it in GitHub Desktop.
Here's a little short code I use to add some conditional magic for federating content. Use case: If the post is short, just post the content - no title; no permalink.
<?php
function conditional_shortcode($atts, $content, $tag){
global $post;
$atts = shortcode_atts(
array( 'length' => 400, 'condition' => 'gt' ),
$atts,
$tag
);
$excerpt_length = intval( $atts['length'] );
if ( 0 === $excerpt_length ) {
$excerpt_length = 400;
}
$the_content = get_the_content($post->ID);
$content_length = strlen( $the_content );
Activitypub\Shortcodes::register();
switch($atts['condition']){
case "lt":
if ($content_length < $excerpt_length) {
return do_shortcode($content);
}
break;
default:
if ($content_length >= $excerpt_length) {
return do_shortcode($content);
}
}
return '';
}
add_shortcode( 'ap_if', 'conditional_shortcode');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment