Support » Plugin: Insert Pages » Increase headers (H1, H2, H3,etc) as needed

  • Resolved Jorge Raigoza

    (@forfe15)


    We are trying the plug-in and it works great. However, we would like to have an option within the short-code to increase the H#.

    We understand that there is the option to change the font of the content generated by the short-code. However, computer might misunderstand the final code (HTML mistakes).

    Example
    The page of this plugin (https://wordpress.org/plugins/insert-pages/) contains:
    H1 – Insert Pages
    H2 – Description
    H3 – Normal use
    H3 – Advanced use
    H2 – Screenshots

    The page of Pager Widget (https://wordpress.org/plugins/pager-widget/) contains:
    H1 – Pager Widget
    H2 – Description
    H2 – Screenshots

    Now, if in another page we want to talk about both plug-ins and insert them using the short-code, this would be the end result:
    H1 – Awesome plug-ins by Paul Ryan
    H2 – We Highly Recommend

    H1 – Insert Pages
    H2 – Description
    H3 – Normal use
    H3 – Advanced use
    H2 – Screenshots

    H1 – Pager Widget
    H2 – Description
    H2 – Screenshots

    As we can see above, there are multiple H1 tags, which are not divided per articles (I don’t want articles). So, I would recommend to have a short-code option to “Increase headers (H1, H2, H3) by:“.

    Let’s say we put the new setting to 2. This should be the result:
    H1 – Awesome plug-ins by Paul Ryan
    H2 – We Highly Recommend
    H3 – Insert Pages
    H4 – Description
    H5 – Normal use
    H5 – Advanced use
    H4 – Screenshots

    H3 – Pager Widget
    H4 – Description
    H4 – Screenshots

    I hope I was clear on my explanation. As of right now, we are using the filter insert_pages_wrap_content to alter the headers, but it’s difficult to know how many levels to go up

    **EDIT**
    We created the following code under our functions.php file to accomplish what we were looking for:

    function tcsp_insert_pages_wrapper_function( $content, $inserted_page, $attributes ) {
    	
    	// To increase headers, add a class that says "HPlus2" to the shortcode. E.g. [insert page='page-slug' display='content' class='HPlus2']
    	if ( !empty($attributes['class']) ) {
    
    		$PossibleHeaderIncrease = str_ireplace("HPlus", "", $attributes['class']);
    
    		if ( is_numeric($PossibleHeaderIncrease) ){
    			$HPlus = $PossibleHeaderIncrease;
    			$NewContent = $content;
    			
    			// Set the maximum to 10 because who uses H11+???
    			for($i = 10; $i > 0; $i--){
    					
    				$NewContent = str_ireplace("</h" . $i . ">", "</h" . ( $i + $HPlus ) . ">", str_ireplace("<h" . $i, "<h" . ( $i + $HPlus ), $NewContent));
    				
    			}
    			
    			return $NewContent;
    
    		} else {
    			// DO NOTHING
    			return $content;
    		}
    	
    	} else {
    		// DO NOTHING
    		return $content;
    		
    	}
    	
    }
    add_filter( 'insert_pages_wrap_content', 'tcsp_insert_pages_wrapper_function', 9, 3 );
    • This topic was modified 5 years, 3 months ago by Jorge Raigoza. Reason: Added workaround
    • This topic was modified 5 years, 3 months ago by Jorge Raigoza.
    • This topic was modified 5 years, 3 months ago by Jorge Raigoza. Reason: grammar
Viewing 1 replies (of 1 total)
  • Plugin Author Paul Ryan

    (@figureone)

    Sorry for the late reply! Your code looks great and is definitely more semantically correct (appropriate nesting of heading elements). I’m not sure I could integrate code like that into the core plugin, though, since many users rely on the inserted page markup being exactly the same as the original page (so their styling stays the same).

    Using the insert_pages_wrap_content filter is definitely the way to go. I suggest using preg_replace_callback() to simplify incrementing all the <h#> tags in the inserted page. Here’s an example:

    function tcsp_insert_pages_wrapper_function( $content, $inserted_page, $attributes ) {
    	$increase_heading_by = strpos( $attributes['class'], 'HPlus' ) !== false ? intval( str_ireplace( 'HPlus', '', $attributes['class'] ) ) : 0;
    	if ( $increase_heading_by > 0 ) {
    		$content = preg_replace_callback( '/(<\/?h)([\d]*)>/', function ( $match ) use ( $increase_heading_by ) { 
    			return $match[1] . ( intval( $match[2] ) + $increase_heading_by ) . '>'; 
    		}, $content );
    	}
    
    	return $content;
    }
    add_filter( 'insert_pages_wrap_content', 'tcsp_insert_pages_wrapper_function', 9, 3 );
Viewing 1 replies (of 1 total)
  • The topic ‘Increase headers (H1, H2, H3,etc) as needed’ is closed to new replies.