I am currently working on a shortcode function, where I need to access the content of a post, before its rendering.
If I do it like this
$content = get_the_content( get_the_ID() );
It gives me the already rendered content. How can I access the content before rendering?
What I'm trying to do is create a shortcode, that generates a table of content from specific headlines that are placed with shortcodes. For that reason, the content still needs to have the unrendered shortcodes in it.
As requested, here is the complete code, so you might get an idea of what I am trying to do.
function nimaji_shortcodes() {
add_shortcode( 'nimaji_toc', 'generate_toc' );
}
add_action( 'wp_head', 'nimaji_shortcodes' );
function generate_toc() {
$content = get_the_content( get_the_ID() );
krumo($content);
// Look for specific shortcode, that has information about headlines in it
$pattern = '/\[x_custom_headline level="(.*?)"(.*?)\](.*?)\[\/x_custom_headline\]/';
preg_match_all( $pattern, $content, $matches );
$levels = $matches[1];
$titles = $matches[3];
$toc_meta = generate_toc_meta( $levels, $titles );
$html = '<ol>';
foreach ( $toc_meta as $key_2 => $level2 ) {
foreach ( $level2 as $key_3 => $level3 ) {
if ( $key_3 == 0 ) {
$html .= "<li>$level3</li>";
} else if ( $key_3 == 1 ) {
$html .= "<ol>";
$html .= "<li>$level3</li>";
} else if ( $key_3 == sizeof( $level2 ) - 1 ) {
$html .= "<li>$level3</li>";
$html .= "</ol>";
} else {
$html .= "<li>$level3</li>";
}
}
}
$html .= '</ol>';
return $html;
}
function generate_toc_meta( array $levels, array $title ) {
$new_levels = array();
$i = - 1;
$j = 0;
foreach ( $levels as $key => $level ) {
if ( $level == 'h2' ) {
$i ++;
$j = 0;
$new_levels[ $i ][ $j ] = $title[$key];
} else if ( $level == 'h3' ) {
$j ++;
$new_levels[ $i ][ $j ] = $title[$key];
} else {
continue;
}
}
return $new_levels;
}