Skip to content

Instantly share code, notes, and snippets.

@Crocoblock
Created December 23, 2024 16:11
Show Gist options
  • Save Crocoblock/a4495d6a2bc2110665e17e738a65de71 to your computer and use it in GitHub Desktop.
Save Crocoblock/a4495d6a2bc2110665e17e738a65de71 to your computer and use it in GitHub Desktop.
JetEngine SEOPress output checkbox/radio/select field, repeater field
<?php
add_filter('seopress_titles_template_variables_array', 'sp_titles_template_variables_array');
add_filter('seopress_titles_template_replace_array', 'sp_titles_template_replace_array');
add_filter('seopress_get_dynamic_variables', 'sp_get_dynamic_variables');
function sp_titles_template_variables_array($array) {
$array[] = '%%je_repeater_template%%';
$array[] = '%%je_checkbox_template%%';
return $array;
}
function sp_titles_template_replace_array($array) {
//escape your values!
$post_id = get_the_ID();
$array[] = esc_attr(wp_strip_all_tags( sp_je_get_repeater_value( $post_id ) ) );
$array[] = esc_attr(wp_strip_all_tags( sp_je_get_field_value( $post_id, 'facilities', 22 ) ) );
return $array;
}
function sp_get_dynamic_variables($array){
$array['%%je_repeater_template%%'] = 'Repeater value';
$array['%%je_checkbox_template%%'] = 'Checkbox value';
return $array;
}
//----------------------------
function sp_je_get_field_value( $post_id, $field, $glossary_id = 0 ) {
if ( ! function_exists( 'jet_engine' ) ) {
return '';
}
$value = get_post_meta( $post_id, $field, true );
if ( ! $value ) {
return '';
}
if ( ! $glossary_id || ! jet_engine()->glossaries->data->get_item_for_edit( absint( $glossary_id ) ) ) {
if ( is_array( $value ) ) {
return implode( ', ', jet_engine_get_prepared_check_values( $value ) );
} elseif ( is_scalar( $value ) ) {
return $value;
} else {
return '';
}
}
return jet_engine_label_by_glossary( $value, $glossary_id );
}
function sp_je_get_repeater_value( $post_id ) {
//get repeater field value from post meta
$participants = get_post_meta( $post_id, 'participants', true );
//fallback in case repeater field is empty
if ( empty( $participants ) || ! is_array( $participants ) ) {
return 'The best artists will present their artworks.';
}
$output = 'The best artists will present their artworks. Honored guests: ';
$participants_formatted = [];
foreach ( $participants as $participant ) {
$participants_formatted[] = sprintf( '%s %s', $participant['first_name'], $participant['last_name'] );
}
$output .= implode( ', ', $participants_formatted ) . '.';
return $output;
}
@Crocoblock
Copy link
Author

Based on this guide https://www.seopress.org/support/guides/create-your-custom-dynamic-variable-for-your-meta-title-description-social/
Read it to understand how variables are registered in SEOPress

Glossary ID can be seen ib JetEngine dashboard here:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment