Last active
June 4, 2024 21:59
-
-
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.
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 | |
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