Site logo

[Resolved] Insert block element through PHP

Home Forums Support [Resolved] Insert block element through PHP

Home Forums Support Insert block element through PHP

  • This topic has 3 replies, 2 voices, and was last updated 5 years ago by David.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1718812
    Frank

    Hi all,

    I’d like to share something and ask something at the same time 😉
    I’ve seen this question multiple times on the forum and like to share the solution I’ve created with help from David’s example. The goal was to add a CTA in blog-posts in a dynamic way, preferably right before the last h2 paragraph, but not further then the fourth paragraph in a blog, but only then whe quantity of h2’s is greater then one.

    The real-example is a bit more advanced where I offer different CTA’s according to the tags/category of the post, but this will give a basic example.

    add_filter( 'the_content', 'db_prefix_first_h2_custom_html' );
    
    function db_prefix_first_h2_custom_html( $content ) {
    
        $insertion = '<div style="width: 100%; background-color: red;">My custom HTML before last H2</div>';
    
        if ( is_single() && ! is_admin() ) {
            return prefix_insert_h2( $insertion, $content );
        }
    
        return $content;
    }
    
    function prefix_insert_h2( $insertion, $content ) {
        $h2tag = '<h2>';
        $h2s = explode( $h2tag, $content );
    	
    	  // One of the H2's will come 
    	  $totalcount = count($h2s) -2 ;
    	
    	  if($totalcount > 0) {
    			  if($totalcount > 4) {
    					$totalcount = 4;
    				}
    				if ($h2s) {
    						$h2s[$totalcount] = $h2s[$totalcount].$insertion;
    				}    			
    		}
    	
    
        return implode( '<h2>', $h2s );
    }

    So what I would like to do now is to create the specific CTA’s through block-elements and that is where I need a little support; is there a way to insert specific blocks/elements through php?

    I know of the posibillity to replicate this code and attach it only to posts which have a specific category or tag, but I’d like to keep things centralized.

    #1718878
    David
    Staff
    Customer Support

    Hi there,

    tricky one as Elements only work with Hooks and adding a Hook inside the Content is not ‘normally’ possible…. but theres a hacky way of doing it by creating a shortcode that contains a hook – see here:

    https://generatepress.com/forums/topic/hook/#post-1038253

    And here’s for reference is an example of the inserter code outputting a shortcode:

    https://generatepress.com/forums/topic/html-code-hook-within-content/#post-1597391

    So you could try filtering in the shortcode, that outputs a hook that can then be targeted using Elements….. BUT i am not 100% sure if this will work if you’re using Blocks and Block plugins as the way the CSS is generated for those blocks generally happens on page parsing so it may break….

    #1718911
    Frank

    Hi David,
    Thanks for the creative solution, but that doesn’t sound like a path I would like to walk in terms of (future) stability 😉
    I’ll just stick with adding the HTML in the code snippet itself for now!

    #1718923
    David
    Staff
    Customer Support

    I thought that would be the case 🙂 It’s super convoluted.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.