Hi,
I’m stuck with this. I’m trying to create a code with ACF repeater field that will add automatically new shortcodes to WordPress. These shortcodes are meant to add or remove a block of text depending on date period.
This is my code (working) for only one shortcode and no repeater field:
function expirationFunction( $atts, $content) {
$dateDebut = get_field('shortcode_date_debut','option');
$dateFin = get_field('shortcode_date_fin','option');
$dateDujour = date("Y-m-d");
if( $dateDebut <= $dateDujour && $dateFin >= $dateDujour ){
return $content;
}else{
return '';
}
}
add_shortcode('expiration', 'expirationFunction');
So whenever I add the [expiration] shortcode to a block of text and determine start and end dates in the page options, it will show or not this block of text. This is working fine.
But what I want is the ability to create new shortcodes without having to always copy the same code. I was thinking of a Repeater field and this is what I have so far (not working):
if( have_rows('ajouter_shortcode','option') ):
while ( have_rows('ajouter_shortcode','option') ) : the_row();
$expFunc = function() use ($content) {
$nomShortcode = get_sub_field('nom_shortcode','option');
$dateDebut = get_sub_field('dateDebut','option');
$dateFin = get_sub_field('dateFin','option');
$dateDujour = date("Y-m-d");
if( $dateDebut <= $dateDujour && $dateFin >= $dateDujour ){
return $content;
}else{
return '';
}
};
add_shortcode( $nomShortcode, $expFunc );
endwhile;
else :
endif;
Any idea on how to get this to work?
Thanks for your help!