Plugin Author
Brecht
(@brechtvds)
At that point the template should already be part of the post content. Are you still seeing the shortcode in the $content variable?
You could try increasing the priority (to 99, for example).
Alternatively, add that same filter to this hook as well: wprm_recipe_shortcode_output
Thread Starter
Jeremy
(@jdesire)
The template is already part of the content, because my function outputs an h2 for the recipe name, even though I changed the recipe name block tag to be a span.
That’s why I’m confused and guess that the plugin is first outputting the base content, and then change the tags according to block properties.
I already tried changing the priority without success.
Plugin Author
Brecht
(@brechtvds)
It doesn’t change anything afterwards.
Do you have the correct recipe template selected for the output?
Thread Starter
Jeremy
(@jdesire)
Yes I have. But actually, I think I tried to isolate the problem too much, and I’ve hidden the issue… my bad.
My ultimate goal is to display a table of content (that’s why I’ve first added anchors on headings). So for this, I’m using a shortcode that is calling a function.
I think the problem is that, at the beginning of my function, I get the content like so :
global $post;
$original_content = $post->post_content;
Is there anything I do wrong ?
It is obviously something wrong in my code, but my table of content works fine for content outside of the WPRM Recipe block.
Just in case, here is the page I need help with, and here is the complete code in functions.php:
function replace_ca($matches){
return '<h'.$matches[1].$matches[2].' id="'.sanitize_title($matches[3]).'">'.$matches[3].'</h'.$matches[4].'>';
}
add_filter('the_content', 'add_anchor_to_title', 99);
function add_anchor_to_title($content){
if(is_singular('post')){
global $post;
$pattern = "/<h([2-4])(.*?)>(.*?)<\/h([2-4])>/i";
$content = preg_replace_callback($pattern, 'replace_ca', $content);
return $content;
}else{
return $content;
}
}
function generate_toc(){
global $post;
$obj = '<div class="php-toc"><h2>Table des matières</h2><nav>';
$original_content = $post->post_content;
$patt = "/<h([2-4])(.*?)>(.*?)<\/h([2-4])>/i";
preg_match_all($patt, $original_content, $results);
$lvl1 = 0;
$lvl2 = 0;
$lvl3 = 0;
foreach ($results[3] as $k=> $r) {
switch($results[1][$k]){
case 2:
$lvl1++;
$niveau = '<span class="title_lvl">'.$lvl1.'/ </span>';
$lvl2 = 0;
$lvl3 = 0;
break;
case 3:
$lvl2++;
$niveau = '<span class="title_lvl">'.base_convert(($lvl2+9),10,36).'. </span>';
if ($lvl3 != 0)
$obj .= '</ul>';
$lvl3 = 0;
break;
case 4:
$lvl3++;
if ($lvl3 == 1)
$obj .= '<ul>';
$niveau = '';
break;
}
if ($lvl3 != 0)
$obj .= '<li>';
$obj .= '<a href="#'.sanitize_title($r).'" class="title_lvl'.$results[1][$k].'">'.$niveau.$r.'</a></br>';
if ($lvl3 != 0)
$obj .= '</li>';
}
$obj .= '</nav></div>';
return $obj;
}
add_shortcode('table-of-content','generate_toc');
Plugin Author
Brecht
(@brechtvds)
Could you try using this instead:
$original_content = apply_filters( 'the_content', $post->post_content );
Thread Starter
Jeremy
(@jdesire)
The change is applied if you want to check the page, but it fails loading.
I tried removing everything from the function except your code and returning an empty string, just to make sure that the issue is not coming from the regex or the loop, still not loading.
Plugin Author
Brecht
(@brechtvds)
Could you send an email to [email protected]
Plugin Author
Brecht
(@brechtvds)
Just to circle back: my previous suggestion (using just apply_filters) causes an infinite loop but using this code fixes the problem:
$original_content = $post->post_content;
// Remove TOC shortcode to prevent infinite nesting.
$original_content = str_ireplace( '[table-of-content]', '', $original_content );
// Apply the content filter.
$original_content = apply_filters( 'the_content', $original_content );