Support » Plugin: WP Recipe Maker » When does a block tag is changed ?

  • Resolved Jeremy

    (@jdesire)


    Hi,

    I customized a Recipe Template, and I added some blocks, like a Recipe Equipment, with a header tag set to H3, this works fine.

    I have a child theme with the following filter, that adds anchors to H2/H3/H4 tags:

    add_filter('the_content', 'add_anchor_to_title', 12);
    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;
      }
    }

    And it is working fine for content outside of the Recipe Template.

    But, for some reasons, there is no anchor on the Equipment H3 tag.
    I guess that WP Recipe Maker is changing the tag structure at some point, and that my filter is applied before.

    How can I run my function after WP Recipe Maker changed the tag structure ?

    Thanks for your help,
    Jeremy

Viewing 8 replies - 1 through 8 (of 8 total)
  • 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 support@bootstrapped.ventures

    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 );
Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.